-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
106 lines (85 loc) · 1.85 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef unsigned char uint8_t;
typedef struct Pick
{
uint8_t code;
// 0 - rock
// 1 - paper
// 2 - scissors
} Pick;
typedef struct Player
{
Pick pick;
} Player;
int get_valid_integer()
{
int value;
int result = scanf("%d", &value);
if (result == EOF)
{
printf("Nothing has been read\n");
return -1;
}
if (result == 0)
{
while (fgetc(stdin) != '\n')
;
return -1;
}
if (value < 0 || value > 2)
{
return -1;
}
return value;
}
Player create_player(uint8_t code)
{
Pick pick;
pick.code = code;
Player player;
player.pick = pick;
return player;
}
uint8_t random_pick(uint8_t lower, uint8_t upper)
{
srand(time(NULL));
int random = rand();
uint8_t pick = (random % (upper - lower + 1)) + lower;
return pick;
}
void show_winner(Player player1, Player player2, uint8_t upper)
{
uint8_t loses = (player1.pick.code + 1) % (upper + 1);
uint8_t wins = (player1.pick.code + 2) % (upper + 1);
uint8_t code = player2.pick.code;
printf("\n");
if (code == loses)
{
printf("Player 2 won!\n");
}
else if (code == wins)
{
printf("Player 1 won!\n");
}
else
{
printf("Tie!");
}
}
int main(void)
{
uint8_t lower = 0, upper = 2;
printf("Enter any integer between 0 and 2\n\n");
printf("0 = Rock\n1 = Paper\n2 = Scissors\n\n");
int value = get_valid_integer();
while (value == -1)
{
value = get_valid_integer();
}
Player player = create_player(value);
Player cpu = create_player(random_pick(lower, upper));
show_winner(player, cpu, upper);
return 0;
}