-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maze.cpp
249 lines (178 loc) · 5.08 KB
/
Maze.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
#include "Maze.h"
#include "Color.h"
#include "Rect.h"
#include <windows.h> //for the sleep function
#include <iostream>
using namespace std;
#define SLEEP_TIME 25
Maze::Maze(Matrix* mz)
{
maze = mz;
height = maze->getNumRows();
width = maze->getNumCols();
WALL = 0;
SPACE = 1;
TRIED = 2;
BACKTRACK = 3;
PATH = 4;
}
Maze::~Maze()
{
delete maze;
}
void Maze::addListener(Update* g)
{
gui = g;
}
bool Maze::solve()
{
bool done = traverse();
return done;
}
//backing through the maze, setting the color to BACKTRACK
Cell* Maze::processBackTrack(StackLinked<Cell>* stack)
{
//DO THIS
//you may need to back through several cells
Cell* top_cell = stack->peek();
//top_cell is NULL if the stack is empty
//top_cell's direction is DEAD_END if you need to keep backtracking
while (top_cell->dir == DEAD_END) //need to back track
{
//remove the cell and set the maze location to BACKTRACK (the maze is a Matrix)
top_cell->pop();
int row = top_cell->getRow();
int col = top_cell->getCol();
//look at the next cell
top_cell = stack->peek();
Sleep(SLEEP_TIME); //slow down the maze traversal
gui->update(); //update whenever the color of a cell has been changed
}
return top_cell;
}
bool Maze::isSolved(Cell* curr_cell, StackLinked<Cell>* stack)
{
//DO THIS
//get row and col from curr_cell
int row = curr_cell->getRow();
int col = curr_cell->getCol();
//have you solved the maze? (check that we are at the bottom right maze location and that it is a SPACE
if (row == 21 && col == 31 && )
{
//set the maze location to TRIED
//push curr_cell
gui->update();
//return the appropriate boolean
}
//return the appropriate boolean
return fasle;
}
//backing through the maze, setting the solution color to PATH
void Maze::processSolution(StackLinked<Cell>* stack)
{
//DO THIS
//the stack has the solution path stored
while( )
{
//get the next cell from the stack
//update the maze location to PATH
gui->update();
}
}
bool Maze::traverse()
{
//DO THIS
//complete several sections in this method
bool done = false; //assume we are not done unless proven otherwise
StackLinked<Cell> stack;
maze->setElement(1, 1, TRIED);
gui->update();
Cell* start_cell = new Cell(1, 1);
stack.push(start_cell); //start from the top left corner
while(!stack.isEmpty())
{
Cell* top_cell = processBackTrack(&stack);
if (top_cell == NULL) break; //no solution (back tracked all the way to the beginning)
//call a method in the Cell class to give you a new Cell in a new direction relative to top_cell (initially, DOWN)
//DO THIS
Cell* curr_cell =
//does this new Cell solve the maze?
done = isSolved(curr_cell, &stack);
if (done) break;
//DO THIS
//get the row and col from curr_cell
int row =
int col =
//check that the current maze location corresponds to SPACE, otherwise delete it
if ( )
{
//update the maze location to TRIED
//put the cell on the stack (move forward through the maze)
Sleep(SLEEP_TIME); //slow down the maze traversal
gui->update();
}
else //look for a different route
{
//DO THIS
//delete the cell
}
}
//did we make it to the bottom right?
if (done)
{
processSolution(&stack);
}
else
{
cout << "No solution." << endl;
}
return done;
}
void Maze::mouseClicked(int x, int y)
{}
void Maze::draw(Cairo::RefPtr<Cairo::Context> cr, int width, int height)
{
int rows = maze->getNumRows();
int cols = maze->getNumCols();
int cell_width = (int) (((double) width)/cols + 0.5);
int cell_height = (int) (((double) height)/rows + 0.5);
Color red(1.0, 0.0, 0.0);
Rect redRect(&red, cell_width, cell_height);
Color green(0.0, 1.0, 0.0);
Rect greenRect(&green, cell_width, cell_height);
Color blue(0.0, 0.0, 1.0);
Rect blueRect(&blue, cell_width, cell_height);
Color white(1.0, 1.0, 1.0);
Rect whiteRect(&white, cell_width, cell_height);
Color black(0.0, 0.0, 0.0);
Rect blackRect(&black, cell_width, cell_height);
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= cols; j++)
{
int val = (int) maze->getElement(i, j);
int x_pixel = (j - 1) * cell_width + cell_width/2;
int y_pixel = (i - 1) * cell_height + cell_height/2;
if (val == WALL)
{
blackRect.draw(cr, x_pixel, y_pixel);
}
else if (val == SPACE)
{
whiteRect.draw(cr, x_pixel, y_pixel);
}
else if (val == TRIED)
{
blueRect.draw(cr, x_pixel, y_pixel);
}
else if (val == BACKTRACK)
{
redRect.draw(cr, x_pixel, y_pixel);
}
else if (val == PATH)
{
greenRect.draw(cr, x_pixel, y_pixel);
}
}
}
}