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
107
108
109
110
111
|
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct Game {
int wins;
int losses;
int draws;
void (*conclusion)(struct Game *self, int decider, char *user_input);
void (*showScore)(struct Game *self);
};
void Conclusion(struct Game *self, int decider, char *user_input) {
if (decider >= 1 && decider <= 34) {
printf("Rock is picked by computer\n");
if (strcmp(user_input, "paper") == 0) {
printf("You win!\n");
self->wins++;
}
else if (strcmp(user_input, "scissor") == 0) {
printf("You lose!\n");
self->losses++;
}
else if (strcmp(user_input, "rock") == 0) {
printf("Draw\n");
self->draws++;
}
else {
printf("Invalid input\n");
}
}
else if (decider >= 35 && decider <= 66) {
printf("Paper is picked by computer\n");
if (strcmp(user_input, "paper") == 0) {
printf("Draw\n");
self->draws++;
}
else if (strcmp(user_input, "scissor") == 0) {
printf("You win!\n");
self->wins++;
}
else if (strcmp(user_input, "rock") == 0) {
printf("You lose\n");
self->losses++;
}
else {
printf("Invalid input\n");
}
}
else if (decider >= 67 && decider <= 100) {
printf("Scissor is picked by computer\n");
if (strcmp(user_input, "paper") == 0){
printf("You lose!\n");
self->losses++;
}
else if (strcmp(user_input, "scissor") == 0) {
printf("Draw\n");
self->draws++;
}
else if (strcmp(user_input, "rock") == 0) {
printf("You win!\n");
self->wins++;
}
else {
printf("Invalid input\n");
}
}
}
void Show_Score(struct Game *self) {
printf("Wins: %d | Losses: %d | Draws: %d\n", self->wins, self->losses, self->draws);
}
void Game_init(struct Game *game) {
game->conclusion = Conclusion;
game->wins = 0;
game->showScore = Show_Score;
game->losses = 0;
game->draws = 0;
}
int main()
{
struct Game mygame;
Game_init(&mygame);
srand(time(NULL));
char user_input[10];
printf("Pick Rock, Paper Or Scissor... (write Exit to quit, Score to see score)\n");
while (fgets(user_input, sizeof(user_input), stdin) != NULL) {
user_input[strcspn(user_input, "\n")] = '\0';
for (int i = 0;user_input[i] != '\0';i++) {
user_input[i] = tolower(user_input[i]);
}
if (strcmp(user_input, "exit") == 0) {
printf("Exited\n");
mygame.showScore(&mygame);
break;
}
else if (strcmp(user_input, "score") == 0) {
mygame.showScore(&mygame);
continue;
}
printf("YOU PICKED: %s\n", user_input);
mygame.conclusion(&mygame, rand() % 100, user_input);
}
return 0;
}
|