-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.h
493 lines (454 loc) · 12.5 KB
/
display.h
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
#pragma once
#include "fundamentals.h"
#include "helper.h"
// #include "modes.h"
#include "maze.h"
void displayHealth()
{
string healthString = "Health: " + to_string(superman.health);
string scoreString = "Score: " + to_string(score);
string escapeString = "Press Esc to skip the game.";
Position healthStringPosition(((dialogueBoxWidth + dialogueBoxStart.x) + dialogueBoxStart.x) / 2 - healthString.length() / 2, 2);
Position scoreStringPosition(((dialogueBoxWidth + dialogueBoxStart.x) + dialogueBoxStart.x) / 2 - healthString.length() / 2, 3);
Position escapeStringPosition(((dialogueBoxWidth + dialogueBoxStart.x) + dialogueBoxStart.x) / 2 - escapeString.length() / 2, 0);
int startX = dialogueBoxStart.x;
int startY = 1;
gotoxy(startX, startY - 1);
for (int i = 0; i < dialogueBoxWidth; i++)
{
cout << " ";
}
gotoxy(escapeStringPosition.x, escapeStringPosition.y);
cout << escapeString;
// Draw the top border
gotoxy(startX, startY);
char topAciiCharacter = 203;
for (int i = 0; i < dialogueBoxWidth; i++)
{
cout << topAciiCharacter;
}
// Draw the sides
char leftAciiCharacter = 204;
char rightAciiCharacter = 185;
for (int j = 0; j < 2; j++)
{
gotoxy(startX, startY + j + 1);
for (int i = 0; i < dialogueBoxWidth; i++)
{
if (i == 0)
{
cout << leftAciiCharacter;
}
else if (i == dialogueBoxWidth - 1)
{
cout << rightAciiCharacter;
}
else
{
cout << " ";
}
}
}
// Draw the bottom border
char bottomAciiCharacter = 202;
gotoxy(startX, startY + 3);
for (int i = 0; i < dialogueBoxWidth; i++)
{
cout << bottomAciiCharacter;
}
if (healthString.length() >= 9 && healthString.length() < 11)
{
// Calculate the number of spaces to add
int spacesToAdd = 11 - healthString.length();
// Append spaces to the string
for (int i = 0; i < spacesToAdd; ++i)
{
healthString += ' ';
}
}
if (scoreString.length() >= 9 && scoreString.length() < 11)
{
// Calculate the number of spaces to add
int spacesToAdd = 11 - scoreString.length();
// Append spaces to the string
for (int i = 0; i < spacesToAdd; ++i)
{
scoreString += ' ';
}
}
gotoxy(healthStringPosition.x, healthStringPosition.y);
cout << healthString;
gotoxy(scoreStringPosition.x, scoreStringPosition.y);
cout << scoreString;
}
void menuOptions(string arr[], int size, int activeOption)
{
system("cls");
// Clear the console (for Windows, use system("clear") for Unix-based systems)
cout << "\x1b[33m\n\nSelect your choice. \x1b[0m" << endl;
cout << "+=============================+" << endl;
for (int i = 0; i < size; i++)
{
if (i == activeOption)
{
cout << BOLD << UNDERLINE << " " << arr[i] << RESET << endl;
}
else
{
cout << " " << arr[i] << endl;
}
}
cout << "+=============================+" << endl;
}
bool enemyCoordiCorrectness(int x, int y)
{
return (getCharacterAtPosition(x, y) != (char)219 && getCharacterAtPosition(x, y) != '+');
}
bool chestCoordiCorrectness(int x, int y)
{
return (getCharacterAtPosition(x, y) != (char)219 && getASCIIAtPosition(x, y) != -80); // -80 is for enemies.
}
void displayScreenBorders() // not working correctly right now
{
int screenWidth = getConsoleWidth(); // Assuming 'screen' is declared somewhere in your code
int screenHeight = getConsoleWidth();
// Display top border
for (int i = 0; i < screenWidth; ++i)
{
cout << "#";
}
// Display side borders and empty space in between
for (int i = 0; i < screenHeight - 2; ++i)
{
gotoxy(0, i);
cout << "#";
gotoxy(screenWidth - 2, i);
cout << "#" << endl;
}
// Display bottom border
for (int i = 0; i < screenWidth; ++i)
{
cout << "#";
}
}
int displayOptions(string arr[], int size)
{
int activeOption = 0;
char arbitrary;
menuOptions(arr, size, activeOption);
while (true)
{
if (_kbhit())
{
arbitrary = _getch();
if (arbitrary == 72 && activeOption > 0)
{
activeOption--;
}
else if (arbitrary == 80 && activeOption < size - 1)
{
activeOption++;
}
else if (arbitrary == 13)
{ // Enter key to confirm selection
return activeOption;
break;
}
menuOptions(arr, size, activeOption); // Update the displayed options
}
}
}
void askQuestions()
{
cleanDialogueBox();
enableBGText(BACKGROUND_SILVER);
srand(time(0));
int x = dialogueBoxStart.x + 1; // one for border
int y = dialogueBoxStart.y + 2; // one for border, one for heading, then show the text
int questionsLength = sizeof(questions) / sizeof(Question);
int index = rand() % questionsLength;
string answer;
Question question = questions[index];
gotoxy(x, y); // moving in dialgoue box
string q = "Enemy " + to_string(encounteredEnemies + 1) + ": Answer this question : " + question.question;
int linesTaken = 1;
customPrint(x, y, dialogueBoxWidth - 2, q, linesTaken);
gotoxy(x, y + linesTaken + 1);
cout << "Your answer: ";
gotoxy(x, y + linesTaken + 2);
cin >> answer;
// Check the player's answer and affect health accordingly
cleanDialogueBox();
gotoxy(x, y + linesTaken + 3);
if (toLowerCase(answer) == toLowerCase(question.answer))
{
string str = "Enemy: Correct! You escaped without damage.";
customPrint(x, y, dialogueBoxWidth - 2, str, linesTaken);
}
else
{
string str = "Enemy: Wrong answer! You lost 20 points of health.";
customPrint(x, y, dialogueBoxWidth - 2, str, linesTaken);
superman.health -= 20;
if (superman.health <= 0)
{
system("cls");
cout << "You have lost all of your health." << endl;
cout << "Your health :" << ((superman.health < 0) ? 0 : superman.health) << endl;
cout << "Your score :" << score << endl;
cout << endl;
score = 0;
superman.health = 100;
mazeRows = 8;
mazeCols = 14;
cout << "Press any key continue.";
_getch();
throw runtime_error("BreakException");
// exit(0);
}
// This will be happening only when we have the classic mode of the game.
// if (response == 0)
// {
// gotoxy(0, 0);
// cout << "+=================================+" << endl;
// cout << "| Health Bar = " << superman.health << " |" << endl;
// cout << "| current score = " << score << " |" << endl;
// cout << "+=================================+" << endl;
// }
}
}
void askRiddles()
{
cleanDialogueBox();
enableBGText(BACKGROUND_SILVER);
srand(time(0));
int x = dialogueBoxStart.x + 1; // one for border
int y = dialogueBoxStart.y + 2; // one for border, one for heading, then show the text
int riddlesLength = sizeof(riddles) / sizeof(Riddle);
int index = rand() % riddlesLength;
Riddle riddle = riddles[index];
gotoxy(x, y); // moving in dialgoue box
string q = "Enemy " + to_string(encounteredEnemies + 1) + ": Answer this riddle : " + riddle.riddle;
int linesTaken = 1; // these are number of extra lines to be taken
customPrint(x, y, dialogueBoxWidth - 2, q, linesTaken);
for (int i = 0; i < 4; i++)
{
customPrint(x, y + linesTaken + 1 + i, dialogueBoxWidth - 2, riddle.options[i], linesTaken);
}
gotoxy(x, y + linesTaken + 5); // after 4 options
cout << "Your answer: ";
char answer;
gotoxy(x, y + linesTaken + 6); // after 4 options and 1 'Your answer'
cin >> answer;
// Check the player's answer and affect health accordingly
cleanDialogueBox();
gotoxy(x, y + linesTaken + 7);
if (tolower(answer) == tolower(riddle.answer))
{
string str = "Enemy: Correct! You escaped without damage.";
customPrint(x, y, dialogueBoxWidth - 2, str, linesTaken);
}
else
{
string str = "Enemy: Wrong answer! You lost 20 points of health.";
customPrint(x, y, dialogueBoxWidth - 2, str, linesTaken);
superman.health -= 20;
if (superman.health <= 0)
{
system("cls");
cout << "You have lost all of your health." << endl;
cout << "Your health :" << superman.health << endl;
cout << "Your score :" << score << endl;
cout << endl;
score = 0;
superman.health = 100;
mazeRows = 8;
mazeCols = 14;
cout << "Press any key continue.";
_getch();
throw runtime_error("BreakException");
// exit(0);
}
// This will be happening only when we have the classic mode of the game.
// if (response == 0)
// {
// gotoxy(0, 0);
// cout << "+=================================+" << endl;
// cout << "| Health Bar = " << superman.health << " |" << endl;
// cout << "| current score = " << score << " |" << endl;
// cout << "+=================================+" << endl;
// }
}
}
void chestReward()
{
enableBGText(BACKGROUND_SILVER);
cleanDialogueBox();
int x = dialogueBoxStart.x + 1;
int y = dialogueBoxStart.y + 2; // one for border, one for heading, then show the text
int random = rand() % 4;
gotoxy(x, y);
switch (random)
{
case 0: // IMMUNE
cout << "YOU HAVE FOUND THE IMMUNE POWER UP .";
superman.immune = true;
break;
case 1: // SCORE
cout << "YOUR SCORE INCREASES BY 12.";
score += 12;
break;
case 2: // HEALTH
cout << "YOUR SCORE HEALTH IS RESTORED BY 5.";
superman.health += 5;
break;
case 3:
cout << "YOUR CHEST IS EMPTY PLEASE";
break;
default:
break;
}
}
void encounterEnemy(int playerX, int enemyX, int playerY, int enemyY)
{
// Check if the player encounters the enemy
if (playerX == enemyX && playerY == enemyY && getASCIIAtPosition(enemyX, enemyY) == -24) // -80 was the previous.
{
if (superman.immune == true)
{
superman.immune = false;
return;
}
int random = (levelno > 2) ? rand() % 2 : 3; // if level > 2, there are chances of both questions and riddles else only questions
switch (random)
{
case 0:
askQuestions();
break;
case 1:
askRiddles();
break;
default:
askQuestions();
break;
}
displayHealth();
}
}
void encounterChest(int playerX, int chestX, int playerY, int chestY)
{
if (playerX == chestX && playerY == chestY && getCharacterAtPosition(chestX, chestY) == '+')
{
chestReward();
displayHealth();
}
}
void displayChests()
{
for (int i = 0; i < numberOfChests; i++)
{
chestsArray[i].character = '+';
}
srand(static_cast<unsigned int>(time(0)));
while (true)
{ // doing the necessary modifidcations so that the chests do not get out of the maze
for (int i = 0; i < numberOfChests; i++)
{
chestsArray[i].x = (5 + (rand() % mazeCols));
chestsArray[i].y = (5 + (rand() % mazeCols));
}
bool isEncounter = false;
// checking enemy correctness
for (int i = 0; i < numberOfChests; i++)
if (chestCoordiCorrectness(chestsArray[i].x, chestsArray[i].y))
{
isEncounter = true;
break;
}
if (isEncounter)
break;
}
for (int i = 0; i < numberOfChests; i++)
{
gotoxy(chestsArray[i].x, chestsArray[i].y);
cout << chestsArray[i].character;
}
}
void displayEnemies()
{
for (int i = 0; i < numberOfEnemies; i++)
enemiesArray[i].enemy = 232;
srand(static_cast<unsigned int>(time(0)));
int enemyX;
int enemyY;
int i = 0;
while (i < numberOfEnemies)
{
enemyX = mazeStart.x + 2 + (rand() % mazeCols - 1); // 2 is to prevent from the start of the wall and 1 is from the end
enemyY = mazeStart.y + 2 + (rand() % mazeRows - 1);
if (enemyCoordiCorrectness(enemyX, enemyY))
continue;
enemiesArray[i].x = enemyX;
enemiesArray[i].y = enemyY;
i++;
}
for (int i = 0; i < numberOfEnemies; i++)
{
gotoxy(enemiesArray[i].x, enemiesArray[i].y);
cout << enemiesArray[i].enemy;
}
}
void displayDialogueBox()
{
gotoxy(dialogueBoxStart.x, dialogueBoxStart.y);
// Draw the top border
char topAciiCharacter = 203;
for (int i = 0; i < dialogueBoxWidth; i++)
{
cout << topAciiCharacter;
}
// Draw the sides
char leftAciiCharacter = 204;
char rightAciiCharacter = 185;
for (int i = 1; i < dialogueBoxHeight - 1; i++)
{
gotoxy(dialogueBoxStart.x, dialogueBoxStart.y + i);
cout << leftAciiCharacter;
gotoxy(dialogueBoxStart.x + dialogueBoxWidth - 1, dialogueBoxStart.y + i);
cout << rightAciiCharacter;
}
// Draw the bottom border
char bottomAciiCharacter = 202;
gotoxy(dialogueBoxStart.x, dialogueBoxStart.y + dialogueBoxHeight - 1);
for (int i = 0; i < dialogueBoxWidth; i++)
{
cout << bottomAciiCharacter;
}
string dialogue = "-----Dialogue Box-----";
int x = (dialogueBoxStart.x + (dialogueBoxStart.x + dialogueBoxWidth)) / 2 - dialogue.length() / 2;
gotoxy(x, dialogueBoxStart.y + 1);
cout << dialogue;
}
void displayMaze(const vector<vector<char>> &maze)
{
int i = 0;
for (const auto &row : maze)
{
gotoxy(mazeStart.x, mazeStart.y + i);
for (char cell : row)
{
cout << cell << "";
}
cout << " " << endl;
i++;
}
displayEnemies();
displayChests();
}
void displayGame(const vector<vector<char>> &maze)
{
displayMaze(maze);
displayDialogueBox();
displayHealth();
// displayScreenBorders(); // not working correctly right now
}