-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeWalker.cpp
More file actions
215 lines (194 loc) · 4.19 KB
/
MazeWalker.cpp
File metadata and controls
215 lines (194 loc) · 4.19 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
/* @Author: Anna Fritz
* @File: MazeWalker.cpp
* @Date: March 27, 2019
* @Brief: This is the implementation file for the MazeWalker class
*/
#include "MazeWalker.h"
/**
* @pre The mazePtr pointer is to a valid maze.
* @post A maze walker if created ready to traverse the maze from the start position is the specified order.
*/
MazeWalker::MazeWalker(const char* const* mazePtr, int startRow, int startCol, int rows, int cols)
{
m_maze = mazePtr;
m_startRow = startRow;
m_startCol = startCol;
m_curRow = m_startRow;
m_curCol = m_startCol;
m_curStep = 0;
m_rows = rows;
m_cols = cols;
cout << "Starting Position: " << m_startRow << ", " << m_startCol << endl;
cout << "Size: " << m_rows << ", " << m_cols << endl;
bool possibleExit = false;
for (int i = 0; i < m_rows; i++)
{
for (int j=0; j < m_cols; j++)
{
if (m_maze[i][j] == 'E')
{
possibleExit = true;
}
}
}
if (possibleExit == false)
{
throw MazeCreationExecption("No way out!");
}
m_visited = new int* [m_rows];
for (int i = 0; i < m_rows; i++)
{
m_visited[i] = new int [m_cols];
}
for (int i = 0; i < m_rows; i++)
{
for (int j = 0; j < m_cols; j++)
{
if (i==m_startRow && j==m_startCol)
{
m_visited[i][j] = 1;
}
else
{
m_visited[i][j] = 0;
}
}
}
}
/**
*
* @pre The visited array still exists and has the same dimensions (rows X cols)
* @post The visited array is deallocated
*/
MazeWalker::~MazeWalker()
{
for (int i = 0; i < m_rows; i++)
{
delete m_visited[i];
}
delete m_visited;
}
/**
* @pre The maze is a valid maze.
* @post The maze is traversed until the end is reached or all moves are exhausted.
* @return true if an exit was reached, false otherwise
*/
bool MazeWalker::walkMaze()
{
int startrow = m_curRow;
int startcol = m_curCol;
bool traveled = mazeTravels(startrow, startcol);
if (traveled == true)
{
return (true);
}
return (false);
}
/**
* @return A const pointer to the visited array. (A pointer that cannot change the array)
*/
const int* const* MazeWalker::getVisited() const
{
const int* const* temp = m_visited;
return (temp);
}
/**
* @return number of rows in maze
*/
int MazeWalker::getVisitedRows() const
{
return (m_rows);
}
/**
* @return number of cols in maze
*/
int MazeWalker::getVisitedCols() const
{
return (m_cols);
}
/**
* @return A const pointer to the maze. (A pointer that cannot change the array)
*/
const char* const* MazeWalker::getMaze() const
{
const char* const* temp = m_maze;
return (temp);
}
/**
* @returns If the current position is the exit, true is returned. False is returned otherwise.
*/
bool MazeWalker::isGoalReached() const
{
if (m_maze[m_curRow][m_curCol] == 'E')
{
return (true);
}
return (false);
}
bool MazeWalker::canMove (int row, int col)
{
if (row >= 0 && col >= 0 && row < m_rows && col < m_cols && m_visited[row][col] == 0)
{
if (m_maze[row][col] == 'P' || m_maze[row][col] == 'E')
{
return (true);
}
}
return (false);
}
bool MazeWalker::mazeTravels(int row, int col)
{
if (row >= 0 && row < m_rows && col >= 0 && col < m_cols)
{
m_curStep++;
m_visited[row][col] = m_curStep;
}
else
{
m_curStep--;
return(false);
}
// if solution is reached
if (m_maze[row][col] == 'E')
{
m_visited[row][col] = m_curStep;
m_curRow = row;
m_curCol = col;
return true;
}
// look up
if (canMove(row-1, col) == true)
{
if(mazeTravels(row-1, col))
{
return true;
}
}
// look right
if (canMove(row, col+1) == true)
{
if(mazeTravels(row, col+1))
{
return true;
}
}
// look down
if (canMove(row+1, col) == true)
{
if (mazeTravels(row+1, col))
{
return true;
}
}
//look left
if (canMove(row, col-1) == true)
{
if (mazeTravels(row, col-1))
{
return true;
}
}
m_curStep--;
m_visited[row][col] = 0;
return false;
}