-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.cpp
More file actions
561 lines (456 loc) · 14.5 KB
/
Copy pathBoard.cpp
File metadata and controls
561 lines (456 loc) · 14.5 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
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
// Group member: Riley Wu and Nidhi Prasad.
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <random>
#include <fstream>
#include <sstream>
using namespace std;
#include "Board.h"
#include "Tile.h"
#define RED "\033[48;2;230;10;10m"
#define GREEN "\033[48;2;34;139;34m" /* Grassy Green (34,139,34) */
#define BLUE "\033[48;2;10;10;230m"
#define PINK "\033[48;2;255;105;180m"
#define BROWN "\033[48;2;139;69;19m"
#define PURPLE "\033[48;2;128;0;128m"
#define ORANGE "\033[48;2;230;115;0m" /* Orange (230,115,0) */
#define GREY "\033[48;2;128;128;128m" /* Grey (128,128,128) */
#define RESET "\033[0m"
void Board::initializeBoard()
{
// Seed random number generator in your main function once
srand(time(0));
for (int i = 0; i < 2; i++)
{
initializeTiles(i); // This ensures each lane has a unique tile distribution
}
}
#include <cstdlib> // For rand() and srand()
#include <ctime> // For time()
void Board::initializeTiles(int lane)
{
// lane==0: Cub Training (easier)
// lane==1: Pride Lands (harder)
Tile temp;
int green_count = 0;
const int minGreens = 30; // at least this many green tiles per path
const int N = _BOARD_SIZE;
for (int i = 0; i < N; i++) {
if (i == 0) {
temp.color = 'Y'; // Start tile (grey)
}
else if (i == N-1) {
temp.color = 'O'; // Pride Rock (orange)
}
else {
// Always ensure at least minGreens total greens:
if (green_count < minGreens && (rand() % (N - i) < minGreens - green_count))
{
temp.color = 'G';
green_count++;
}
else {
// Now pick a “good” vs “bad” tile with different weights
int r = rand() % 100;
if (lane == 0) {
// Cub Training (easier): 30/30/15/15/10
if (r < 30) {
temp.color = 'B';
}
else if (r < 60) {
temp.color = 'P';
}
else if (r < 75) {
temp.color = 'U';
}
else if (r < 90) {
temp.color = 'R';
}
else {
temp.color = 'N';
}
}
else {
// Pride Lands (harder): 20/20/20/20/20 split
if (r < 20) {
temp.color = 'B';
}
else if (r < 40) {
temp.color = 'P';
}
else if (r < 60) {
temp.color = 'U';
}
else if (r < 80) {
temp.color = 'R';
}
else {
temp.color = 'N';
}
}
}
}
// write into the two master tracks
_masterTiles[lane][i] = temp;
}
}
Board::Board()
{
_player_count = 2;
//Initialize player position
for (int i = 0; i < _MAX_PLAYERS; i++){
_player_position[i] = 0;
}
}
Board::Board(int player_count){
_player_count = player_count;
srand((unsigned)time(nullptr));
for (int p = 0; p < _player_count; ++p) {
_player_position[p] = 0;
playerPath[p] = -1;
advisor[p] = 0;
}
// build both tracks
for (int lane = 0; lane < 2; ++lane)
initializeTiles(lane);
}
bool Board::isPlayerOnTile(int player_index, int pos)
{
if (_player_position[player_index] == pos)
{
return true;
}
return false;
}
void Board::displayBoard()
{
// Determine which lanes are active
bool used[2] = {false, false};
for (int p = 0; p < _player_count; p++) {
if (playerPath[p] >= 0 && playerPath[p] < 2) {
used[playerPath[p]] = true;
}
}
// Print each lane
for (int lane = 0; lane < 2; lane++) {
for (int pos = 0; pos < _BOARD_SIZE; pos++) {
bool onP1 = (playerPath[0] == lane && _player_position[0] == pos);
bool onP2 = (playerPath[1] == lane && _player_position[1] == pos);
int p;
if (onP1 && onP2) {
p = 1;
} else {
p = 0;
}
int tileWidth = 1 + 2 * p;
std::string tile(tileWidth, ' ');
if (onP1 && onP2) {
std::string mark = "1&2";
int start = (tileWidth - (int)mark.size()) / 2;
tile.replace(start, mark.size(), mark);
}
else if (onP1) {
tile[tileWidth/2] = '1';
}
else if (onP2) {
tile[tileWidth/2] = '2';
}
// pick color from the master
char c = _masterTiles[lane][pos].color;
string colorCode = RESET;
switch (c) {
case 'R':
colorCode = RED;
break;
case 'G':
colorCode = GREEN;
break;
case 'B':
colorCode = BLUE;
break;
case 'P':
colorCode = PINK;
break;
case 'N':
colorCode = BROWN;
break;
case 'U':
colorCode = PURPLE;
break;
case 'O':
colorCode = ORANGE;
break;
case 'Y':
colorCode = GREY;
break;
}
cout << colorCode << "|" << tile << "|" << RESET;
}
cout << "\n";
}
}
bool Board::movePlayer(int player_index)
{
// Increment player position
_player_position[player_index] += spinner();
if (_player_position[player_index] == _BOARD_SIZE - 1)
{
// Player reached last tile
return true;
}
return false;
}
int Board::getPlayerPosition(int player_index) const
{
if (player_index >= 0 && player_index <= _player_count)
{
return _player_position[player_index];
}
return -1;
}
void Board::setPlayerPosition(int player_index, int position){
_player_position[player_index] = position;
}
// spinner to choose a random number between 1-6, used when player choose to move.
int Board::spinner(){
int randomNum = rand() % 6 + 1;
return randomNum;
}
// select advisor
void Board::selectAdvisor(int player){
int advisorChoice;
cout << "Please choose your advisor\n";
cout << "0. No advisor\n";
cout << "1. Rafiki - Invisibility(the ability to become un-seen)\n";
cout << "2. Nala - Night Vision (the ability to see clearly in darkness)\n";
cout << "3. Sarabi - Energy Manipulation (the ability to shape and control the properties of energy)\n";
cout << "4. Zazu - Weather Control (the ability to influence and manipulate weather patterns)\n";
cout << "5. Sarafina - Super Speed (the ability to run 4x faster than the maximum speed of lions)\n";
do { // if the user don't pick between number 0-5
cout << "Enter choice (0-5): ";
cin >> advisorChoice;
if (advisorChoice < 0 || advisorChoice > 5) {
cout << "Invalid choice. Try again.\n";
}
} while (advisorChoice < 0 || advisorChoice > 5);
advisor[player] = advisorChoice;
}
//review advisor for the player
void Board::reviewAdvisor(int turns){
cout << "Your advisor: " << endl;
switch (advisor[turns])
{
case 0:
cout << "0. No advisor\n";
break;
case 1:
cout << "Rafiki - Invisibility(the ability to become un-seen)\n";
break;
case 2:
cout << "Nala - Night Vision (the ability to see clearly in darkness)\n";
break;
case 3:
cout << "Sarabi - Energy Manipulation (the ability to shape and control the properties of energy)\n";
break;
case 4:
cout << "Zazu - Weather Control (the ability to influence and manipulate weather patterns)\n";
break;
case 5:
cout << "5. Sarafina - Super Speed (the ability to run 4x faster than the maximum speed of lions)\n";
break;
default:
break;
}
}
void Board::setPlayerPath(int player, int choice){
int lane = choice - 1;
playerPath[player] = lane;
// copy into _tiles[player][]
for (int i = 0; i < _BOARD_SIZE; i++)
_tiles[player][i] = _masterTiles[lane][i];
// Copy the master into this player’s working tiles
for (int i = 0; i < _BOARD_SIZE; i++)
_tiles[player][i] = _masterTiles[lane][i];
// If the other player already chose this same lane
int other = 1 - player;
if (playerPath[other] == lane) {
for (int i = 0; i < _BOARD_SIZE; i++)
_tiles[other][i] = _masterTiles[lane][i];
}
}
// Tile implementation
char Board::getCurrentTileColor(int player_index) const {
int pos = _player_position[player_index];
return _tiles[player_index][pos].color;
}
Player Board::HandleTileEffects(Player player, int turns){
char col = getCurrentTileColor(turns);
switch (col)
{
case 'G': // Green
Green(player, turns);
break;
case 'B': // Oasis
cout << "Congrats, you landed on Oasis Tile, you get an extra turn and gain 200 Stamina, Strength, and Wisdom Points." << endl;
player.Oasis();
turns--;
break;
case 'P': // Pink
cout << "You landed on Counseling Tile, you gained 300 Stamina, Strength, and Wisdom Points. And you get to choose an advisor. " << endl;
player.Pink();
selectAdvisor(turns);
break;
case 'R': // Red
cout << "Uh-oh, you landed on the Graveyard Tile. You will move back 10 tiles and lose 100 Stamina, Strength, and Wisdom Points." << endl;
player.Red();
_player_position[turns] -= 10;
break;
case 'N': // Brown
cout << "You landed on the Hyenas Tile, you will be returned to your previous position and lose 300 Stamina Points. " << endl;
player.Brown();
_player_position[turns] -= spinner();
break;
case 'U': // Purple
cout << "Time for a test of wits! Here's the riddle, if you answer correctly you will earn 500 Wisdom Points. " << endl;
bool r;
r = riddle();
if (r){
player.ifPurple();
}
break;
}
return player;
}
bool Board::riddle(){
ifstream file("riddles.txt");
if (!file){
cout << "Could not open the file." << endl;
return false;
}
string line;
getline(file, line);
// read everything into a vector
vector<string> entries;
while (getline(file, line)) {
if (!line.empty())
entries.push_back(line);
}
file.close();
// pick one at random
int idx = rand() % entries.size();
string entry = entries[idx];
// split on the last '|'
size_t sep = entry.find_last_of('|');
string question = entry.substr(0, sep);
string answer = entry.substr(sep + 1);
// prompt the player
string attempt;
cout << "Here's the riddle: ";
cout << question << endl;
cout << "Your answer: ";
cin >> attempt;
// check and award
if (attempt == answer) {
cout << "Correct! +500 Wisdom." << endl;
return true;
} else {
cout << "Nope, the answer was: " << answer << endl;
return false;
}
}
// Green tile trigger random events function
Player Board::Green(Player p, int turns) {
// 50% chance: nothing happens
if (rand() % 2 != 0) {
return p;
}
cout << "50% chance that an event will happen on Green tile, and you got it!" << endl;
struct Ev { string desc; int adv, pts; };
vector<Ev> evs;
ifstream fin("random_events.txt");
if (!fin) {
cout << "Cannot open random_events.txt\n";
return p;
}
// skip the header
string line;
getline(fin, line);
// read & parse
while (getline(fin, line)) {
if (line.empty()) {
continue;
}
istringstream ss(line);
string desc, pathStr, advStr, ptsStr;
if (!getline(ss, desc,'|')) {
continue;
}
if (!getline(ss, pathStr,'|')) {
continue;
}
if (!getline(ss, advStr,'|')) {
continue;
}
if (!getline(ss, ptsStr)) {
continue;
}
int pathId, advId, pts;
try {
pathId = stoi(pathStr);
advId = stoi(advStr);
pts = stoi(ptsStr);
} catch (...) {
//... is used to handle any type of exception
continue;
}
// only keep events for this player's path
if (pathId != playerPath[turns] - 1)
continue;
evs.push_back({desc, advId, pts});
}
fin.close();
if (evs.empty())
return p;
// pick one at random
auto e = evs[rand() % evs.size()];
// advisor protection
if (e.pts < 0 && advisor[turns] == e.adv) {
cout << "Your advisor protected you from “" << e.desc << "”\n";
return p;
}
// print and apply result
if (e.pts >= 0) {
cout << "Good news: " << e.desc << " --> You gained " << e.pts << " Pride Points\n";
} else {
cout << "Bad news: " << e.desc << " --> You gained " << -e.pts << " Pride Points lost\n";
}
p.addPrideGreen(e.pts);
return p;
}
// end of the game
// SORTING ALGORITHM: sort all the pride points to determine winner
int Board::findWinner(int p1PridePoints, int p2PridePoints){
// using sorting algorithm to find the winner between player 1 and player 2
int order = -1;
int pts[2] = {p1PridePoints, p2PridePoints};
for (int i = 0; i < 1; i++) { //Selection Sort
int min = i; //sets current element to minimum value
for (int j = i+1; j < 2; j++) {
if (pts[j] < pts[min]) { //Iterate through the unsorted portion of the array to find the actual minimum
min = j; //Update the min value if a min is found
}
}
int temp = pts[i]; //Swaps elements to move the min value to the correctly sorted position
pts[i] = pts[min];
pts[min] = temp;
}
if (pts[1] == p1PridePoints) {
order = 0;
}
else if (pts[1] == p2PridePoints) {
order = 1;
}
return order;
}