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. diff --git a/scissors/scissors_game/Makefile b/scissors/scissors_game/Makefile new file mode 100644 index 0000000..953fa4c --- /dev/null +++ b/scissors/scissors_game/Makefile @@ -0,0 +1,6 @@ +CC=gcc +SOURCES=main.c scissors.h +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..c7f27ba --- /dev/null +++ b/scissors/scissors_game/main.c @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include "scissors.h" + +int static char_to_text(char c, char *res, int size) +{ + int ans = eU; + switch(c) { + case 'r': + strncpy(res, "rock", size); + ans=eR; + break; + case 'p': + strncpy(res, "paper", size); + ans=eP; + break; + case 's': + strncpy(res, "scissors", size); + ans=eS; + break; + default: + strncpy(res, "unknown", size); + } + return ans; +} + +int main(void) +{ + srand(time(NULL)); + while(1){ + printf("Please choose: rock (r) - paper (p) - scissors (s)\n"); + char input; + char res [10]; + memset (res, 0, 10); + scanf ("%1s", &input); + int user = -1; + user = char_to_text(input, res, sizeof(res)-1); + static char *ChNames[] = { + "rock", + "paper", + "scissors" + }; + int comp=rand()%3; + printf("You choose %s, I choose %s\n", res, ChNames[comp]); + if (user==eU) + continue; + int check = game_table[user][comp]; + switch (check) { + case eLOST: + printf("I win: %s beats %s\n", ChNames[comp], res); + break; + case eWINN: + printf("You win: %s beats %s\n", res, ChNames[comp]); + break; + case eDRAW: + printf("It's draw\n"); + } + } +} 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 +}; +