-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmaze-game.cpp
More file actions
208 lines (182 loc) · 5.12 KB
/
maze-game.cpp
File metadata and controls
208 lines (182 loc) · 5.12 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
#include "engine.h"
#include <fstream>
class MazeGame : public Engine
{
protected:
int player_x;
int player_y;
int goal_x;
int goal_y;
string ICON_PLAYER = "🧍";
string ICON_GOAL = "🏁";
string ICON_BREADCRUMB = "░░";
string ICON_WALL = "🌲";
const int MAZE_HEIGHT = 11;
const int MAZE_WIDTH = 27;
// a vector of strings to store the map data
// maze_data[i][j] gives us access to
// i'th row, j'th column
// or coordinate (j, i) i.e., x = column, y = row
vector<string> maze_data;
void setup()
{
// read the map data from file
string line;
ifstream maze_file("maze-data.txt");
if (maze_file.is_open())
{
while (getline(maze_file,line))
{
maze_data.push_back(line);
}
maze_file.close();
}
// find the player's starting position
// and the position of the goal
for (int i = 0; i < MAZE_HEIGHT; i++)
{
for (int j = 0; j < MAZE_WIDTH; j++)
{
char c = maze_data[i][j];
switch (c)
{
case 'g':
goal_x = j;
goal_y = i;
break;
case 's':
player_x = j;
player_y = i;
}
}
}
createCanvas(MAZE_WIDTH, MAZE_HEIGHT);
}
void keyPressed(int keyCode)
{
int x1 = player_x;
int y1 = player_y;
switch (keyCode)
{
case 'd':
x1 = player_x + 1;
break;
case 'a':
x1 = player_x - 1;
break;
case 'w':
y1 = player_y - 1;
break;
case 's':
y1 = player_y + 1;
break;
}
// check if this move is not within the bound and also not a wall
if (x1 < MAZE_WIDTH && y1 < MAZE_HEIGHT && x1 >= 0 && y1 >= 0 && maze_data[y1][x1] != 'w')
{
player_x = x1;
player_y = y1;
// mark this location as 'visited'
maze_data[y1][x1] = 'v';
}
}
void draw()
{
clear();
for (int i = 0 ; i < MAZE_HEIGHT; i++)
{
for (int j = 0 ; j < MAZE_WIDTH ; j++)
{
char d = maze_data[i][j];
switch (d)
{
case 'w':
stroke(ICON_WALL);
point(j, i);
break;
case 'v':
stroke(ICON_BREADCRUMB);
point(j, i);
break;
}
}
}
stroke(ICON_GOAL);
point(goal_x, goal_y);
stroke(ICON_PLAYER);
point(player_x, player_y);
stroke(BLOCK);
}
void console()
{
if (player_x == goal_x && player_y == goal_y)
{
cout << "You won!!" << endl;
quit();
}
}
};
class MazeGameSolver : public MazeGame
{
bool isGoalReached = false;
void setup()
{
// call the parent's setup() to read the map data
MazeGame::setup();
// explore the map
explore(player_x, player_y);
noLoop();
}
// explore the map until the goal is reached using a recursive
// depth-first-search algorithm
void explore(int x, int y)
{
if (isGoalReached)
{
// BASE CONDITION: if the goal is already reached.
return;
}
else if (x >= MAZE_WIDTH || y >= MAZE_HEIGHT || x < 0 || y < 0)
// BASE CONDITION: if it is out of bound.
{
return;
}
else if (maze_data[y][x] == 'w' || maze_data[y][x] == 'v')
{
// BASE CONDITION: if the location is a wall or is already visited.
return;
}
else if (maze_data[y][x] == 'g')
{
// BASE CONDITION: if the location is the goal.
string s;
cout <<"GOAL!" << endl;
isGoalReached = true;
return;
}
else if (!isGoalReached)
{
// mark this cell as visited
maze_data[y][x] = 'v';
// update the location of the player
player_x = x;
player_y = y;
// redraw the canvas immedidately
redraw();
// sleep a bit so that the animation does not move too fast
sleep(100);
// recursively explore the four adjacent directions
explore(x, y+1);
explore(x, y-1);
explore(x+1, y);
explore(x-1, y);
}
}
};
int main()
{
Menu menu;
menu.add(new MazeGame(), "MazeGame");
menu.add(new MazeGameSolver(), "MazeGameSolver");
menu.play();
}