-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRPSgame.cpp
418 lines (370 loc) · 6.83 KB
/
RPSgame.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
Student Number: 100999036
Student Name: Austin Kirby
*/
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include <time.h>
using namespace std;
#pragma region SAVE class
/* Class SAVE
* name string used to store player name
* wins int used to store player wins
*/
class SAVE
{
public:
string name = "";
int wins;
} LOCAL_SAVE[12];
#pragma endregion
#pragma region Global Variables
/* Save file name
* SAVE_FILE ("save.txt")
*/
string SAVE_FILE = "save.txt";
/* Store current player variables
*/
string PLAYER_NAME;
int PLAYER_WINS;
#pragma endregion
#pragma region Method Declaration
/* Used to load values from save file.
* Called at start of program.
*/
void Init();
/* Used to control the four stages of game. [ Menu, Leaderboard, Game, Quit ]
* Called after the Init method.
*/
void GameLoop();
/* Used to write to save file.
* Called on quiting of the game.
*/
void Save();
/* Used to gather player's name.
* Called if there is no save file when Init method is ran.
*/
void GetPlayerName();
/* Used to sort the local save array of players.
* Called when the local save array is changed.
*/
void SortLocalSave();
/* Used to find current player's position in the save array.
*
*/
void GetPlayerSpot();
/* Used to show the player the local save array.
* Called when player enters 3 in menu method.
*/
void ShowSave();
/* Used to change the players name.
* Called when 2 is entered in Menu method.
*/
void ChangeName();
/* Used to save player to the local save array.
* Called when plaver is changed.
*/
void SavePlayer();
/* Used to play Rock Papers Sissors.
* Called in GameMenu and Menu.
*/
void Game();
/* Used to gather player input for playing the game again.
* Called in Menu method.
*/
void GameMenu();
/* Used to get player input on which game state they would like to be in. [ Menu, Leaderboard, Game, Quit ]
* Called in GameLoop.
*/
int Menu();
/* Used to retreive the length of the longest name in the local save array.
* Called in ShowLocalSave method.
*/
int GetLongestName();
#pragma endregion
int main()
{
// Seed the random
srand(time(NULL));
// Load from the save file
Init();
// Run main game loop
GameLoop();
// Save local array
Save();
system("PAUSE");
return 0;
}
#pragma region Prototype
void Init()
{
ifstream inFile;
string line;
string name;
int numWins;
int i = 0;
inFile.open(SAVE_FILE);
if (inFile.good())
{
while (getline(inFile, line))
{
istringstream iss(line);
iss >> name >> numWins;
if (i == 0)
{
PLAYER_NAME = name;
PLAYER_WINS = numWins;
}
LOCAL_SAVE[i].name = name;
LOCAL_SAVE[i].wins = numWins;
i++;
}
SortLocalSave();
}
else
{
ofstream file;
file.open(SAVE_FILE);
file.close();
GetPlayerName();
PLAYER_WINS = 0;
}
}
void GameLoop()
{
int choice;
do
{
choice = Menu();
switch (choice)
{
case 1:
Game();
GameMenu();
break;
case 2:
ChangeName();
break;
case 3:
ShowSave();
break;
}
} while (choice != 0);
}
void Save()
{
int i = 0;
ofstream file;
SortLocalSave();
file.open(SAVE_FILE);
for (i = 0; i < 11; i++)
{
if (LOCAL_SAVE[i].name != "")
file << LOCAL_SAVE[i].name << ' ' << LOCAL_SAVE[i].wins << endl;
}
file.close();
}
void GetPlayerName()
{
string player;
cout << "Please enter players name:" << endl << "==> ";
cin >> player;
PLAYER_NAME = player;
}
void SortLocalSave()
{
int i, tmpWins = -1;
for (i = 10; i > 0; i--)
{
if (LOCAL_SAVE[i].name != "")
{
if (tmpWins == -1)
tmpWins = LOCAL_SAVE[i].wins;
else if (LOCAL_SAVE[i].wins < tmpWins)
{
LOCAL_SAVE[11] = LOCAL_SAVE[i + 1];
LOCAL_SAVE[i + 1] = LOCAL_SAVE[i];
LOCAL_SAVE[i] = LOCAL_SAVE[11];
SortLocalSave();
}
}
}
}
void ShowSave()
{
int longestName = GetLongestName();
int i, z;
cout << endl << "Rank Player Name" << setw(abs(11 - longestName)) << " No Wins" << endl;
for (i = 0; i < 26; i++)
cout << '-';
cout << endl;
for (i = 1; i < 11; i++)
{
cout << i << " ";
if (i != 10)
cout << " ";
if (LOCAL_SAVE[i].name != "")
{
cout << LOCAL_SAVE[i].name << setw(14 - LOCAL_SAVE[i].name.length());
cout << LOCAL_SAVE[i].wins;
}
cout << endl;
}
cout << endl;
system("PAUSE");
}
void ChangeName()
{
string name = "";
int i;
do
{
cout << "Enter new name:" << endl << "==> ";
cin.clear();
fflush(stdin);
cin >> name;
} while (name == "");
SavePlayer();
PLAYER_NAME = name;
PLAYER_WINS = 0;
LOCAL_SAVE[0].name = name;
for (i = 1; i < 11; i++)
{
if (PLAYER_NAME == LOCAL_SAVE[i].name)
{
PLAYER_WINS = LOCAL_SAVE[i].wins;
}
}
LOCAL_SAVE[0].wins = PLAYER_WINS;
}
void SavePlayer()
{
int i;
bool found = false;
for (i = 1; i < 11; i++)
{
if (PLAYER_NAME == LOCAL_SAVE[i].name)
{
LOCAL_SAVE[i].wins = PLAYER_WINS;
found = true;
}
}
if (!found)
{
for (i = 1; i < 11; i++)
{
if (LOCAL_SAVE[i].name == "")
{
LOCAL_SAVE[i].name = PLAYER_NAME;
LOCAL_SAVE[i].wins = PLAYER_WINS;
found = true;
break;
}
}
if (!found && LOCAL_SAVE[10].wins < PLAYER_WINS)
{
LOCAL_SAVE[10].name = PLAYER_NAME;
LOCAL_SAVE[10].wins = PLAYER_WINS;
}
}
SortLocalSave();
}
void Game()
{
char s = 'a';
do
{
cout << endl << "Choose your weapon!" << endl;
cout << "r => Rock" << endl;
cout << "p => Paper" << endl;
cout << "s => Sissors" << endl;
cout << "==> ";
cin.clear();
fflush(stdin);
cin >> s;
} while (s != 'r' && s != 'p' && s != 's');
switch (rand() % 3 + 1)
{
case 3: // rock
if (s == 'p')
{
PLAYER_WINS++;
LOCAL_SAVE[0].wins++;
cout << "You won!" << endl;
SavePlayer();
}
else
cout << "You didn't win." << endl;
break;
case 2: // paper
if (s == 's')
{
PLAYER_WINS++;
LOCAL_SAVE[0].wins++;
cout << "You won!" << endl;
SavePlayer();
}
else
cout << "You didn't win." << endl;
break;
case 1: // sissor
if (s == 'r')
{
PLAYER_WINS++;
LOCAL_SAVE[0].wins++;
cout << "You won!" << endl;
SavePlayer();
}
else
cout << "You didn't win." << endl;
break;
}
}
void GameMenu()
{
char s = 'a';
do
{
cout << "Play again? [ y / n ]" << endl << "==> ";
cin.clear();
fflush(stdin);
cin >> s;
} while (s != 'y' && s != 'n');
if (s == 'y')
{
Game();
GameMenu();
}
}
int Menu()
{
int i = -1;
do
{
system("cls");
cout << "Current Player Name: <" << PLAYER_NAME << ">" << endl << endl;
cout << "1 => Start the game" << endl;
cout << "2 => Change the player name" << endl;
cout << "3 => Show highest scores" << endl;
cout << "0 => Exit" << endl << endl;
cout << "What is your option?" << endl;
cin.clear();
fflush(stdin);
cin >> i;
} while (i < 0 || i > 3);
return i;
}
int GetLongestName()
{
int i, tmp = 0;
for (i = 1; i < 11; i++)
if (LOCAL_SAVE[i].name != "")
if (LOCAL_SAVE[i].name.length() > tmp)
tmp = LOCAL_SAVE[i].name.length();
return tmp;
}
#pragma endregion