-
Notifications
You must be signed in to change notification settings - Fork 0
/
MazeSolver.cpp
226 lines (203 loc) · 4.92 KB
/
MazeSolver.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
/*
File Name: PlayList.cpp
Date: November 6, 2018
Name: Nancy Yoon
Class: CSCI 235 - Fall 2018
[Project 4] MazeSolver
: maze solver will read the maze from an input file,
find a path that leads to the exit and print the solution to the standard out
(solution = maze with highlighted path from start position to exit)
*/
#include "MazeSolver.h"
#include <cctype>
#include <stack>
MazeSolver::MazeSolver(std::string input_file)
{
std::ifstream in_stream;
in_stream.open(input_file);
if(in_stream.fail())
{
std::cout << "Cannot read from input_file" << std::endl;
exit(1);
}
else
{
in_stream >> maze_rows_ >> maze_columns_;
initializeMaze(maze_rows_, maze_columns_);
fillMaze(in_stream);
initializeSolution();
maze_ready = true;
}//end of else loop
in_stream.close();
}// end of constructor
MazeSolver::~MazeSolver()
{
if(maze_ready != true)
{
delete [] solution_;
delete [] maze_;
}
else
{
delete [] *solution_;
delete [] solution_;
delete [] *maze_;
delete [] maze_;
}
}//end of destructor
bool MazeSolver::mazeIsReady()
{
if(maze_ready == true)
return true;
return false;
}//end of mazeIsReady
//STUB
bool MazeSolver::solveMaze()
{
Position current_position;
current_position.row = 0;
current_position.column = 0;
while(!backtrack_stack_.empty())
{
if(maze_[current_position.row][current_position.column] == '$')
{
std::cout << "Found exit!!!" << std::endl;
return true;
}
else if(extendPath(current_position))
{
current_position = backtrack_stack_.top();
}
else
{
maze_[current_position.row][current_position.column] = 'X';
solution_[current_position.row][current_position.column] = '@';
backtrack_stack_.pop();
if(!backtrack_stack_.empty())
{
current_position = backtrack_stack_.top();
}
else
{
std::cout << "This maze has no solution." << std::endl;
return false;
}
}
}
return true;
}//end of solveMaze
void MazeSolver::printSolution()
{
std::cout << "The solution to this mase is:" << std::endl;
for(int i=0; i<maze_rows_; i++)
{
for(int j=0; j<maze_columns_; j++)
{
std::cout << solution_[i][j] << " ";
}
std::cout << std::endl;
}
}//end of printSolution
//PRIVATE
void MazeSolver::initializeMaze(int rows, int columns)
{
if(rows >= 0 && columns >= 0)
{
maze_ = new char *[rows];
for(int i=0; i<rows; i++)
maze_[i] = new char [columns];
}//end of if condition
}//end of initializeMaze
void MazeSolver::fillMaze(std::ifstream& input_stream)
{
for(int i=0; i<maze_rows_; i++)
{
for(int j=0; j<maze_columns_; j++)
{
input_stream >> maze_[i][j];
}
}//end of for loop
}//end of fillMaze
void MazeSolver::initializeSolution()
{
copyMazetoSolution();
Position viable_paths;
solution_[0][0] = '>';
viable_paths.row = 0;
viable_paths.column = 0;
backtrack_stack_.push(viable_paths);
}//end of initializeSolution
void MazeSolver::copyMazetoSolution()
{
solution_ = new char *[maze_rows_];
for(int i=0; i<maze_rows_; i++)
solution_[i] = new char [maze_columns_];
for(int i=0; i<maze_rows_; i++)
{
for(int j=0; j<maze_columns_; j++)
{
solution_[i][j] = maze_[i][j];
}
}//end of for loop
}//end of copyMazetoSolution
bool MazeSolver::extendPath(Position current_position)
{
bool extended = false;
if(isExtensible(current_position, SOUTH))
{
backtrack_stack_.push(getNewPosition(current_position, SOUTH));
if(maze_[current_position.row+1][current_position.column] == '_')
{
solution_[current_position.row+1][current_position.column] = '>';
}
else
{
solution_[current_position.row+1][current_position.column] = '$';
}
extended = true;
}
if(isExtensible(current_position, EAST))
{
backtrack_stack_.push(getNewPosition(current_position, EAST));
if(maze_[current_position.row][current_position.column+1] == '_')
{
solution_[current_position.row][current_position.column+1] = '>';
}
else
{
solution_[current_position.row][current_position.column+1] = '$';
}
extended = true;
}
return extended;
}//end of extendPath
Position MazeSolver::getNewPosition(Position old_position, direction dir)
{
Position newPosition;
if(dir == SOUTH)
{
newPosition.row = old_position.row+1;
newPosition.column = old_position.column;
}
if(dir == EAST)
{
newPosition.row = old_position.row;
newPosition.column = old_position.column+1;
}
return newPosition;
}//end of getNewPosition
bool MazeSolver::isExtensible(Position current_position, direction dir)
{
bool extensible = false;
if(dir == SOUTH && current_position.row < maze_rows_ -1)
{
if(maze_[current_position.row+1][current_position.column] == '_' || maze_[current_position.row+1][current_position.column] == '$')
extensible = true;
}
if(dir == EAST && current_position.column < maze_columns_ -1)
{
if(maze_[current_position.row][current_position.column+1] == '_' || maze_[current_position.row][current_position.column+1] == '$')
extensible = true;
}
return extensible;
}//end of isExtensible