From b741ed384779e6c43a18f1ad68d919d5d9c5148e Mon Sep 17 00:00:00 2001 From: Eugene Date: Wed, 17 Feb 2021 11:53:16 +0200 Subject: [PATCH 1/7] Create README.md --- scissors/README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 scissors/README.md diff --git a/scissors/README.md b/scissors/README.md new file mode 100644 index 0000000..1182c39 --- /dev/null +++ b/scissors/README.md @@ -0,0 +1,14 @@ +# git homework +Create a rock-paper-scissors console game +(rules https://en.wikipedia.org/wiki/Rock_paper_scissors) + +The game should be simple, the goal is to create a git project with proper commit order and structure. E.g.: +``` +> Please choose: rock (r) - paper (p) - scissors (s) +> r +> You choose rock, I choose paper +> I win: paper beats rock +``` +Please add your sources into folder 'scissors' + +Optional task: add printing md5 checksum of computer's choice before human's move. From 2cca301202a2cec337c873ba20aaedec7b0e8cee Mon Sep 17 00:00:00 2001 From: Maryna Malakhova Date: Wed, 10 Mar 2021 15:10:17 +0200 Subject: [PATCH 2/7] 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 | 6 ++++++ 2 files changed, 12 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 0000000..45ec381 --- /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 0000000..816613d --- /dev/null +++ b/scissors/scissors_game/main.c @@ -0,0 +1,6 @@ +#include + +int main(void) +{ + printf("Please choose: rock (r) - paper (p) - scissors (s)\n"); +} From 17d01f6648fdadad5e78836a6f2ee89e5e054e95 Mon Sep 17 00:00:00 2001 From: Maryna Malakhova Date: Wed, 10 Mar 2021 15:47:53 +0200 Subject: [PATCH 3/7] SCISSORS: Add reading users input and it`s convertion to text Add users input reading and printing it out after convertion to text Signed-off-by: Maryna Malakhova --- scissors/scissors_game/main.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/scissors/scissors_game/main.c b/scissors/scissors_game/main.c index 816613d..ee51dae 100644 --- a/scissors/scissors_game/main.c +++ b/scissors/scissors_game/main.c @@ -1,6 +1,32 @@ #include +#include + +void static char_to_text(char c, char *res, int size) +{ + switch(c) { + case 'r': + strncpy(res, "rock", size); + break; + case 'p': + strncpy(res, "paper", size); + break; + case 's': + strncpy(res, "scissors", size); + break; + default: + strncpy(res, "unknown", size); + } +} int main(void) { - printf("Please choose: rock (r) - paper (p) - scissors (s)\n"); + while(1){ + printf("Please choose: rock (r) - paper (p) - scissors (s)\n"); + char input; + char res [10]; + memset (res, 0, 10); + scanf ("%1s", &input); + char_to_text(input, res, sizeof(res)-1); + printf("You choose %s and it's ok!\n", res); + } } From e8e3b83ccd9317094061c1e5a1410e1aa53e2bcc Mon Sep 17 00:00:00 2001 From: Maryna Malakhova Date: Wed, 10 Mar 2021 18:08:53 +0200 Subject: [PATCH 4/7] 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 | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/scissors/scissors_game/main.c b/scissors/scissors_game/main.c index ee51dae..5d8b925 100644 --- a/scissors/scissors_game/main.c +++ b/scissors/scissors_game/main.c @@ -1,5 +1,7 @@ #include #include +#include +#include void static char_to_text(char c, char *res, int size) { @@ -20,6 +22,7 @@ void static char_to_text(char c, char *res, int size) int main(void) { + srand(time(NULL)); while(1){ printf("Please choose: rock (r) - paper (p) - scissors (s)\n"); char input; @@ -27,6 +30,12 @@ int main(void) memset (res, 0, 10); scanf ("%1s", &input); char_to_text(input, res, sizeof(res)-1); - printf("You choose %s and it's ok!\n", res); - } + static char *ChNames[] = { + "rock", + "paper", + "scissors" + }; + int comp=rand()%3; + printf("You choose %s, I choose %s\n", res, ChNames[comp]); + } } From a2a404c5ec169fdb52b2e799bdf592bc1ba81af4 Mon Sep 17 00:00:00 2001 From: Maryna Malakhova Date: Wed, 10 Mar 2021 21:07:33 +0200 Subject: [PATCH 5/7] SCISSORS: Add game and Finish task Add game_table in order to get winner. Add winner print Signed-off-by: Maryna Malakhova --- scissors/scissors_game/main.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/scissors/scissors_game/main.c b/scissors/scissors_game/main.c index 5d8b925..5deca08 100644 --- a/scissors/scissors_game/main.c +++ b/scissors/scissors_game/main.c @@ -3,21 +3,36 @@ #include #include -void static char_to_text(char c, char *res, int size) +//"-1" - draw +// "0" - fail +// "1" - win + +const int game_table[3][3]={ + -1, 0, 1, + 1, -1, 0, + 0, 1, -1 +}; + +int static char_to_text(char c, char *res, int size) { + int ans = -1; switch(c) { case 'r': strncpy(res, "rock", size); + ans=0; break; case 'p': strncpy(res, "paper", size); + ans=1; break; case 's': strncpy(res, "scissors", size); + ans=2; break; default: strncpy(res, "unknown", size); } + return ans; } int main(void) @@ -29,7 +44,8 @@ int main(void) char res [10]; memset (res, 0, 10); scanf ("%1s", &input); - char_to_text(input, res, sizeof(res)-1); + int user = -1; + user = char_to_text(input, res, sizeof(res)-1); static char *ChNames[] = { "rock", "paper", @@ -37,5 +53,18 @@ int main(void) }; int comp=rand()%3; printf("You choose %s, I choose %s\n", res, ChNames[comp]); + if (user==-1) + continue; + int check = game_table[user][comp]; + switch (check) { + case 0: + printf("I win: %s beats %s\n", ChNames[comp], res); + break; + case 1: + printf("You win: %s beats %s\n", res, ChNames[comp]); + break; + case -1: + printf("It's draw\n"); } + } } From 6ba12d0e0d1ab51dbbf5d80698b0f1b210ead9cf Mon Sep 17 00:00:00 2001 From: Maryna Malakhova Date: Thu, 11 Mar 2021 19:52:20 +0200 Subject: [PATCH 6/7] SCISSORS: Change numbers to enums Change numbers to enums for Game_table and Choise definition Signed-off-by: Maryna Malakhova --- scissors/scissors_game/main.c | 39 ++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/scissors/scissors_game/main.c b/scissors/scissors_game/main.c index 5deca08..41472c5 100644 --- a/scissors/scissors_game/main.c +++ b/scissors/scissors_game/main.c @@ -3,31 +3,42 @@ #include #include -//"-1" - draw -// "0" - fail -// "1" - win +typedef enum { + eDRAW = -1, + eLOST, + eWINN +} decision_t; + +/*Enum of Unknown, Rock, Paper and Scissors choise*/ +typedef enum { + eU = -1, + eR, + eP, + eS + +} element_t; const int game_table[3][3]={ - -1, 0, 1, - 1, -1, 0, - 0, 1, -1 + eDRAW, eLOST, eWINN, + eWINN, eDRAW, eLOST, + eLOST, eWINN, eDRAW }; int static char_to_text(char c, char *res, int size) { - int ans = -1; + int ans = eU; switch(c) { case 'r': strncpy(res, "rock", size); - ans=0; + ans=eR; break; case 'p': strncpy(res, "paper", size); - ans=1; + ans=eP; break; case 's': strncpy(res, "scissors", size); - ans=2; + ans=eS; break; default: strncpy(res, "unknown", size); @@ -53,17 +64,17 @@ int main(void) }; int comp=rand()%3; printf("You choose %s, I choose %s\n", res, ChNames[comp]); - if (user==-1) + if (user==eU) continue; int check = game_table[user][comp]; switch (check) { - case 0: + case eLOST: printf("I win: %s beats %s\n", ChNames[comp], res); break; - case 1: + case eWINN: printf("You win: %s beats %s\n", res, ChNames[comp]); break; - case -1: + case eDRAW: printf("It's draw\n"); } } From 214ccac8bbb056687e6529de37178655335b5299 Mon Sep 17 00:00:00 2001 From: Maryna Malakhova Date: Thu, 11 Mar 2021 20:06:12 +0200 Subject: [PATCH 7/7] SCISSORS: Move enums to Create header and move enums there Signed-off-by: Maryna Malakhova --- scissors/scissors_game/Makefile | 2 +- scissors/scissors_game/main.c | 22 +--------------------- scissors/scissors_game/scissors.h | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+), 22 deletions(-) create mode 100644 scissors/scissors_game/scissors.h diff --git a/scissors/scissors_game/Makefile b/scissors/scissors_game/Makefile index 45ec381..953fa4c 100644 --- a/scissors/scissors_game/Makefile +++ b/scissors/scissors_game/Makefile @@ -1,5 +1,5 @@ CC=gcc -SOURCES=main.c +SOURCES=main.c scissors.h EXECUTABLE=scissors_game all: $(SOURCES) diff --git a/scissors/scissors_game/main.c b/scissors/scissors_game/main.c index 41472c5..c7f27ba 100644 --- a/scissors/scissors_game/main.c +++ b/scissors/scissors_game/main.c @@ -2,27 +2,7 @@ #include #include #include - -typedef enum { - eDRAW = -1, - eLOST, - eWINN -} decision_t; - -/*Enum of Unknown, Rock, Paper and Scissors choise*/ -typedef enum { - eU = -1, - eR, - eP, - eS - -} element_t; - -const int game_table[3][3]={ - eDRAW, eLOST, eWINN, - eWINN, eDRAW, eLOST, - eLOST, eWINN, eDRAW -}; +#include "scissors.h" int static char_to_text(char c, char *res, int size) { diff --git a/scissors/scissors_game/scissors.h b/scissors/scissors_game/scissors.h new file mode 100644 index 0000000..b7bf902 --- /dev/null +++ b/scissors/scissors_game/scissors.h @@ -0,0 +1,19 @@ +/*Enum of Game results*/ +typedef enum { + eDRAW = -1, + eLOST, + eWINN +} decision_t; +/*Enum of Unknown, Rock, Paper and Scissors choise*/ +typedef enum { + eU = -1, + eR, + eP, + eS +} element_t; +const int game_table[3][3]={ + eDRAW, eLOST, eWINN, + eWINN, eDRAW, eLOST, + eLOST, eWINN, eDRAW +}; +