-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhorserace.cpp
531 lines (451 loc) · 17.2 KB
/
horserace.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
#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
#include <sstream>
#include <fstream>
#include <chrono>
#include <thread>
#include <regex>
#include <random>
#include "horserace.h"
#include "introduction.h"
#include "visuals.h"
#include "gameroom.h"
#include "revive.h"
// need to know how much the user wagered across functions
int users_wager;
int users_horse;
Introduction token_access;
// constructor
HorseRace::HorseRace(){
};
// kicks off the horse race game
void HorseRace::run_horse_race()
{
horse_race_menu();
};
// Prompts user to either read the instruction or start playing horse race.
void HorseRace::horse_race_menu()
{
// available options
int instruction = 1;
int play = 2;
// pause / delay time
int time = 3000;
// users input is stored and checked if valid in these variables
int users_input;
bool is_valid_input;
do {
// Clear the console
system("clear");
std::cout << "\n\n\n\n\n\n\n\n\n";
std::cout << " HORSE RACE" << std::endl;
std::cout << "\n";
std::cout << " 1: HOW TO PLAY HORSE RACE" << std::endl;
std::cout << " 2: PLAY HORSE RACE" << std::endl;
std::cout << "\n";
std::cout << " PLEASE SELECT AN OPTION: ";
// Read the entire line of input
std::string line;
std::getline(std::cin, line);
// Create a string stream to parse the integer
std::istringstream iss(line);
if (iss >> users_input) {
// Check for extra characters in the line
if (iss.eof() && (users_input == 1 || users_input == 2)) {
is_valid_input = true;
} else {
// Extra characters present
system("clear");
std::cout << "\n\n\n\n\n\n\n\n\n\n\n";
std::cout << " PLEASE ENTER A VALID RESPONSE";
std::cout << "\n";
std::this_thread::sleep_for(std::chrono::milliseconds(time));
is_valid_input = false;
}
} else {
// Input is not an integer
system("clear");
std::cout << "\n\n\n\n\n\n\n\n\n\n\n";
std::cout << " PLEASE ENTER A VALID RESPONSE";
std::cout << "\n";
std::this_thread::sleep_for(std::chrono::milliseconds(time));
is_valid_input = false;
}
} while (!is_valid_input);
// once valid response is given, breaks out of do while loop and directs player to instructions or to play
if (users_input == (instruction))
{
instructions();
}
else if (users_input == (play))
{
place_wager();
}
}
// Gives user instructions and information on how to play
void HorseRace::instructions()
{
bool is_valid_input = false;
int users_input;
int time = 3000;
/*
Prompt the user for input (play or return to game floor)
If the user enters an invalid response, it will loop and keep prompting a new input until a valid one is given
*/
do
{
// clear the console
system("clear");
std::cout << "\n\n" << " HORSE RACE" << "\n\n\n\n" << " -EACH HORSE IS REPRESENTED BY A NUMBER (1-8)" << "\n\n" << " -THE PLAYER SELECTS ONE HORSE FROM EIGHT OPTIONS AND PLACES A WAGER" << "\n\n" << " -IF THE PLAYERS HORSE COMES IN FIRST, THE PLAYER WINS FIVE TIMES THEIR WAGER" << "\n\n" << " -IF THE PLAYERS HORSE DOES NOT COME IN FIRST, THEY LOSE THE WAGERED AMOUNT"; //c
std::cout << "\n\n\n\n" << " 1: PLAY HORSE RACE" << std::endl;
std::cout << " 2: RETURN TO THE GAME FLOOR" << std::endl;
std::cout << "\n" << " PLEASE SELECT AN OPTION: ";
//std::cin >> users_input;
// Read the entire line of input
std::string line;
std::getline(std::cin, line);
// Create a string stream to parse the integer
std::istringstream iss(line);
if (iss >> users_input) {
// Check for extra characters in the line
if (iss.eof() && (users_input == 1 || users_input == 2)) {
is_valid_input = true;
} else {
// Extra characters present
system("clear");
std::cout << "\n\n\n\n\n\n\n\n\n\n\n";
std::cout << " PLEASE ENTER A VALID RESPONSE";
std::cout << "\n";
std::this_thread::sleep_for(std::chrono::milliseconds(time));
is_valid_input = false;
}
} else {
// Input is not an integer
system("clear");
std::cout << "\n\n\n\n\n\n\n\n\n\n\n";
std::cout << " PLEASE ENTER A VALID RESPONSE";
std::cout << "\n";
std::this_thread::sleep_for(std::chrono::milliseconds(time));
is_valid_input = false;
}
} while (!is_valid_input);
if ((users_input) == (1))
{
place_wager();
}
// returns user to game room (other games)
else if ((users_input) == (2))
{
Gameroom game_room;
game_room.return_to_games_menu();
}
}
// Prompts user to enter wager / bet amount.
void HorseRace::place_wager()
{
// clear console
system("clear");
std::string key;
int time = 3000;
bool is_valid_response;
// this object is used to check the # of tokens the user has in their account
Introduction token_access_obj;
// prompts user to enter a horse to bet on and the wager amount
do
{
system("clear");
std::cout << "\n\n\n\n\n\n" << " HORSE RACE";
std::cout << "\n\n" << " SELECT A HORSE (1-8): ";
std::cin >> users_horse;
// checks if input is INT type and if user selected a valid horse
if (std::cin.fail())
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
system("clear");
std::cout << "\n\n\n\n\n\n\n\n\n\n\n";
std::cout << " PLEASE ENTER A VALID RESPONSE";
std::cout << "\n";
std::this_thread::sleep_for(std::chrono::milliseconds(time));
is_valid_response = false;
continue;
}
else if (users_horse > 8 || users_horse < 1)
{
//std::cin.clear();
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
system("clear");
std::cout << "\n\n\n\n\n\n\n\n\n\n\n";
std::cout << " PLEASE ENTER A VALID RESPONSE";
std::cout << "\n";
std::this_thread::sleep_for(std::chrono::milliseconds(time));
is_valid_response = false;
continue;
}
std::cout << "\n" << " WAGER AMOUNT: ";
std::cin >> users_wager;
// check if wager was INT type and if the user wagered a valid amount
if (std::cin.fail())
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
system("clear");
std::cout << "\n\n\n\n\n\n\n\n\n\n\n";
std::cout << " PLEASE ENTER A VALID RESPONSE";
std::cout << "\n";
std::this_thread::sleep_for(std::chrono::milliseconds(time));
is_valid_response = false;
continue;
}
else if (users_wager > token_access_obj.get_tokens())
{
//std::cin.clear();
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
system("clear");
std::cout << "\n\n\n\n\n\n\n\n\n\n\n";
std::cout << " PLEASE ENTER A VALID RESPONSE";
std::cout << "\n";
std::this_thread::sleep_for(std::chrono::milliseconds(time));
is_valid_response = false;
continue;
}
std::cout << "\n" << " YOU WAGERED " << users_wager << token_or_tokens(users_wager) << " ON HORSE #" << users_horse << "\n";
std::cout << "\n\n\n\n" << " ENTER ANY KEY TO CONTINUE: ";
std::cin >> key;
// if all conditions are met user entered valid information and breaks out of loop
is_valid_response = true;
//std::cin.clear();
} while (!is_valid_response);
// call race once user selects a horse to bet on and the amount once inputs are validated.
animate_race();
}
// returns a random horse if there is a tie
int HorseRace::tie_breaker(int total_horses)
{
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist6(1, total_horses);
int winning_horse = dist6(rng);
return winning_horse;
}
// returns how many spaces the horse should run between 0-2
int HorseRace::get_random_spaces()
{
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist6(0, 2);
int spaces = dist6(rng);
return spaces;
}
// builds the path length the horse will run
std::string HorseRace::build_racers_path()
{
int path_length = 11;
std::string current_horse = "";
std::string line = "|";
for (int i = 0; i <= path_length; i++)
{
int spaces = get_random_spaces();
for (int j = 0; j < spaces; j++)
{
current_horse = current_horse + line;
}
}
return current_horse;
}
// checks if the user lost or won his bet and modifies their account
void HorseRace::check_outcome(int winning_horse)
{
Visuals visuals_obj;
// user picked the winning horse. they win
if (users_horse == winning_horse)
{
std::cout << "\n\n" << " YOUR HORSE WON. YOU WON " << (users_wager * 5) << token_or_tokens(users_wager * 5) << std::endl;
token_access.set_tokens(token_access.get_tokens() + (users_wager * 5));
}
// otherwise they picked the wrong horse and lost.
else
{
std::cout << "\n\n" << " YOUR HORSE LOST. YOU LOST " << (users_wager) << token_or_tokens(users_wager) << std::endl;
token_access.set_tokens(token_access.get_tokens() - (users_wager));
}
}
// this will take in the token amount and return the string "TOKEN" or "TOKENS" depending on how many
std::string HorseRace::token_or_tokens(int token_amount){
if((token_amount) == 1){
return " TOKEN";
}
else{
return " TOKENS";
}
}
// promtps user to either play again or return to gameroom
void HorseRace::replay()
{
int users_input;
int time = 3000;
bool is_valid_input;
// Clear any leftover newline from the buffer
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
do
{
system("clear");
std::cout << "\n\n\n\n\n\n\n\n\n";
std::cout << " WOULD YOU LIKE TO PLAY AGAIN?" << std::endl;
std::cout << "\n";
std::cout << " 1: PLAY AGAIN" << std::endl;
std::cout << " 2: RETURN TO GAMEROOM" << std::endl;
std::cout << "\n";
std::cout << " PLEASE SELECT AN OPTION: ";
// Read the entire line of input
std::string line;
std::getline(std::cin, line);
// Create a string stream to parse the integer
std::istringstream iss(line);
if (iss >> users_input) {
// Check for extra characters in the line
if (iss.eof() && (users_input == 1 || users_input == 2)) {
is_valid_input = true;
} else {
// Extra characters present
system("clear");
std::cout << "\n\n\n\n\n\n\n\n\n\n\n";
std::cout << " PLEASE ENTER A VALID RESPONSE";
std::cout << "\n";
std::this_thread::sleep_for(std::chrono::milliseconds(time));
is_valid_input = false;
}
}
else{
// Input is not an integer
system("clear");
std::cout << "\n\n\n\n\n\n\n\n\n\n\n";
std::cout << " PLEASE ENTER A VALID RESPONSE";
std::cout << "\n";
std::this_thread::sleep_for(std::chrono::milliseconds(time));
is_valid_input = false;
}
} while (!is_valid_input);
if (users_input == (1))
{
place_wager();
}
else if (users_input == (2))
{
Gameroom GRobj;
GRobj.return_to_games_menu();
}
}
// This is where we animate the horse race
void HorseRace::animate_race()
{
// clear console
system("clear");
// Time delays
int title_timer = 1000;
int race_timer = 400;
int outcome_timer = 5000;
// Horses & their paths
int number_of_horses = 8;
int longest_path = 0;
std::string h1 = build_racers_path();
std::string h2 = build_racers_path();
std::string h3 = build_racers_path();
std::string h4 = build_racers_path();
std::string h5 = build_racers_path();
std::string h6 = build_racers_path();
std::string h7 = build_racers_path();
std::string h8 = build_racers_path();
std::string horses[8] = {h1, h2, h3, h4, h5, h6, h7, h8};
std::string winner;
// horses that are tied
std::vector<std::string> tied_horses;
std::vector<int> tied_horse_positions;
int corresponding_position;
// find longest path
for (int i = 0; i < number_of_horses; i++)
{
int winningHorse = ((horses[i]).length());
if (longest_path < winningHorse)
{
longest_path = winningHorse;
}
}
// check how many horses are tied
for (int x = 0; x < number_of_horses; x++)
{
if (horses[x].length() == longest_path)
{
tied_horses.push_back(horses[x]);
tied_horse_positions.push_back(x);
}
}
// if there is a tie, pick a random horse from the pile and add 1 extra line to make it the winner.
if (tied_horses.size() > 1)
{
int random_number = tie_breaker(tied_horses.size());
winner = tied_horses[random_number - 1]; // maybe causes seg fault if the - 1 is not there
corresponding_position = tied_horse_positions[random_number - 1];
winner = winner + "|";
}
else
{
winner = tied_horses[0];
corresponding_position = tied_horse_positions[0];
}
horses[corresponding_position] = winner;
longest_path = winner.length();
// make all horse strings the same length
for (int j = 0; j < number_of_horses; j++)
{
std::string horse_in_construction = horses[j];
for (int k = 0; horse_in_construction.length() < longest_path; k++)
{
horse_in_construction = horse_in_construction + " ";
}
horses[j] = horse_in_construction;
}
std::string ready = " READY";
std::string set = " SET";
std::string go = " GO!";
std::cout << "\n\n\n\n\n\n\n\n\n\n"
<< std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(title_timer));
std::cout << ready << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(title_timer));
std::cout << set << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(title_timer));
std::cout << go << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(title_timer));
// clear console so only the horse race is displayed
system("clear");
std::cout << "" << std::endl;
std::string lane_numbers = " 1 2 3 4 5 6 7 8";
std::string line;
std::string first_lane_spacer = " ";
std::string lane_spacer = " ";
std::cout << lane_numbers << std::endl;
// display the horses and their paths in the terminal
for (int i = 0; i < longest_path; i++)
{
std::this_thread::sleep_for(std::chrono::milliseconds(race_timer));
line = first_lane_spacer + horses[0].at(i) + lane_spacer + horses[1].at(i) + lane_spacer + horses[2].at(i) + lane_spacer + horses[3].at(i) + lane_spacer + horses[4].at(i) + lane_spacer + horses[5].at(i) + lane_spacer + horses[6].at(i) + lane_spacer + horses[7].at(i);
std::cout << line << std::endl;
}
corresponding_position = corresponding_position + 1;
check_outcome(corresponding_position);
std::this_thread::sleep_for(std::chrono::milliseconds(outcome_timer));
// 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{
replay();
}
}