-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
468 lines (407 loc) · 13.9 KB
/
main.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
* File: main.cpp
* Author: James Hartley
* Created: January 14, 2019, 5:28 PM
* Purpose: Klondike, The classic Dice Game.
*/
#include <iostream>//IO Stream.
#include <iomanip> //IO Manipulation, for formatting
#include <fstream> //File Stream, for reading in the rules.
#include <cstdlib> //Random numbers
#include <ctime> //Time library.
#include <cstring> //memcpy
#include <vector> //using vectors instead of arrays for function types.
#include <string> //For the to_string function
#include <limits> //For checking user input
using namespace std;
//Structs
struct hand { char val; string desc; };
//Function Prototypes
string GetRules();
vector<char> RollDice();
hand CalcHand(string, vector<char>);
void PlayGame(string, float &, float &, vector<char> &, vector<char> &, hand &, hand &);
bool CheckForSave();
void LoadGame(string &, float &) ;
void SaveGame(string, float);
void DisplayDice(vector<char> &);
//Added for grading purposes
void misc(char[], char[]);
void misc(char [][5]);
//Main Function
int main(int argc, char** argv) {
//Set the random seed
srand(time(0));
//Variables
hand bHand, //Banker Hand
pHand; //Player Hand
char menuSel; //Menu Selection
bool play; //Are we currently playing the game?
string name; //Players name
float money, //Amount of money the player has.
betAmt; //Amount of bet.
vector<char> pDie, //Player Dice.
bDie; //Banker Dice.
//Initialize
play = false;
money = 1000;
//Intro
cout << "*******************************" << endl;
cout << "**** Klondike ****" << endl;
cout << "**** The Dice Game ****" << endl;
cout << "**** By ****" << endl;
cout << "**** James Hartley ****" << endl;
cout << "*******************************" << endl;
cout << " ------------- " << endl;
//Display Menu
do {
cout << setfill('.');
cout << "P" << setw(30) << "Play" << endl;
cout << "R" << setw(30) << "Rules" << endl;
cout << "E" << setw(30) << "Exit" << endl;
cout << "Please input a selection..." << endl;
cin >> menuSel;
//Check menu selection
switch (toupper(menuSel)) {
case 'P':
play = true;
cout << fixed << setprecision(2);
//Check if we have a valid save file
if (CheckForSave()) {
LoadGame(name, money) ;
}
else {
//Get player name and create save file.
cout << "Please enter your name: ";
cin >> name;
SaveGame(name, money);
}
while (play)
{
//Play the game
PlayGame(name, money, betAmt, bDie, pDie, pHand, bHand);
//Ask to play again
if (money > 0) {
cout << endl << "Play Again? (Y or N) ";
}
else {
cout << endl << "Game Over! You ran out of money.";
cout << endl << "Would you like to restart?";
}
cin >> menuSel;
if (toupper(menuSel) == 'N') {
play = false;
SaveGame(name, money);
}
else if (money <= 0) {
money = 1000;
}
}
break;
case 'R':
//Display Rules
cout << GetRules() << endl;
break;
case 'E':
//Exit
cout << "Goodbye!";
break;
default:
//Exit
cout << "Goodbye!";
break;
}
}while (toupper(menuSel) == 'R');
//Exit
exit(EXIT_SUCCESS);
return 0;
}
hand CalcHand(string name, vector<char> dice)
{
hand result; //Resulting hand
char value1, value2, pairs1, pairs2;
value1 = value2 = pairs1 = pairs2 = 0;
//Check for pairs for the banker
//This gruesome piece of code loops through the dice array to find multiples and stores their value for scoring.
for (int i = 0; i < 5; i++) {
for (int j = i + 1; j < 5; j++) {
if (dice[i] == dice[j]) {
//We got a match
if (value1 == 0 && value2 == 0) {
//This is the first pair found.
pairs1 = 1;
value1 = dice[i];
pairs1++;
dice[i] = -1; //Counted.
} else if (value1 == dice[i]) {
//Same value of die
pairs1++;
dice[i] = -1; //Counted.
} else if (value1 != 0 && value2 == 0) {
//Storing value 2 of die
value2 = dice[i];
pairs2 = 1;
pairs2++;
dice[i] = -1; //Counted.
} else if (value1 != 0 && value2 == dice[i]) {
//Storing value 2 of die if multiples.
pairs2++;
dice[i] = -1; //Counted.
}
}
}
}
if (pairs1 == 5 || pairs2 == 5) { //FIVE OF A KIND WORTH 60 POINTS
//Set description
result.desc = "FIVE OF A KIND!";
//Set hand worth
result.val = 60 + value1 + value2;
} else if (pairs1 == 4 || pairs2 == 4) { //FOUR OF A KIND WORTH 50 POINTS
//Display output
result.desc.append(name);
result.desc.append(" has: ");
result.desc.append(to_string(pairs1));
result.desc.append(" ");
result.desc.append(to_string(value1));
result.desc.append("'s.");
result.desc.append("\nFOUR OF A KIND!\n");
//Set hand worth
result.val = 50 + value1 + value2;
//1's are greater value
if (value1 == 1 || value2 == 1) {
result.val += 5;
}
} else if (pairs1 == 3 && pairs2 == 2 || pairs2 == 3 && pairs1 == 2) { //FULL HOUSE WORTH 40 POINTS
//Display output
result.desc.append(name);
result.desc.append(" has: ");
result.desc.append(to_string(pairs1));
result.desc.append(" ");
result.desc.append(to_string(value1));
result.desc.append("'s. and ");
result.desc.append(to_string(pairs2));
result.desc.append(" ");
result.desc.append(to_string(value2));
result.desc.append("'s.");
result.desc.append("\nFULL HOUSE!\n");
//Set hand worth
result.val = 40 + value1 + value2;
} else if (pairs1 == 3 || pairs2 == 3) { //THREE OF A KIND WORTH 30 POINTS
//Display output
result.desc.append(name);
result.desc.append(" has: ");
result.desc.append(to_string(pairs1));
result.desc.append(" ");
result.desc.append(to_string(value1));
result.desc.append("'s.");
result.desc.append("\nTHREE OF A KIND!\n");
//Set hand worth
result.val = 30 + value1 + value2;
//1's are greater value
if (value1 == 1 || value2 == 1) {
result.val += 5;
}
} else if (pairs1 == 2 && pairs2 == 2) { //TWO PAIR WORTH 20 POINTS
//Display output
result.desc.append(name);
result.desc.append(" has: ");
result.desc.append(to_string(pairs1));
result.desc.append(" ");
result.desc.append(to_string(value1));
result.desc.append("'s. and ");
result.desc.append(to_string(pairs2));
result.desc.append(" ");
result.desc.append(to_string(value2));
result.desc.append("'s.");
result.desc.append("\nTWO PAIRS!\n");
//Set hand worth
result.val = 20 + value1 + value2;
if (value1 == 1 || value2 == 1) {
result.val += 5;
}
} else if (pairs1 > 1 || pairs2 > 1) { //ONE PAIR WORTH 10 POINTS
//Display output
//Display output
result.desc.append(name);
result.desc.append(" has: ");
result.desc.append(to_string(pairs1));
result.desc.append(" ");
result.desc.append(to_string(value1));
result.desc.append("'s.");
result.desc.append("\nONE PAIR!\n");
//Set hand worth
result.val = 10 + value1 + value2;
if (value1 == 1 || value2 == 1) {
result.val += 5;
}
} else {
result.desc = name + " has no pairs.";
//Set hand worth
result.val = 0;
}
return result;
}
void PlayGame( string name, float &money, float &betAmt, vector<char> &bDie,
vector<char> &pDie, hand &pHand, hand &bHand )
{
//Play the game
cout << " ------------- " << endl;
cout << "Your Money: $" << money << endl;
cout << "Enter your bet: $";
cin >> betAmt;
//Check for correct value
while(!cin || betAmt <= 0)
{
// user didn't input a number
cin.clear(); // reset failbit
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //skip bad input
// next, request user reinput
cout << "You must enter a positive number for your bet" << endl;
cout << "Enter your bet: $";
cin >> betAmt;
}
cout << "Banker Rolls" << endl;
//Roll the dice for the banker
bDie = RollDice();
//Sort and Display the dice
DisplayDice(bDie);
//Calculate the banker hand value
bHand = CalcHand("Banker", bDie);
//Display hand description for the banker
cout << endl << bHand.desc << endl;
//Players Roll ---------------
cout << name << " Rolls" << endl;
//Roll the dice for the player
pDie = RollDice();
//Sort and Display the dice
DisplayDice(pDie);
//Calculate player hand value
pHand = CalcHand(name, pDie);
//Display hand description
cout << endl << pHand.desc << endl;
//Determine winner
if (pHand.val > bHand.val) {
cout << name << " Wins!";
money += (betAmt*2);
}
else if (pHand.val == bHand.val) {
cout << "It's a Tie!";
}
else {
cout << "Banker Wins.";
money -= betAmt;
}
}
vector<char> RollDice()
{
//This rolls the dice and returns a vector containing the roll
vector<char> result; //Resulting vector.
for (int i = 0; i < 5; i++) {
result.push_back(rand()%6+1);
}
return result;
}
void DisplayDice(vector<char> &dice) {
//Marksort
for (int i = 0; i < 5; i++) {
for (int j = i+1; j < 5; j++) {
if (dice[j] < dice[i]) {
//Swap
char tmp = dice[i];
dice[i] = dice[j];
dice[j] = tmp;
}
}
}
//Display dice roll for the banker
for (int i = 0; i < 5; i++) {
cout << static_cast<short> (dice[i]) << " ";
}
}
void SaveGame(string name, float money)
{
ofstream ofs; //Output File stream.
//Open the file stream
ofs.open("save.dat");
//Save Player name and money
ofs << 1 << '\n' << name << '\n' << money;
//close the file stream
ofs.close();
}
bool CheckForSave() {
ifstream ifs; //Input File stream.
string tmpSave;
bool save; //0/false for new game, 1/true for saved game.
//Open the file stream
ifs.open("save.dat");
//Check line 1
getline(ifs, tmpSave);
save = stoi(tmpSave);
//close the file stream
ifs.close();
//If line 1 != 0 then found save
if (save != 0) {
cout << "----Found Save!---";
return true;
}
else {
return false;
}
}
void LoadGame(string &name, float &money)
{
ifstream ifs; //Input File stream.
string saveSt;
string tmpName;
string tmpMon;
//Open the file stream
ifs.open("save.dat");
//Get player name from line 1 and player money from line 2
getline(ifs, saveSt);
getline(ifs, tmpName);
getline(ifs, tmpMon);
//Assign the values to the vars
name = tmpName;
money = atof(tmpMon.c_str());
//close the file stream
ifs.close();
}
string GetRules()
{
ifstream ifs; //Input File stream.
string temp; //Temporary string for building result
string result = ""; //Resulting string
//Open the file stream
ifs.open("rules.dat");
//Loop through and add the text into the string.
while (!ifs.eof()) {
getline(ifs, temp);
result += temp;
result += '\n';
}
//close the file stream
ifs.close();
//return the rules string
return result;
}
void misc(char a[5], char b[5])
{
//Fill array with random numbers for grade points
for (int i = 0; i < 5; i++)
{
static int count = 0; //Variable stays in static storage area
a[i] = rand()%90+1;
b[i] = i; // Parallel array.
}
}
void misc(char a[5][5])
{
//Fill array with random numbers for grade points
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
a[i][j] = rand()%90+1;
}
}
}