From d8ec6b14747e994bdd89db66dbd17b1ed908c658 Mon Sep 17 00:00:00 2001 From: Maryna Malakhova Date: Wed, 10 Mar 2021 15:10:17 +0200 Subject: [PATCH 1/4] SCISSORS: Add Makefile and main Add main file with initial output (according to task02). Add Makefile. Signed-off-by: Maryna Malakhova --- scissors/scissors_game/Makefile | 6 ++++++ scissors/scissors_game/main.c | 8 ++++++++ 2 files changed, 14 insertions(+) create mode 100644 scissors/scissors_game/Makefile create mode 100644 scissors/scissors_game/main.c diff --git a/scissors/scissors_game/Makefile b/scissors/scissors_game/Makefile new file mode 100644 index 00000000..45ec3813 --- /dev/null +++ b/scissors/scissors_game/Makefile @@ -0,0 +1,6 @@ +CC=gcc +SOURCES=main.c +EXECUTABLE=scissors_game + +all: $(SOURCES) + $(CC) $(SOURCES) -o $(EXECUTABLE) diff --git a/scissors/scissors_game/main.c b/scissors/scissors_game/main.c new file mode 100644 index 00000000..9036b018 --- /dev/null +++ b/scissors/scissors_game/main.c @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0 +#include + +int main(void) +{ + printf("Please choose: rock (r) - paper (p) - scissors (s)\n"); +} + From 42eaa7741675c12ad27e4aca5b3f5be22e2a07e0 Mon Sep 17 00:00:00 2001 From: Maryna Malakhova Date: Wed, 10 Mar 2021 15:47:53 +0200 Subject: [PATCH 2/4] SCISSORS: Add reading users input and it`s convertion to text Add users input reading and printing if it is a wrong one Signed-off-by: Maryna Malakhova --- scissors/scissors_game/Makefile | 2 +- scissors/scissors_game/main.c | 16 +++++++++++++++- scissors/scissors_game/scissors.c | 20 ++++++++++++++++++++ scissors/scissors_game/scissors.h | 8 ++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 scissors/scissors_game/scissors.c create mode 100644 scissors/scissors_game/scissors.h diff --git a/scissors/scissors_game/Makefile b/scissors/scissors_game/Makefile index 45ec3813..b411d57c 100644 --- a/scissors/scissors_game/Makefile +++ b/scissors/scissors_game/Makefile @@ -1,5 +1,5 @@ CC=gcc -SOURCES=main.c +SOURCES=main.c scissors.c EXECUTABLE=scissors_game all: $(SOURCES) diff --git a/scissors/scissors_game/main.c b/scissors/scissors_game/main.c index 9036b018..aae09735 100644 --- a/scissors/scissors_game/main.c +++ b/scissors/scissors_game/main.c @@ -1,8 +1,22 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include +#include "scissors.h" + +static const char * const Game_Elements[] = { + "rock", "paper", "scissors", "unknown"}; int main(void) { - printf("Please choose: rock (r) - paper (p) - scissors (s)\n"); + char input; + int res; + + do { + printf("Please choose: rock (r) - paper (p) - scissors (s)\n"); + scanf("%1s", &input); + res = char_to_element_index(input); + if (res == UNKNOWN) + printf("You've chosen something strange!\n"); + } while (1); } diff --git a/scissors/scissors_game/scissors.c b/scissors/scissors_game/scissors.c new file mode 100644 index 00000000..d560e0a8 --- /dev/null +++ b/scissors/scissors_game/scissors.c @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "scissors.h" + +int char_to_element_index(char c) +{ + int ans = UNKNOWN; + + switch (c) { + case 'r': + ans = ROCK; + break; + case 'p': + ans = PAPER; + break; + case 's': + ans = SCISSORS; + break; + } + return ans; +} diff --git a/scissors/scissors_game/scissors.h b/scissors/scissors_game/scissors.h new file mode 100644 index 00000000..5ad75f5d --- /dev/null +++ b/scissors/scissors_game/scissors.h @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define ROCK 0 +#define PAPER 1 +#define SCISSORS 2 +#define UNKNOWN 3 + +int char_to_element_index(char c); From dcf92dac1bc0c4308a014d58569b5ad70977e26c Mon Sep 17 00:00:00 2001 From: Maryna Malakhova Date: Wed, 10 Mar 2021 18:08:53 +0200 Subject: [PATCH 3/4] SCISSORS: Add random choise of the computers' decision Init random. Add new random decision of the computer's choise. Finish intermidiate output. Signed-off-by: Maryna Malakhova --- scissors/scissors_game/main.c | 9 ++++++++- scissors/scissors_game/scissors.c | 12 ++++++++++++ scissors/scissors_game/scissors.h | 2 ++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/scissors/scissors_game/main.c b/scissors/scissors_game/main.c index aae09735..e001381c 100644 --- a/scissors/scissors_game/main.c +++ b/scissors/scissors_game/main.c @@ -10,13 +10,20 @@ int main(void) { char input; int res; + int comp_choise; + init_scissors(); do { printf("Please choose: rock (r) - paper (p) - scissors (s)\n"); scanf("%1s", &input); res = char_to_element_index(input); - if (res == UNKNOWN) + if (res == UNKNOWN) { printf("You've chosen something strange!\n"); + continue; + } + comp_choise = play_scissors(); + printf("You choose %s, I choose %s\n", + Game_Elements[res], Game_Elements[comp_choise]); } while (1); } diff --git a/scissors/scissors_game/scissors.c b/scissors/scissors_game/scissors.c index d560e0a8..8c005200 100644 --- a/scissors/scissors_game/scissors.c +++ b/scissors/scissors_game/scissors.c @@ -1,4 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 +#include +#include #include "scissors.h" int char_to_element_index(char c) @@ -18,3 +20,13 @@ int char_to_element_index(char c) } return ans; } + +void init_scissors(void) +{ + srand(time(NULL)); +} + +int play_scissors(void) +{ + return (rand()%3); +} diff --git a/scissors/scissors_game/scissors.h b/scissors/scissors_game/scissors.h index 5ad75f5d..943e1268 100644 --- a/scissors/scissors_game/scissors.h +++ b/scissors/scissors_game/scissors.h @@ -6,3 +6,5 @@ #define UNKNOWN 3 int char_to_element_index(char c); +void init_scissors(void); +int play_scissors(void); From 889a3ccd4ce20a790f502dc1f1035aecb43d354a Mon Sep 17 00:00:00 2001 From: Maryna Malakhova Date: Wed, 10 Mar 2021 21:07:33 +0200 Subject: [PATCH 4/4] SCISSORS: Add game and Finish task Add table an function in order to get the winner Add game result output Signed-off-by: Maryna Malakhova --- scissors/scissors_game/main.c | 13 +++++++++++++ scissors/scissors_game/scissors.c | 18 ++++++++++++++++++ scissors/scissors_game/scissors.h | 4 ++++ 3 files changed, 35 insertions(+) diff --git a/scissors/scissors_game/main.c b/scissors/scissors_game/main.c index e001381c..0a5b9e16 100644 --- a/scissors/scissors_game/main.c +++ b/scissors/scissors_game/main.c @@ -24,6 +24,19 @@ int main(void) comp_choise = play_scissors(); printf("You choose %s, I choose %s\n", Game_Elements[res], Game_Elements[comp_choise]); + switch (get_game_result(res, comp_choise)) { + case LOST: + printf("I win: %s beats %s\n", + Game_Elements[comp_choise], Game_Elements[res]); + break; + case WINN: + printf("You win: %s beats %s\n", + Game_Elements[res], Game_Elements[comp_choise]); + break; + case DRAW: + printf("It's draw\n"); + break; + } } while (1); } diff --git a/scissors/scissors_game/scissors.c b/scissors/scissors_game/scissors.c index 8c005200..d4693c20 100644 --- a/scissors/scissors_game/scissors.c +++ b/scissors/scissors_game/scissors.c @@ -3,6 +3,19 @@ #include #include "scissors.h" +/* + * Table indicates user's lost or win + * The row index corresponds to user's choise + * The column index corresponds to comp's choice + */ + +static const int game_table[3][3] = { +/* rock paper scissors */ +/* rock*/ DRAW, LOST, WINN, +/* paper*/ WINN, DRAW, LOST, +/* sciss*/ LOST, WINN, DRAW +}; + int char_to_element_index(char c) { int ans = UNKNOWN; @@ -30,3 +43,8 @@ int play_scissors(void) { return (rand()%3); } + +int get_game_result(int user, int comp) +{ + return game_table[user][comp]; +} diff --git a/scissors/scissors_game/scissors.h b/scissors/scissors_game/scissors.h index 943e1268..9e922147 100644 --- a/scissors/scissors_game/scissors.h +++ b/scissors/scissors_game/scissors.h @@ -4,7 +4,11 @@ #define PAPER 1 #define SCISSORS 2 #define UNKNOWN 3 +#define DRAW 0 +#define LOST 1 +#define WINN 2 int char_to_element_index(char c); void init_scissors(void); int play_scissors(void); +int get_game_result(int user, int comp);