-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPROJECT.cpp
299 lines (211 loc) · 6.52 KB
/
PROJECT.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
297
298
299
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
enum enGameChoice { Stone = 1 , Paper = 2 , Scissors = 3 };
enum enWinner { Player1 = 1 , Computer = 2 , Draw = 3 };
struct stRoundInfo
{
short RoundNumber = 0;
enGameChoice PlayerChoice;
enGameChoice ComputerChoice;
enWinner Winner;
string WinnerName;
};
struct stGameResults
{
short GameRounds = 0;
short Player1WinTimes = 0;
short Computer2WinTimes =0;
short DrawTimes = 0;
enWinner GameWinner;
string WinnerName = "";
};
int RandomNumber(int From, int To)
{
int RandNum = rand() % (To - From + 1) + From;
return RandNum;
}
string WinnerName(enWinner Winner)
{
string arrWinnerName[3] = { "Player1","Computer","Draw" };
return arrWinnerName[Winner - 1];
}
enWinner WhoWonTheRound(stRoundInfo RoundInfo)
{
if (RoundInfo.PlayerChoice == RoundInfo.ComputerChoice)
{
return enWinner::Draw;
}
switch (RoundInfo.PlayerChoice)
{
case enGameChoice::Stone:
if (RoundInfo.ComputerChoice == enGameChoice::Paper)
{
return enWinner::Computer;
}
break;
case enGameChoice::Paper:
if (RoundInfo.ComputerChoice == enGameChoice::Scissors)
{
return enWinner::Computer;
}
break;
case enGameChoice::Scissors:
if (RoundInfo.ComputerChoice == enGameChoice::Stone)
{
return enWinner::Computer;
}
break;
}
return enWinner::Player1;
}
string ChoiceName(enGameChoice Choice)
{
string arrGameChoice[3] = { "Stone","Paper","Scissors" };
return arrGameChoice[Choice - 1];
}
void SetWinnerScreenColor(enWinner Winner)
{
switch (Winner)
{
case enWinner::Player1:
system("color 2F");
break;
case enWinner::Computer:
system("color 4F");
cout << "\a";
break;
default:
system("color 6F");
break;
}
}
void PrintRoundResults(stRoundInfo RoundInfo)
{
cout << "\n--------Round [" << RoundInfo.RoundNumber << "]--------\n\n";
cout << "Player1 Choice : " << ChoiceName(RoundInfo.PlayerChoice) << "\n";
cout << "Computer Choice : " << ChoiceName(RoundInfo.ComputerChoice) << "\n";
cout << "Round Winner : [" << RoundInfo.WinnerName << "]\n";
cout << "-----------------------------------------------------------\n\n";
SetWinnerScreenColor(RoundInfo.Winner);
}
enWinner WhoWonTheGame(short PlayerTimes, short ComputerTimes)
{
if (PlayerTimes > ComputerTimes)
return enWinner::Player1;
else if (ComputerTimes > PlayerTimes)
return enWinner::Computer;
else
return enWinner::Draw;
}
stGameResults FillGameResults(int GameRound, short Player1WinTimes,
short ComputerWinTimes, short DrawTimes)
{
stGameResults GameResults;
GameResults.GameRounds = GameRound;
GameResults.Player1WinTimes = Player1WinTimes;
GameResults.Computer2WinTimes = ComputerWinTimes;
GameResults.DrawTimes = DrawTimes;
GameResults.GameWinner = WhoWonTheGame(Player1WinTimes, ComputerWinTimes);
GameResults.WinnerName = WinnerName(GameResults.GameWinner);
return GameResults;
}
enGameChoice ReadPlayer1Choice()
{
short Choice = 1;
do
{
cout << "\n Your choice : [1]:Stone, [2]:Paper, [3]:Scissors ? ";
cin >> Choice;
} while (Choice < 1 || Choice > 3);
return (enGameChoice)Choice;
}
enGameChoice GetcomputerChoice()
{
return (enGameChoice)RandomNumber(1, 3);
}
stGameResults PlayGame(short HowManyRound)
{
stRoundInfo RoundInfo;
short Player1WinTimes = 0, ComputerwinTimes = 0, DrawTimes = 0;
for (short GameRound = 1; GameRound <= HowManyRound; GameRound++)
{
cout << "\nRound [" << GameRound << "] begines :\n";
RoundInfo.RoundNumber = GameRound;
RoundInfo.PlayerChoice = ReadPlayer1Choice();
RoundInfo.ComputerChoice = GetcomputerChoice();
RoundInfo.Winner = WhoWonTheRound(RoundInfo);
RoundInfo.WinnerName = WinnerName(RoundInfo.Winner);
if (RoundInfo.Winner == enWinner::Player1)
Player1WinTimes++;
else if (RoundInfo.Winner == enWinner::Computer)
ComputerwinTimes++;
else
DrawTimes++;
PrintRoundResults(RoundInfo);
}
return FillGameResults(HowManyRound, Player1WinTimes,
ComputerwinTimes, DrawTimes);
}
string Tabs(short Numberoftabs)
{
string t = "";
for (short i = 1; i < Numberoftabs; i++)
{
t = t + "\t";
cout << t;
}
return t;
}
void ShowGameOverScreen()
{
cout << Tabs(2) <<"__________________________________________________________\n\n";
cout << Tabs(2) <<" +++ G a m e O v e r +++ \n";
cout << Tabs(2) <<"__________________________________________________________\n\n";
}
void ShowFinalGameResults(stGameResults GameResults)
{
cout << Tabs(2) << "_____________________ [Game Results]_____________________\n\n";
cout << Tabs(2) << "Game Rounds : " << GameResults.GameRounds << endl;
cout << Tabs(2) << "Player1 won times : " << GameResults.Player1WinTimes << endl;
cout << Tabs(2) << "Computer won times : " << GameResults.Computer2WinTimes << endl;
cout << Tabs(2) << "Draw times : " << GameResults.DrawTimes << endl;
cout << Tabs(2) << "Final Winner : " << GameResults.WinnerName << endl;
cout << Tabs(2) << "___________________________________________________________\n";
SetWinnerScreenColor(GameResults.GameWinner);
}
short ReadHowManyRounds()
{
short GameRounds = 1;
do
{
cout << "How Many Rounds 1 To 10 ?\n";
cin >> GameRounds;
} while (GameRounds < 1 || GameRounds>10);
return GameRounds;
}
void ResetScreen()
{
system("cls");
system("color 0F");
}
void StartGame()
{
char playAgain = 'Y';
do
{
ResetScreen();
stGameResults GameResults = PlayGame(ReadHowManyRounds());
ShowGameOverScreen();
ShowFinalGameResults(GameResults);
cout << "\n" << Tabs(3) << "Do You Want To Play Again ? Y/N?";
cin >> playAgain;
} while (playAgain == 'Y' || playAgain == 'y');
}
int main()
{
srand((unsigned)time(NULL));
StartGame();
return 0;
}