-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreature.cpp
More file actions
199 lines (124 loc) · 4.09 KB
/
creature.cpp
File metadata and controls
199 lines (124 loc) · 4.09 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
// creature.cpp
// Implementations for Creature class
// Author: Juan Arias
//
// The Creature class represents something that can move through the Maze
// class, marking locations as visited or part of the path to the exit, and
// returns a string indicating its path in North West South East.
#include "creature.h"
// Friend function
// Overloaded output operator
// Prints the position of the Creature in the format: C(x, y)
std::ostream& operator<<(std::ostream& out, const Creature& creat) {
out << "C(" << creat.row << ", " << creat.col << ')';
return out;
}
// Constructs Creature at position in Maze,
// assumes position is valid open space
// Initialies mazePtr to nullptr, row to parameter startRow &
// col to parameter startCol
Creature::Creature(int startRow, int startCol)
:mazePtr(nullptr), row(startRow), col(startCol) {}
// Destroys Creature
Creature::~Creature() {}
// Returns a string in the form of NNEEN
// (where N means North, E means East, etc)
// that will let the creature get to the exit
// if creature cannot get to the exit, returns "X"
// if constructed at exit, returns ""
// Assumes maze has not been marked
std::string Creature::Solve(Maze& maze) {
if (maze.IsExit(row, col)) {
maze.MarkAsPath(row, col);
return "";
}
mazePtr = &maze;
std::string path(goNorth());
path = (path[0] == NO_PATH) ? goWest() : path;
path = (path[0] == NO_PATH) ? goEast() : path;
path = (path[0] == NO_PATH) ? goSouth() : path;
if (path[0] == NO_PATH) {
maze.MarkAsVisited(row, col);
}
return path;
}
// Attempts to go North in Maze, updates position &
// returns NORTH if successful, otherwise returns NO_PATH
std::string Creature::goNorth() {
if (mazePtr->IsClear(row - 1, col)) {
mazePtr->MarkAsPath(row--, col);
if (mazePtr->IsExit(row, col)) {
mazePtr->MarkAsPath(row, col);
return std::string(1, NORTH);
}
std::string newPath(goNorth());
newPath = (newPath[0] == NO_PATH) ? goWest() : newPath;
newPath = (newPath[0] == NO_PATH) ? goEast() : newPath;
if (newPath[0] == NO_PATH) {
mazePtr->MarkAsVisited(row++, col);
return newPath;
}
return newPath.insert(0, 1, NORTH);
}
return std::string(1, NO_PATH);
}
// Attempts to go South in Maze, updates position &
// returns SOUTH if successful, otherwise NO_PATH
std::string Creature::goSouth() {
if (mazePtr->IsClear(row + 1, col)) {
mazePtr->MarkAsPath(row++, col);
if (mazePtr->IsExit(row, col)) {
mazePtr->MarkAsPath(row, col);
return std::string(1, SOUTH);
}
std::string newPath(goSouth());
newPath = (newPath[0] == NO_PATH) ? goWest() : newPath;
newPath = (newPath[0] == NO_PATH) ? goEast() : newPath;
if (newPath[0] == NO_PATH) {
mazePtr->MarkAsVisited(row--, col);
return newPath;
}
return newPath.insert(0, 1, SOUTH);
}
return std::string(1, NO_PATH);
}
// Attempts to go East in Maze, updates position &
// returns EAST if successful, otherwise returns NO_PATH
std::string Creature::goEast() {
if (mazePtr->IsClear(row, col + 1)) {
mazePtr->MarkAsPath(row, col++);
if (mazePtr->IsExit(row, col)) {
mazePtr->MarkAsPath(row, col);
return std::string(1, EAST);
}
std::string newPath(goEast());
newPath = (newPath[0] == NO_PATH) ? goNorth() : newPath;
newPath = (newPath[0] == NO_PATH) ? goSouth() : newPath;
if (newPath[0] == NO_PATH) {
mazePtr->MarkAsVisited(row, col--);
return newPath;
}
return newPath.insert(0, 1, EAST);
}
return std::string(1, NO_PATH);
}
// Attempts to go West in Maze, updates position &
// returns WEST if successful, otherwise returns NO_PATH
std::string Creature::goWest() {
if (mazePtr->IsClear(row, col - 1)) {
mazePtr->MarkAsPath(row, col--);
if (mazePtr->IsExit(row, col)) {
mazePtr->MarkAsPath(row, col);
return std::string (1, WEST);
}
std::string newPath(goWest());
newPath = (newPath[0] == NO_PATH) ? goNorth() : newPath;
newPath = (newPath[0] == NO_PATH) ? goSouth() : newPath;
if (newPath[0] == NO_PATH) {
mazePtr->MarkAsVisited(row, col++);
return newPath;
}
return newPath.insert(0, 1, WEST);
}
return std::string(1, NO_PATH);
}