Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions scissors/scissors_game/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CC=gcc
SOURCES=main.c scissors.c
EXECUTABLE=scissors_game

all: $(SOURCES)
$(CC) $(SOURCES) -o $(EXECUTABLE)
42 changes: 42 additions & 0 deletions scissors/scissors_game/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// SPDX-License-Identifier: GPL-2.0
#include <stdio.h>
#include <string.h>
#include "scissors.h"

static const char * const Game_Elements[] = {
"rock", "paper", "scissors", "unknown"};

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) {
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]);
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;
}
Comment thread
AleksandrBulyshchenko marked this conversation as resolved.
} while (1);
}

50 changes: 50 additions & 0 deletions scissors/scissors_game/scissors.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-License-Identifier: GPL-2.0
#include <stdlib.h>
#include <time.h>
#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;

switch (c) {
case 'r':
ans = ROCK;
break;
case 'p':
ans = PAPER;
break;
case 's':
ans = SCISSORS;
break;
}
return ans;
}

void init_scissors(void)
{
srand(time(NULL));
}

int play_scissors(void)
{
return (rand()%3);
}

int get_game_result(int user, int comp)
{
return game_table[user][comp];
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitions order in source file and declarations order in header should be aligned.

14 changes: 14 additions & 0 deletions scissors/scissors_game/scissors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: GPL-2.0

#define ROCK 0
#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);