-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaze.cpp
More file actions
163 lines (132 loc) · 3.6 KB
/
Maze.cpp
File metadata and controls
163 lines (132 loc) · 3.6 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
#include "Maze.h"
#include "Color.h"
#include "Rect.h"
#include <windows.h>
#include <iostream>
using namespace std;
Maze::Maze(Matrix* mz)
{
maze = mz;
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(1, 1);
return done;
}
bool Maze::traverse(int row, int col)
{
bool done = false; //assume we are not done unless proven otherwise
//test that the current grid location is a space (i.e. not a wall or already tried)
if (maze->getElement(row,col) == SPACE)
{
//now it has been tried so mark it as tried
maze->setElement(row,col,TRIED);
Sleep(75); //slow down the maze traversal
gui->update();
//check to see if we have arrived at the bottom right corner of the maze
int height = maze->getNumRows();
int width = maze->getNumCols();
if (height == row && width == col)
{
done = true;
}
else
{
//DO THIS
//make recursive calls that consider all four orthogonal directions
//basically, we will try all possible paths until a solution is found
//IMPORTANT!!
//don't use row++ or column++ use row + 1 or col + 1, etc.
//IMPORTANT: make use of the boolean that is returned every time you call traverse
done = traverse(row+1,col); //UP
if (done != TRUE)
{
done = traverse(row,col+1); //RIGHT
}
if (done != TRUE)
{
done = traverse(row-1,col); //DOWN
}
if (done != TRUE)
{
done = traverse(row,col-1); //LEFT
}
}
//if we are done, on the way back recursively we must mark the path that we took as the solution path
if (done)
{
//mark the path taken as the solution path
maze->setElement(row,col,PATH);
gui->update();
}
//backtrack
else
{
maze->setElement(row,col,BACKTRACK);
Sleep(75);
gui->update();
}
}
return done;
}
void Maze::mouseClicked(int x, int y)
{}
void Maze::draw(wxDC& dc, 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(dc, x_pixel, y_pixel);
}
else if (val == SPACE)
{
whiteRect.draw(dc, x_pixel, y_pixel);
}
else if (val == TRIED)
{
blueRect.draw(dc, x_pixel, y_pixel);
}
else if (val == BACKTRACK)
{
redRect.draw(dc, x_pixel, y_pixel);
}
else if (val == PATH)
{
greenRect.draw(dc, x_pixel, y_pixel);
}
}
}
}