-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRock Paper Scissors.cpp
296 lines (260 loc) · 9.03 KB
/
Rock Paper Scissors.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
Rock Paper Sicssor Game
- Ascii Art Hands
- Default Rounds 4
- Computer Player
Creator : Nisheet Patel
Github : https://github.com/NisheetNakrani
*/
#include <iostream>
#include <conio.h>
#include <map>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
// Change game rounds
#define ROUND 4
class Player
{
private:
map<int, char> nchoice = {{1, 'R'}, {2, 'P'}, {3, 'S'}};
public:
char option;
string Name;
int score = 0;
// Update Name
Player(string name) : Name(name){};
// Update User Input Choice
void choice(int i)
{
option = nchoice.at(i);
}
// Check if this->Player wins
int iswin(Player &P)
{
// true = 1, false = 0, tie = 2
if (option == P.option)
return 2;
if (option == 'R' && P.option == 'S')
return 1;
else if (option == 'P' && P.option == 'R')
return 1;
else if (option == 'S' && P.option == 'P')
return 1;
return 0;
}
// return Random number 1,2,3
int rand_choice()
{
srand(time(0));
return 1 + rand() % 3;
}
// increment score
void update_score()
{
score++;
}
};
void Logo();
void show_hand(int, int);
void display(string, string, int, int, int, int, int);
int main()
{
/*
| p1h = User Choice
| p2h = Computer Choice
|
'-> Use for show_hand();
*/
int User, win, p1h = 0, p2h = 0, round = 1;
string name, YN;
// Get Name
cout << "Enter Name : ";
cin >> name;
// Two Players
Player p1(name), p2("Computer");
// Total 4 Rounds
while (round <= ROUND)
{
display(p1.Name, p2.Name, round, p1h, p2h, p1.score, p2.score);
// input guide
cout << "[ 1 ] Rock -- [ 2 ] Paper - [ 3 ] Sicssor\n?";
try
{
cin >> User; // User Input
if (User > 3)
throw(400);
p1h = User;
p1.choice(User); // User Choice
p2h = p2.rand_choice();
p2.choice(p2h); // Computer Choice
win = p1.iswin(p2);
display(p1.Name, p2.Name, round, p1h, p2h, p1.score, p2.score);
// print current choice
printf("Status : %c - %c\n", p1.option, p2.option);
if (win == 1)
{
cout << p1.Name << " Wins" << endl;
p1.update_score();
}
else if (win == 0)
{
cout << p2.Name << " Wins" << endl;
p2.update_score();
}
else
cout << "~ Tie ~" << endl;
getch();
round++;
}
catch (int e)
{
// [ 400 ] Invalid Input
if (e == 400)
{
cout << "\nInvalid Input plz Select from [ 1,2,3 ]\nEnter to play: ";
getch();
}
}
}
// Show Final Winner
if (p1.score > p2.score)
{
system("cls");
Logo();
cout << "\n You Win " << p1.Name << endl;
}
else if (p1.score < p2.score)
{
system("cls");
Logo();
cout << "\n"
<< p2.Name << " Wins\nBetter Luck Next Time :)" << endl;
}
else
{
system("cls");
Logo();
cout << "\n\n-+-+- TIE -+-+-";
}
PA: // If invalid input
cout << "\n\nDo you want to play again [y/n] ?";
cin >> YN;
if (YN == "y")
main();
else if (YN == "n")
exit(1);
else
goto PA;
return 0;
}
// Display Basic & Necessary Stuff
// Include : [ Logo, current round, score board, sign Hand ]
void display(string p1name, string p2name, int r, int p1, int p2, int s1, int s2)
{
system("cls"); // clear screen
// Logo
Logo();
// Current Round
printf("\n\t\t-+-+- Round %d -+-+-\n\n", r);
// Score Board
cout << "\t [ " << s1 << " ] " << p1name << " -- [ " << s2 << " ] " << p2name << " \n\n";
// Sign Hand
show_hand(p1, p2);
cout << "You\n\n";
}
void Logo()
{
// Logo
cout << "\t ______ ______ _____ " << endl;
cout << "\t | ___ \\ | ___ \\ / ___|" << endl;
cout << "\t | |_/ / | |_/ / \\ `--. " << endl;
cout << "\t | / | __/ `--. \\" << endl;
cout << "\t | |\\ \\ | | /\\__/ /" << endl;
cout << "\t \\_| \\_| \\_| \\____/ " << endl;
}
// Display Sign hand as per choice
void show_hand(int a, int b)
{
// R=1, P=2, S=3
switch (a * 10 + b)
{
case 11:
cout << " _______ _______ " << endl;
cout << "----' ____) (____ '---" << endl;
cout << " (_____) (_____) " << endl;
cout << " (_____) (_____) " << endl;
cout << " (____) (____) " << endl;
cout << "----.__(___) (___)__.---" << endl;
break;
case 12:
cout << " _______ ________ " << endl;
cout << "----' ____) ____(____ '---" << endl;
cout << " (_____) (______ " << endl;
cout << " (_____) (_______ " << endl;
cout << " (____) (_______ " << endl;
cout << "----.__(___) (__________.---" << endl;
break;
case 13:
cout << " _______ _______ " << endl;
cout << "----' ____) _____(____ '---" << endl;
cout << " (_____) (_______ " << endl;
cout << " (_____) (__________ " << endl;
cout << " (____) (____) " << endl;
cout << "----.__(___) (___)__.---" << endl;
break;
case 21:
cout << " _______ _______ " << endl;
cout << "----' ____)____ (____ '---" << endl;
cout << " ______) (_____) " << endl;
cout << " _______) (_____) " << endl;
cout << " _______) (____) " << endl;
cout << "----.__________) (___)__.---" << endl;
break;
case 22:
cout << " _______ ________ " << endl;
cout << "----' ____)____ ____(____ '---" << endl;
cout << " ______) (______ " << endl;
cout << " _______) (_______ " << endl;
cout << " _______) (_______ " << endl;
cout << "----.__________) (__________.---" << endl;
break;
case 23:
cout << " _______ _______ " << endl;
cout << "----' ____)____ _____(____ '---" << endl;
cout << " ______) (_______ " << endl;
cout << " _______) (__________ " << endl;
cout << " _______) (____) " << endl;
cout << "----.__________) (___)__.---" << endl;
break;
case 31:
cout << " _______ _______ " << endl;
cout << "----' ____)____ (____ '---" << endl;
cout << " ______) (_____) " << endl;
cout << " __________) (_____) " << endl;
cout << " (____) (____) " << endl;
cout << "----.__(___) (___)__.---" << endl;
break;
case 32:
cout << " _______ ________ " << endl;
cout << "----' ____)____ ____(____ '---" << endl;
cout << " ______) (______ " << endl;
cout << " __________) (_______ " << endl;
cout << " (____) (_______ " << endl;
cout << "----.__(___) (__________.---" << endl;
break;
case 33:
cout << " _______ _______ " << endl;
cout << "----' ____)____ _____(____ '---" << endl;
cout << " ______) (_______ " << endl;
cout << " __________) (__________ " << endl;
cout << " (____) (____) " << endl;
cout << "----.__(___) (___)__.---" << endl;
break;
default:
cout << "\n\n\n\n\n\n"
<< endl;
break;
}
}