-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
495 lines (410 loc) · 11.8 KB
/
Copy pathmain.cpp
File metadata and controls
495 lines (410 loc) · 11.8 KB
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// File Name: A6russellr.cpp
// Written by: Robert Russell
// Description: Final Project, PowerBall. Generates 5 random numbers between 1 and 59 and 1 PowerBall between
// 1 and 35. Asks user to pick same format of numbers and matches based on the rules of the PowerBall lottery
// game.
// Challenges: Generating Random Numbers, Comparing two different sets of numbers with different rules.
// Time Spent: Approx 10 hours
// Given Input: (3 sets) Expected Output: (3 corresponding sets)
// -------------------- -------------------------
// 1) t , 1 2 3 4 5 6 Your Numbers: 1, 2, 3, 4, 5, PowerBall: 6
// PowerBall Numbers: ##, ##, ##, ##, ##, PowerBall: ##
// You need to match at least the powerball or three white balls to win.
// Revision History
// Date: Revised By: Action:
// ------------------------------------------------------------------
// 4/28 RSR Wrote inArray, printArray functions
// 4/30 RSR Added in function definitions, tested random numbers
// 5/4 RSR Finished.
// 5/6 RSR Fixed srand, added odds of winning, changed powerball to 1-35, fix modulus statement, fix calling GeneratePowerball() twice
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
#include <string.h>
using namespace std;
void displayMenu();
void back();
int randomNum(int, int);
void DisplayInstructions ( );
//(5 points)
//DisplayInstructions will display a description of the game, how to play, and how to win.
void GetPlayerSelections (int *);
//(15 points)
// GetPlayerSelections is passed an array. It will ask the player for input.
// Player selects 5 numbers from 1-59, and 1 number from 1-35 for the last number.
// Do not allow the player to enter duplicate numbers for the first 5 numbers. (Keep looping until they have selected 6 acceptable numbers.) Store the player’s selections in the array.
void GeneratePowerball (int *);
// (15 points)
// GeneratePowerball is passed an array.
// It randomly generates the 6 numbers according to the rules above and fills the array with these numbers.
// Make sure there are no duplicates in the first 5 numbers.
int CheckForWin (int *, int *, int);
// (30 points)
// CheckForWin is passed the two arrays.
// It displays the Player’s Selections and the Powerball Numbers.
// It returns an integer 1-9 that represents one of the winning combinations or returns 0 for no win.
// Remember that the numbers do not have to be in order to win.
void reset(int *);
//sets all items in array to 0
int DisplayPrize (int );
//(15 points)
// DisplayPrize takes an integer from 0-9 and displays whether it’s a win or a loss, what the prize is and what the odds are of winning that prize.
// It returns the amount won (0 if no win).
void printArr(int *arr, int elem);
//prints a passed array
void copyArray(int *array1, int *array2, int elem);
//copies first array into second arrray
//number of balls
const int NUMBALLZ = 6;
//debug global
int debug = 0;
//screen colors
const char green[] = "Color 27";
const char red[] = "Color 47";
const char l1[] = "Color 71";
const char l2[] = "Color 78";
int main() {
system(l2);
system("Color 34");
int playerBalance = 0;
int PlayerNumbers[] = {0,0,0,0,0,0};
int WinningNumbers[] = {0,0,0,0,0,0};
int *playerPB = &PlayerNumbers[5];
int *winningPB = &WinningNumbers[5];
int matches = 0;
srand(time(NULL));
displayMenu();
bool done = false;
while (done == false) {
char choice = 'k';
cin >> choice;
choice = toupper(choice);
switch (choice) {
case 'k':
cout << "Please choose a menu option.";
back();
break;
case 'I':
system(l2);
DisplayInstructions();
back();
break;
case 'T':
reset(PlayerNumbers);
reset(WinningNumbers);
playerBalance -= 2;
GeneratePowerball(WinningNumbers);
if (debug == 1) {
cout << "\n\nGenerated numbers: ";
printArr(WinningNumbers, 6);
}
GetPlayerSelections(PlayerNumbers);
system("cls");
cout << "\nYour Numbers: \n";
printArr(PlayerNumbers, 6);
cout << "\nPowerBall Numbers: \n";
printArr(WinningNumbers, 6);
matches = CheckForWin(PlayerNumbers, WinningNumbers, NUMBALLZ);
playerBalance += DisplayPrize(matches);
back();
break;
case 'B':
cout << "\n\nYou're balance is: $" << playerBalance << endl;
back();
break;
case 'X':
done = true;
break;
default:
cout << "\nPlease choose a letter.";
back();
break;
}
}
system("PAUSE");
}
void reset(int *arr) {
int *nPt;
nPt = arr + NUMBALLZ; //one past last elem
while (arr < nPt) {
*arr = 0;
arr++;
}
}
void displayMenu() {
system("Color 34");
cout << "\n************************POWERBALL************************\n" << endl;
cout << " MENU \n" << endl;
cout << "[i] Instructions\n[t] New Ticket\n[b] View Balance\n[x] Exit" << endl;
cout << "\n\nEnter your choice and press return: ";
}
void back() {
//clear the screen and return to main menu
system("PAUSE");
system("cls");
displayMenu();
}
void DisplayInstructions() {
system("cls");
cout << "************************POWERBALL************************\n" <<endl ;
cout << "Players win by matching one of these 9 Ways to Win:\n" ;
cout << setw(26) << "Match all 5 + PowerBall =" << setw(12) << " Grand Prize\n" ;
cout << "Odds: 1 in 175,223,510.00\n\n";
cout << setw(26) << "Match 5 =" << setw(12) << " $1,000,000\n" ;
cout << "Odds: 1 in 5,153,632.65\n\n";
cout << setw(26) << "Match 4 + PowerBall =" << setw(12) << " $10,000\n";
cout << "Odds: 1 in 648,975.96\n\n";
cout << setw(26) << "Match 4 =" << setw(12) << " $100\n" ;
cout << "Odds: 1 in 19,087.53\n\n";
cout << setw(26) << "Match 3 + PowerBall =" << setw(12) << " $100\n" ;
cout << "Odds: 1 in 12,244.83\n\n";
cout << setw(26) << "Match 3 =" << setw(12) << " $7\n" ;
cout << "Odds: 1 in 360.14\n\n";
cout << setw(26) << "Match 2 + PowerBall =" << setw(12) << " $7\n" ;
cout << "Odds: 1 in 706.43\n\n";
cout << setw(26) << "Match 1 + PowerBall =" << setw(12) << " $4\n" ;
cout << "Odds: 1 in 110.81\n\n";
cout << setw(26) << "Match PowerBall" << setw(12) << " $4\n" ;
cout << "Odds: 1 in 55.41\n\n";
}
int randomNum(int min, int max) {
int rand1 = rand() % max + 1;
}
//this works. 4/29
int inArray(int Value, int *arr, int sizeOf) {
//cout << "\nCalled inArray(" << Value << ", " << *arr << ", " << sizeOf << ")";
int numOf = 0;
for (int i = 0; i < sizeOf; i++) {
if (Value == *arr) {
numOf++;
//cout << "\n Array Value:" << *arr << " == " << Value << " Value";
} else {
arr++;
}
}
return numOf;
}
//this works. 4/29
int CheckForWin (int *pSelect, int *generated, int sizeOf) {
int winCode = 0;
int numOfMatches = 0;
int *oPt = pSelect;
int playerPB = *(pSelect + 5);
int winningPB = *(generated + 5);
if (debug == 1) {
cout << "\nPlayerPB = " << playerPB;
cout << "\nwinningPB = " << winningPB;
}
for (int i = 0; i < (sizeOf - 1); i++) {
if (inArray(*pSelect, generated, sizeOf - 1) != 0) {
numOfMatches++;
}
pSelect++;
}
switch (numOfMatches) {
case 0:
if (playerPB == winningPB) {
winCode = 1;
} else {
winCode = 0;
}
break;
case 1:
if (playerPB == winningPB) {
winCode = 2;
} else {
winCode = 0;
}
break;
case 2:
if (playerPB == winningPB) {
winCode = 3;
} else {
winCode = 0;
}
break;
case 3:
if (playerPB == winningPB) {
winCode = 5;
} else {
winCode = 4;
}
break;
case 4:
if (playerPB == winningPB) {
winCode = 7;
} else {
winCode = 6;
}
break;
case 5:
if (playerPB == winningPB) {
winCode = 9;
} else {
winCode = 8;
}
break;
}
return winCode;
}
int DisplayPrize(int winCode) {
if (winCode > 0) {
system(green);
cout <<"\n\nYou Won!!!\n\n";
}
switch (winCode) {
case 0:
system(red);
cout << "\nYou need to match at least the powerball or three white balls to win.\n";
return 0;
break;
case 1:
cout << setw(26) << "Match PowerBall" << setw(12) << " $4\n";
cout << "Odds: 1 in 55.41\n";
return 4;
break;
case 2:
cout << setw(26) << "Match 1 + PowerBall =" << setw(12) << " $4\n";
cout << "Odds: 1 in 110.81\n";
return 4;
break;
case 3:
cout << setw(26) << "Match 2 + PowerBall =" << setw(12) << " $7\n";
cout << "Odds: 1 in 706.43\n";
return 7;
break;
case 4:
cout << setw(26) << "Match 3 =" << setw(12) << " $7\n";
cout << "Odds: 1 in 1 in 360.14\n";
return 7;
break;
case 5:
cout << setw(26) << "Match 3 + PowerBall =" << setw(12) << " $100\n";
cout << "Odds: 1 in 12,244.83\n";
return 100;
break;
case 6:
cout << setw(26) << "Match 4 =" << setw(12) << " $100\n";
cout << "Odds: 1 in 19,087.53\n";
return 100;
break;
case 7:
cout << setw(26) << "Match 4 + PowerBall =" << setw(12) << " $10,000\n";
cout << "Odds: 1 in 648,975.96\n";
return 10000;
break;
case 8:
cout << setw(26) << "Match 5 =" << setw(12) << " $1,000,000\n";
cout << "Odds: 1 in 5,153,632.65\n";
return 1000000;
break;
case 9:
cout << setw(26) << "Match all 5 + PowerBall =" << setw(12) << " Grand Prize\n $100,000,000";
cout << "Odds: 1 in 175,223,510.00\n";
return 100000000;
break;
default:
if (debug = 1) {
cout << "\n\n**function DisplayPrize(int) called incorrrectly.**\n\n";
} return 0;
break;
}
}
//this works 4/29
void copyArray(int *array1, int *array2, int elem) {
int *nPt;
nPt = array1 + elem;
while (array1 < nPt) {
//cout << "Hey its working!";
*array2 = *array1;
array1++;
array2++;
}
// for (int i = 0; i < elem; i++) {
// *array2 = *array1;
// array2++;
// array1++;
// }
}
//this works 4/29
void printArr(int *arr, int elem) {
int *nPt = arr + elem;
while(arr < nPt) {
if ((arr + 1) == nPt) {
cout << "PowerBall: " << *arr;
} else {
cout << *arr << " ";
}
arr++;
}
cout << endl;
}
void GetPlayerSelections(int *playerNumbers) {
//max pointer
int *nPt = playerNumbers + NUMBALLZ;
//original pointer
int *oPt = playerNumbers;
int tryThis;
if (debug != 1) {
system("cls");
}
cout << "Pick your lottery numbers...\n";
while (nPt > playerNumbers) {
if ((playerNumbers + 1) == nPt) {
cout << "\nPick a PowerBall number between 1-35: ";
cin >> tryThis;
//check to make sure it's 1-59
if (tryThis > 0 && tryThis < 36) {
//check for dups
if (inArray(tryThis, oPt, NUMBALLZ) == 0) {
//cool, picked a good number, add it to the array and increment the pointer
*playerNumbers = tryThis;
playerNumbers++;
//TODO: this can be commented out
//cout << tryThis << " entered at " << playerNumbers;
} else {
cout << "\nDon't choose the same number twice.";
}
} else {
cout << "\nPick a whole number between 1-35";
}
} else {
cout << "\nEnter an integer between 1-59: ";
cin >> tryThis;
//check to make sure it's 1-59
if (tryThis > 0 && tryThis < 60) {
//check for dups
if (inArray(tryThis, oPt, NUMBALLZ) == 0) {
//cool, picked a good number, add it to the array and increment the pointer
*playerNumbers = tryThis;
playerNumbers++;
//TODO: this can be commented out
//cout << tryThis << " entered at " << playerNumbers;
} else {
cout << "\nDon't choose the same number twice.";
}
} else {
cout << "\nPick a whole number between 1-59";
}
}
}
}
void GeneratePowerball (int *generatedNumbers) {
int *nPt = generatedNumbers + NUMBALLZ;
int *oPt = generatedNumbers;
int tryThis;
tryThis = randomNum(0,59);
while (nPt > generatedNumbers) {
while (inArray(tryThis, oPt, NUMBALLZ) > 0) {
if ((generatedNumbers + 1) == nPt) {
tryThis = randomNum(0,35);
} else {
tryThis = randomNum(0,59);
}
}
*generatedNumbers = tryThis;
generatedNumbers++;
}
cout << "Generated Powerball." << endl;
}