-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrockPaperScissors.c
More file actions
80 lines (74 loc) · 2.79 KB
/
rockPaperScissors.c
File metadata and controls
80 lines (74 loc) · 2.79 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
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
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
void rpsSelector(char compChoice[], char playerChoice[]);
int comparePlays(char compChoice[], char playerChoice[]);
int main(){
//GATHERING USER INPUTS...
char str1[4], playerChoice[9];
char compChoice[9];
int pScore = 0, cScore = 0;
printf("Welcome to Rock, Paper, Scissors \n Would you like to play? ");
scanf("%s", str1);
// END GATHERING USER INPUTS
while((strcmp(str1, "yes") == 0) && !(cScore == 3) && !(pScore == 3)) {
rpsSelector(compChoice, playerChoice);
int victor = comparePlays(compChoice, playerChoice);
if(victor == 0){
printf("There was a tie! The score is now you: %d, computer %d \n", pScore, cScore);
}
if(victor == 1){
pScore++;
printf("You won this game!\n The score is now you: %d, computer %d \n", pScore, cScore);
}
if(victor == -1){
cScore++;
printf("The computer won this game!\n The score is now you: %d, computer %d \n", pScore, cScore);
}
}
if(pScore == 3)
printf("You won Rock, Paper, Scissors. The score was you: %d, computer %d \n", pScore, cScore);
else if(cScore == 3)
printf("The computer won Rock, Paper, Scissors. The score was you: %d, computer %d \n", pScore, cScore);
return 0;
}
void rpsSelector(char compChoice[], char playerChoice[]){
printf("\nWhat is your choice? ");
scanf("%s", playerChoice);
srand((unsigned int)time(NULL)); //seed the rand num gen.
int compNum = rand() % (2-0 + 1);
switch(compNum){
case 0:
strcpy(compChoice, "rock");
break;
case 1:
strcpy(compChoice, "paper");
break;
case 2:
strcpy(compChoice, "scissors");
break;
default:
printf("error! invalid random number generated\n");
}
printf("The computer chooses %s\n", compChoice);
}
int comparePlays(char compChoice[], char playerChoice[]){
//if returns 1 player won, 0 tie, -1 comp won
if(strcmp(compChoice, playerChoice) == 0){ //tie
return 0;
}
else if((strcmp(compChoice,"rock") == 0) && (strcmp(playerChoice, "paper") == 0))
return 1;
else if((strcmp(compChoice,"rock") == 0) && (strcmp(playerChoice, "scissors") == 0))
return -1;
else if((strcmp(compChoice,"paper") == 0) && (strcmp(playerChoice, "rock") == 0))
return -1;
else if((strcmp(compChoice,"paper") == 0) && (strcmp(playerChoice, "scissors") == 0))
return 1;
else if((strcmp(compChoice,"scissors") == 0) && (strcmp(playerChoice, "paper") == 0))
return -1;
else if((strcmp(compChoice,"scissors") == 0) && (strcmp(playerChoice, "rock") == 0))
return 1;
return 10;
}