-
Notifications
You must be signed in to change notification settings - Fork 0
/
rps.cpp
112 lines (105 loc) · 2.19 KB
/
rps.cpp
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
112
// Rock, Paper, Scissors!
#include <iostream>
#include <cstdlib>
using namespace std;
int points;
//rock
int rock(int choice){
switch(choice){
case 1:
cout<<"I played rock. It's a tie!"<<endl;
points = points + 1;
break;
case 2:
cout<<"I played rock. Congratulations, you won! :D"<<endl;
points = points + 3;
break;
case 3:
cout<<"I played rock. Too bad, you lost :("<<endl;
points = points - 2;
break;
case 9:
break;
default:
cout<<"Your input was invalid. Try again."<<endl;
break;
}
return points;
}
//paper
int paper(int choice){
switch(choice){
case 1:
cout<<"I played paper. Too bad, you lost :("<<endl;
points = points - 2;
break;
case 2:
cout<<"I played paper. It's a tie!"<<endl;
points = points + 1;
break;
case 3:
cout<<"I played paper. Congratulations, you won! :D"<<endl;
points = points + 3;
break;
case 9:
break;
default:
cout<<"Your input was invalid. Try again."<<endl;
break;
}
return points;
}
//scissors
int scissors(int choice){
switch(choice){
case 1:
cout<<"I played scissors. Congratulations, you won! :D"<<endl;
points = points + 3;
break;
case 2:
cout<<"I played scissors. Too bad, you lost :("<<endl;
points = points - 2;
break;
case 3:
cout<<"I played scissors. It's a tie!"<<endl;
points = points + 1;
break;
case 9:
break;
default:
cout<<"Your input was invalid. Try again."<<endl;
break;
}
return points;
}
int main(){
int choice;
for(int turns=0; turns <=4; turns ++){ // counter-controlled loop
while (choice != 9){ // sentinel-controlled loop
cout<<"Rock Paper Scissors!"<<endl;
cout<<"1. Rock"<<endl;
cout<<"2. Paper"<<endl;
cout<<"3. Scissors"<<endl;
cout<<"Enter your option (or enter 9 to quit):\t";
cin>>choice;
int computer = 1 + (rand() % 4); // generate random number between 1 and 4
switch(computer){
case 1:
rock(choice);
break;
case 2:
paper(choice);
break;
case 3:
scissors(choice);
break;
default:
cout<<"I made a bad move, you\'ll get another turn"<<endl;
turns --;
break;
}
}
}
cout<<"Game Over! You have "<<points<<" points";
return 0;
}