-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
369 lines (312 loc) · 9.76 KB
/
main.cpp
File metadata and controls
369 lines (312 loc) · 9.76 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
#include <iostream>
#include <windows.h>
#include <chrono>
#include <thread>
#include <conio.h>
#include <fstream>
using namespace std::this_thread;
using namespace std::chrono_literals;
using namespace std;
enum Input {
UP,
DOWN,
LEFT,
RIGHT,
EXIT,
NONE
};
struct Player {
char symbol = 'o';
string name;
int position[2] = { 1, 1 };
int moves = 0;
int totalMoves = 0;
};
struct Game {
bool inProgress = true;
int mazeSize = 20;
int level = 1;
int numOfLevels = 5;
Input input;
Player player;
char** maze;
};
char** loadMaze(const std::string& filename) {
ifstream file(filename);
vector<string> lines;
string line;
// Read each line from the file into the vector
while (getline(file, line)) {
lines.push_back(line);
}
file.close();
// Determine dimensions of the 2D array
const int rows = lines.size();
const int cols = rows > 0 ? lines[0].size() : 0;
// Create the 2D array
const auto maze = new char*[rows];
for (int i = 0; i < rows; ++i) {
maze[i] = new char[cols];
for (int j = 0; j < cols; ++j) {
if (j < lines[i].size()) {
maze[i][j] = lines[i][j];
} else {
maze[i][j] = ' '; // Fill with space if the line is shorter
}
}
}
return maze;
}
// Deletes the dynamically allocated memory to avoid memory leak
void deleteMaze(char** maze) {
for (int i = 0; i < 20; ++i) {
delete[] maze[i];
}
delete[] maze;
}
Input getInput() {
if (GetKeyState(VK_ESCAPE) & 0x8000) {
return EXIT;
} else if (GetKeyState(VK_UP) & 0x8000) {
return UP;
} else if (GetKeyState(VK_DOWN) & 0x8000) {
return DOWN;
} else if (GetKeyState(VK_LEFT) & 0x8000) {
return LEFT;
} else if (GetKeyState(VK_RIGHT) & 0x8000) {
return RIGHT;
}
return NONE;
}
void move(char** maze, Player &player, const Input direction) {
switch (direction) {
case UP:
if (maze[player.position[0]-1][player.position[1]] != '#') {
maze[player.position[0]-1][player.position[1]] = player.symbol;
maze[player.position[0]][player.position[1]] = ' ';
player.position[0]--;
}
break;
case DOWN:
if (maze[player.position[0]+1][player.position[1]] != '#') {
maze[player.position[0]+1][player.position[1]] = player.symbol;
maze[player.position[0]][player.position[1]] = ' ';
player.position[0]++;
}
break;
case LEFT:
if (maze[player.position[0]][player.position[1]-1] != '#') {
maze[player.position[0]][player.position[1]-1] = player.symbol;
maze[player.position[0]][player.position[1]] = ' ';
player.position[1]--;
}
break;
case RIGHT:
if (maze[player.position[0]][player.position[1]+1] != '#') {
maze[player.position[0]][player.position[1]+1] = player.symbol;
maze[player.position[0]][player.position[1]] = ' ';
player.position[1]++;
}
break;
default:
break;
}
}
void displayTitle() {
// Output title
cout << "\033[32m"; // Set colour to green
cout << ",--. ,--. ,----. " << endl;
cout << "| `.' |,--,--,-----.,---. ' .-./ ,--,--,--,--,--.,---. " << endl;
cout << "| |'.'| ' ,-. `-. /| .-. : | | .---' ,-. | | .-. : " << endl;
cout << R"(| | | \ '-' |/ `-\ --. ' '--' \ '-' | | | \ --. )" << endl;
cout << "`--' `--'`--`--`-----'`----' `------' `--`--`--`--`--'`----' " << endl;
cout << "\033[0m"; // Set colour to white
}
// Outputs maze to console
void displayMaze(const Game &game) {
// Clear console
system("cls");
displayTitle();
// Output level number
cout << "\033[92mLevel: " << game.level << "\033[0m" << endl;
// Output active player's name
cout << "\033[92mPlayer: " << game.player.name << "\033[0m" << endl;
// Output maze
for(int a = 0; a < game.mazeSize; a++)
{
for(int b = 0; b < game.mazeSize; b++)
{
cout << game.maze[a][b] << " ";
}
cout << endl;
}
// Output info
cout << "\033[92m"; // Set colour to green
cout << "Moves made: " << game.player.moves << endl;
cout << "\033[0m"; // Set colour to white
}
void displayReview(int level, int moves) {
system("cls"); // Clear the console
cout << "\033[92m"; // Set colour to green
cout << "You completed level: " << level << endl;
cout << "Moves made: " << moves << endl;
cout << "\033[0m"; // Set colour to white
system("pause");
}
Game gameUpdate(Game &game) {
game.input = getInput();
if (game.input != NONE) {
move(game.maze, game.player, game.input);
game.player.moves++;
displayMaze(game);
}
// Check to see if user has reached the end of the maze
if (game.player.position[0] == 18 && game.player.position[1] == 18) {
displayReview(game.level, game.player.moves);
game.player.totalMoves += game.player.moves;
game.level++;
// Check if there is another level and load it
if (game.level <= game.numOfLevels) {
game.player.moves = 0;
game.maze = loadMaze("maze"+ to_string(game.level) +".txt");
game.player.position[0] = 1;
game.player.position[1] = 1;
displayMaze(game);
}
}
// Exit game and return to menu
if (game.input == EXIT) {
game.inProgress = false;
system("cls");
return game;
}
return game;
}
void sortLeaderboard(vector<string>& leaderboard) {
int n = leaderboard.size();
int score1 = 0, score2 = 0;
// Bubble sort the leaderboard
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
// Extract the scores from each line as an integer
score1 = 0;
score2 = 0;
stringstream number1;
number1 << leaderboard[j];
number1 >> score1;
stringstream number2;
number2 << leaderboard[j + 1];
number2 >> score2;
if (score1 > score2) {
swap(leaderboard[j], leaderboard[j + 1]);
}
}
}
}
void displayLeaderboard(int totalMoves, const string& player) {
// Write user's most recent score to the file
if (player != "null") {
fstream file("leaderboard.txt", ios::app);
file << totalMoves << " - " << player << endl;
file.close();
}
// Open the file in read mode
ifstream leaderboard("leaderboard.txt");
vector<string> lines;
string line;
// Read each line from the file into the vector
while (getline(leaderboard, line)) {
lines.push_back(line);
}
leaderboard.close();
// Sort the leaderboard by moves made
sortLeaderboard(lines);
cout << "\033[92m";
cout << "Leaderboard:" << endl;
for (int i = 0; i < lines.size(); i++) {
cout << i+1 << ". " << lines[i] << endl;
}
cout << "\033[0m";
};
void endGame(Game &game) {
system("cls");
cout << "\033[92m"; // Set colour to green
cout << "Congratulations " << game.player.name <<"!" << endl;
cout << "You have completed the maze game." << endl << endl;
cout << "Statistics:" << endl;
cout << "Total moves made: " << game.player.totalMoves << endl << endl;
cout << "\033[0m";
displayLeaderboard(game.player.totalMoves, game.player.name);
deleteMaze(game.maze);
game.inProgress = false;
system("pause");
system("cls");
}
void introMenu() {
displayTitle();
cout << "\033[92m";
cout << "1. Start Game" << endl;
cout << "2. Rules & Controls" << endl;
cout << "3. Leaderboard" << endl;
cout << "4. Exit" << endl;
cout << "Enter Your Choice: ";
cout << "\033[0m";
int choice;
cin >> choice;
system("cls");
switch (choice) {
case 1:
break;
case 2:
cout << "\033[92m";
cout << "Rules" << endl << endl;
cout << "The aim of the game is to reach the end of the maze (marked by an 'x') in the fewest moves possible." << endl;
cout << "Your final score is the total number of moves made throughout all of the mazes (Lower is better)." << endl << endl;
cout << "Controls" << endl << endl;
cout << "Movement: Arrow Keys" << endl;
cout << "End Game & Return to Menu: Esc" << endl << endl;
cout << "\033[0m";
system("pause");
system("cls");
introMenu();
break;
case 3:
system("cls");
displayLeaderboard(0, "null");
system("pause");
system("cls");
introMenu();
break;
case 4:
exit(0);
default:
cout << "\033[1;91m";
cout << "Invalid Choice!" << endl;
cout << "\033[0m";
system("pause");
system("cls");
introMenu();
break;
}
};
int main() {
while (true) {
introMenu();
Game game;
// Load the first maze from the file
game.maze = loadMaze("maze"+ to_string(game.level) +".txt");
// Get user's name
cout << "\033[92m";
cout << "Enter your name: ";
cin >> game.player.name;
displayMaze(game);
while (game.inProgress) {
if (game.level > game.numOfLevels) {
endGame(game);
} else {
gameUpdate(game);
sleep_for(0.1s);
}
}
}
}