-
Notifications
You must be signed in to change notification settings - Fork 1
/
keno.cpp
626 lines (518 loc) · 17.7 KB
/
keno.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
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <random>
#include <stdlib.h>
#include <regex>
#include <algorithm>
#include <chrono>
#include <thread>
#include <cmath>
#include "introduction.h"
#include "gameroom.h"
#include "keno.h"
#include "revive.h"
// constructor
Keno::Keno(){
}
// kicks off the keno game
void Keno::run_keno(){
keno_menu();
}
// keno insturctions
void Keno::keno_instructions(){
system("clear");
bool valid_input;
int users_input;
do
{
system("clear");
std::cout << "\n\n" << " KENO" << "\n\n\n" << " -THE PLAYER SELECTS BETWEEN 2-10 NUMBERS RANGING FROM 1 TO 80 AND PLACES A" << "\n" << " WAGER ON THAT POOL OF NUMBERS" << "\n\n" << " -THEN, 20 NUMBERS ARE DRAWN AT RANDOM" << "\n\n" << " -THE PLAYER WINS BASED ON MATCHING SELECTED NUMBERS TO THE DRAWN ONES" << "\n\n" << " -THE PAYOUTS VARY BASED ON THE TOTAL NUMBER OF SELECTED NUMBERS" << "\n" << " (SEE GITHUB README FOR DETAILS)" << "\n\n" << " -IF NONE OF THE SELECTED NUMBERS MATCH, THEY LOSE THE WAGERED AMOUNT" << "\n"; //c //c
std::cout << "\n\n" << " 1: PLAY KENO" << std::endl; //c //c
std::cout << " 2: RETURN TO THE GAME FLOOR" << std::endl; //c //c
std::cout << "\n" << " PLEASE SELECT AN OPTION: "; //c //c
std::cin >> users_input;
valid_input = validate_input(users_input);
} while (!valid_input);
if (users_input == (1))
{
play_keno();
}
else if (users_input == (2))
{
Gameroom game_room_obj;
game_room_obj.return_to_games_menu();
}
}
// prompt user with the keno menu to either play or display instructions
void Keno::keno_menu(){
// Clear the input buffer
std::cin.clear();
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
int instructions = 1;
int play = 2;
int users_input;
bool valid_input;
do
{
system("clear");
std::cout << "\n\n\n\n\n\n\n\n\n" << " KENO" << "\n" << std::endl; //c //c
std::cout << " 1: HOW TO PLAY KENO" << std::endl; //c //c
std::cout << " 2: PLAY KENO" << std::endl; //c //c
std::cout << "\n" << " PLEASE SELECT AN OPTION: "; //c //c
std::cin >> users_input;
valid_input = validate_input(users_input);
} while (!valid_input);
if (users_input == (instructions))
{
keno_instructions();
}
else if (users_input == (play))
{
play_keno();
}
}
// plays keno
void Keno::play_keno(){
int time = 6000;
bool are_valid_numbers;
bool is_valid_wager = false;
int wagered_amount;
do{
system("clear");
std::cout << "\n\n" << " KENO" << "\n\n\n" << std::endl; //c
int counter = 0;
std::string add_space = " "; //c
std::string wagered_numbers;
for(int x = 1; x <= 80; x++){
if(counter == 0){
std::cout << add_space;
}
if(x < 10 && x > 0){
std::cout << " [0" + std::to_string(x) + "]";
}
else{
std::cout << " [" + std::to_string(x) + "]";
}
counter++;
if(counter >= 10){
std::cout << "" << std::endl;
counter = 0;
}
}
std::cout << "\n\n\n" << " SELECT BETWEEN 2-10 NUMBERS TO BET ON:" << std::endl; //c //c
std::cout << " ";
std::getline(std::cin >> std::ws, wagered_numbers);
are_valid_numbers = validate_wagered_numbers(wagered_numbers);
std::cout << "\n" << " WAGER AMOUNT: "; //c //c
std::cin >> wagered_amount;
if (std::cin.fail()){
std::cin.clear();
std::cin.ignore(10000, '\n');
system("clear");
std::cout << "\n\n\n\n\n\n\n\n\n\n\n";
std::cout << " PLEASE ENTER A VALID RESPONSE"; //c //c
std::cout << "\n";
std::this_thread::sleep_for(std::chrono::milliseconds(time));
continue;
}
is_valid_wager = validate_wager(wagered_amount);
if(!are_valid_numbers || !is_valid_wager){
system("clear");
std::cout << "\n\n\n\n\n\n\n\n\n\n\n";
std::cout << " PLEASE ENTER A VALID RESPONSE"; //c //c
std::cout << "\n";
std::this_thread::sleep_for(std::chrono::milliseconds(time));
}
else {
std::vector<float> winning_numbers_vector = {};
// draw 20 winning numbers
while(winning_numbers_vector.size() < 20){
int random_number = random_number_generator();
// check if random number is already in the vector
if(std::count(winning_numbers_vector.begin(), winning_numbers_vector.end(), random_number)){
continue;
}
else{
winning_numbers_vector.insert(winning_numbers_vector.end(), random_number);
}
}
system("clear");
std::this_thread::sleep_for(std::chrono::milliseconds(time / 3));
std::cout << "\n\n" << " THE WINNING NUMBERS ARE:" << "\n\n" << std::endl; //c //c
std::this_thread::sleep_for(std::chrono::milliseconds(time / 2));
for(int x = 1; x <= 80; x++){
if(counter == 0){
std::cout << add_space;
}
if(x < 10 && x > 0){
std::cout << "";
}
// the number is in the winning numbers pool
if(std::count(winning_numbers_vector.begin(), winning_numbers_vector.end(), x)){
std::cout << " [XX]";
}
else
{
if(x < 10 && x > 0){
std::cout << " [0" + std::to_string(x) + "]";
}
else{
std::cout << " [" + std::to_string(x) + "]";
}
}
counter++;
if(counter >= 10){
std::cout << "" << std::endl;
counter = 0;
}
}
std::cout << "\n\n ";
for(float i = 0; i < winning_numbers_vector.size(); i++){
std::cout << winning_numbers_vector.at(i) << " ";
}
std::cout << "\n\n" << " YOUR NUMBERS: "; //c //c
int hits = 0;
// convert to a vector
std::vector<float> wagered_numbers_vector = {};
std::string number_builder;
wagered_numbers = wagered_numbers + " ";
for(int i = 0; i < wagered_numbers.length(); i++)
{
if(wagered_numbers[i] == '.'){
number_builder = number_builder + '.';
}
else if(wagered_numbers[i] != ' '){
int t = wagered_numbers[i] - '0';
number_builder = number_builder + std::to_string(t);
}
if(wagered_numbers[i+1] == ' '){
wagered_numbers_vector.insert(wagered_numbers_vector.end(), stof(number_builder));
number_builder = "";
continue;
}
}
// check how many hits (matched numbers by user input and drawing) and print their wagered numbers
for(int i = 0; i < wagered_numbers_vector.size(); i++){
std::cout << wagered_numbers_vector.at(i) << " ";
if(std::count(winning_numbers_vector.begin(), winning_numbers_vector.end(), wagered_numbers_vector.at(i))){
hits++;
}
}
std::cout << "\n" << " HITS: " << hits << "\n\n"; //c //c
outcome_checker(hits, wagered_numbers_vector.size(), wagered_amount);
std::this_thread::sleep_for(std::chrono::milliseconds(time + time));
}
} while (!are_valid_numbers || !is_valid_wager);
// user is out of tokens
Introduction token_access_obj;
if(token_access_obj.get_tokens() < 1){
Revive revive_obj;
revive_obj.Explanation();
Gameroom gameroom_obj;
gameroom_obj.return_to_games_menu();
}
else{
play_again();
}
}
void Keno::play_again(){
int input;
bool outcome = false;
do{
system("clear");
std::cout << "\n\n\n\n\n\n\n\n\n";
std::cout << " WOULD YOU LIKE TO PLAY AGAIN?" << std::endl; //c //cc
std::cout << "\n";
std::cout << " 1: PLAY AGAIN" << std::endl; //c //c
std::cout << " 2: RETURN TO GAMEROOM" << std::endl; //c //c
std::cout << "\n";
std::cout << " PLEASE SELECT AN OPTION: "; //c //c
std::cin >> input;
bool outcome = validate_input(input);
if ((input) == (1))
{
play_keno();
}
// returns user to game room (other games)
else if ((input) == (2))
{
Gameroom game_room;
game_room.return_to_games_menu();
}
} while(!outcome);
}
// validates that the user wagered a valid amount (i.e they have the funds)
bool Keno::validate_wager(int wager){
Introduction account_checker;
if(wager < 0){
return false;
}
else if(wager > account_checker.get_tokens()){
return false;
}
return true;
}
// validates that the string of wagered numbers is valid
bool Keno::validate_wagered_numbers(std::string users_input){
std::string working_input = users_input;
// remove all whitespace
working_input.erase(std::remove_if(working_input.begin(), working_input.end(), ::isspace),working_input.end());
// check if the string contains only numbers
bool valid_digits = is_digit(working_input);
if(valid_digits == false){
return false;
}
// check to see if all digits are between 1 - 80
// remove leading spaces
users_input = std::regex_replace(users_input, std::regex("^ +"), "");
// remove trailing spaces
users_input = std::regex_replace(users_input, std::regex(" +$"), "");
// removing extra spaces
users_input = regex_replace(users_input, std::regex(" +"), " ");
users_input = users_input + " ";
// convert to a vector
std::vector<float> wagered_numbers_vector = {};
std::string number_builder;
for(int i = 0; i < users_input.length(); i++)
{
if(users_input[i] == '.'){
number_builder = number_builder + '.';
}
else if(users_input[i] != ' '){
int t = users_input[i] - '0';
number_builder = number_builder + std::to_string(t);
}
if(users_input[i+1] == ' '){
wagered_numbers_vector.insert(wagered_numbers_vector.end(), stof(number_builder));
number_builder = "";
continue;
}
}
if(wagered_numbers_vector.size() > 10 || wagered_numbers_vector.size() < 2){
return false;
}
for(int j = 0; j < wagered_numbers_vector.size(); j++){
if(wagered_numbers_vector.at(j) != std::floor(wagered_numbers_vector.at(j))){
return false;
}
if(wagered_numbers_vector.at(j) < 1 || wagered_numbers_vector.at(j) > 80){
return false;
}
}
return true;
}
// check if input is only digits
bool Keno::is_digit(const std::string &str)
{
return str.find_first_not_of("0123456789") == std::string::npos;
}
// validate the users input for main menu
bool Keno::validate_input(int users_input){
system("clear");
const int time = 3000;
// Check if user even entered a digit
if (std::cin.fail())
{
std::cin.clear();
std::cin.ignore(10000, '\n');
std::cout << "\n\n\n\n\n\n\n\n\n\n\n";
std::cout << " PLEASE ENTER A VALID RESPONSE"; //c //c
std::cout << "\n";
std::this_thread::sleep_for(std::chrono::milliseconds(time));
system("clear");
return false;
}
// Check if digit is one of the available options
if (users_input == (1) || users_input == (2))
{
return true;
}
else
{
std::cout << "\n\n\n\n\n\n\n\n\n\n\n";
std::cout << " PLEASE ENTER A VALID RESPONSE"; //c //c
std::cout << "\n";
std::this_thread::sleep_for(std::chrono::milliseconds(time));
system("clear");
return false;
}
}
int Keno::random_number_generator(){
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist6(1, 80);
int number = dist6(rng);
return number;
}
// check how many hits and add tokens to profile
void Keno::outcome_checker(int hits, int spots, int wager){
int won_tokens = 0;
switch (spots)
{
case 2:
switch(hits)
{
case 2:
won_tokens = wager * 15;
break;
}
break;
case 3:
switch(hits)
{
case 2:
won_tokens = wager;
break;
case 3:
won_tokens = wager * 16;
break;
}
break;
case 4:
switch(hits)
{
case 2:
won_tokens = wager;
break;
case 3:
won_tokens = wager * 2;
break;
case 4:
won_tokens = wager * 25;
break;
}
break;
case 5:
switch(hits)
{
case 3:
won_tokens = wager;
break;
case 4:
won_tokens = wager * 3;
break;
case 5:
won_tokens = wager * 160;
break;
}
break;
case 6:
switch(hits)
{
case 3:
won_tokens = wager;
break;
case 4:
won_tokens = wager;
break;
case 5:
won_tokens = wager * 15;
break;
case 6:
won_tokens = wager * 270;
break;
}
break;
case 7:
switch(hits)
{
case 3:
won_tokens = wager;
break;
case 4:
won_tokens = wager * 2;
break;
case 5:
won_tokens = wager * 4;
break;
case 6:
won_tokens = wager * 70;
break;
case 7:
won_tokens = wager * 1000;
break;
}
break;
case 8:
switch(hits)
{
case 4:
won_tokens = wager * 2;
break;
case 5:
won_tokens = wager * 3;
break;
case 6:
won_tokens = wager * 16;
break;
case 7:
won_tokens = wager * 240;
break;
case 8:
won_tokens = wager * 1250;
break;
}
break;
case 9:
switch(hits)
{
case 4:
won_tokens = wager;
break;
case 5:
won_tokens = wager * 5;
break;
case 6:
won_tokens = wager * 7;
break;
case 7:
won_tokens = wager * 50;
break;
case 8:
won_tokens = wager * 580;
break;
case 9:
won_tokens = wager * 1111;
break;
}
break;
case 10:
switch(hits)
{
case 5:
won_tokens = wager * 3;
break;
case 6:
won_tokens = wager * 4;
break;
case 7:
won_tokens = wager * 20;
break;
case 8:
won_tokens = wager * 125;
break;
case 9:
won_tokens = wager * 600;
break;
case 10:
won_tokens = wager * 1500;
break;
}
break;
default:
std::cout << "Something went wrong" << std::endl;
}
// access tokens to add and remove tokens from balance
Introduction token_access;
if(won_tokens == 0){
std::cout << " YOU LOST " << std::to_string(wager) << " TOKENS" << "\n\n"; //c //c
token_access.set_tokens(token_access.get_tokens() - wager);
}
else{
std::cout << " YOU WON " << std::to_string(won_tokens) << " TOKENS" << "\n\n"; //c //c
token_access.set_tokens(token_access.get_tokens() + won_tokens);
}
}