-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrps.c
More file actions
43 lines (41 loc) · 1.29 KB
/
rps.c
File metadata and controls
43 lines (41 loc) · 1.29 KB
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
#include <stdio.h>
#include <math.h>
#include <time.h>
#define R "ROCK"
#define P "PAPER"
#define S "SCISSORS"
void gameLogic(char *pm, char *bm){
printf("You guessed: %s\nBot guessed: %s\n", pm, bm);
if((strcmp(pm, R) == 0 && strcmp(bm, S) == 0) ||
(strcmp(pm, S) == 0 && strcmp(bm, P) == 0) ||
(strcmp(pm, P) == 0 && strcmp(bm, R) == 0))
printf("You won!\n");
else if(!strcmp(pm, bm)) //means it is 0, so theyre equal
printf("You tied.\n");
else
printf("You lost.\n");
return;
}
int main(void){
srand(time(NULL) ^ getpid()); //seed random
printf("lets play rock paper scissors.\n");
char *pm = malloc(10); //player move
while(1){
printf("Enter %s (R) %s (P) %s (S): ", R, P, S);
scanf(" %1s", pm); //scan single char to convert
getchar(); // consume newline character
if((strcmp(pm, "R") && strcmp(pm, "P") && strcmp(pm, "S")))
continue; //retry until input valid
if(strcmp(pm, "R"))
strcpy(pm, R);
else if(strcmp(pm, "P"))
strcpy(pm, P);
else
strcpy(pm, S);
char choices[3][10] = {R, P, S};
char *bm = &choices[rand() % 3]; //bot move takes from arr idx 0-2
gameLogic(pm, bm);
break;
}
return 0;
}