-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrockpaperscissors.cpp
More file actions
80 lines (76 loc) · 2.11 KB
/
Copy pathrockpaperscissors.cpp
File metadata and controls
80 lines (76 loc) · 2.11 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<iostream>
#include<ctime>
using namespace std;
char userChoice();
char computerChoice();
void choice(char choice);
void winner(char userchoice, char computerchoice);
int main () {
cout<< "******** Rock-Paper-Scissors Game ********\n\n";
char userchoice = userChoice();
cout<< "Your choice is ";
choice(userchoice);
char computerchoice = computerChoice();
cout<< "Computer choice is ";
choice(computerchoice);
winner(userchoice, computerchoice);
cout<< "******************************************\n";
return 0;
}
char userChoice() {
char option;
do {
cout<< "Enter Rock(r), Paper(p) or Scissors(s): ";
cin>> option;
cout<<'\n';
}while ( option != 'r' && option != 's' && option != 'p');
return option;
}
char computerChoice() {
srand(time(NULL));
int choice = rand()%3+1;
switch (choice) {
case 1: return 'r';
case 2: return 'p';
case 3: return 's';
}
return 'r';
}
void choice(char choice) {
switch(choice) {
case 'r': cout<< "Rock\n";
break;
case 'p': cout<< "Paper\n";
break;
case 's': cout<< "Scissors\n";
break;
}
}
void winner(char userchoice, char computerchoice) {
switch (userchoice) {
case 'r': if (computerchoice == 'r') {
cout<< "That's a tied!\n";
} else if (computerchoice == 's') {
cout<< "You won!\n";
} else {
cout<< "You lose!\n";
}
break;
case 's': if (computerchoice == 'r') {
cout<< "You lose!\n";
} else if (computerchoice == 's') {
cout<< "That's a tied!\n";
} else {
cout<< "You won!\n";
}
break;
case 'p': if (computerchoice == 'r') {
cout<< "You won!\n";
} else if (computerchoice == 's') {
cout<< "You lose!\n";
} else {
cout<< "That's a tied!\n";
}
break;
}
}