-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHandrailAlgo.cpp
240 lines (216 loc) · 7 KB
/
HandrailAlgo.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
#include <iostream>
#include <cstdlib>
using namespace std;
void handrailAlgo(int &startCellX, int &startCellY); //pain
void moveToCell(int ¤tCellX, int ¤tCellY, int nextCellX, int nextCellY); //(Ximena)
bool isValidMove(int currentCellX, int currentCellY, int facingDir); //returns false if given move would go into wall(Ash, done)
void makeNextMove(int ¤tCellX, int ¤tCellY, int facingDir); //make next move and update mazeMap (char)
void readMaze(); //iterates over maze cells and stores as array,
void initialize(); //initializes robot
int findNextMove(int const & cursorCellX, int const & cursorCellY, int &facingDir); //(done)
void storeNextMove(int currentCellX, int currentCellY, int facingDir); //(pranav)
void modifyMazeMap(int const & cursorCellX, int const & cursorCellY);
bool searchEnds();
bool checkBlank();
const int MAZE_R = 17;
int MAZE_C = 17;
int WALL = -1;
int mazeMap[17][17] = {
{-1, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, -1},
{-1, 0, -1, -1, -1, 0, -1, 0, -1, 0, -1, 0, -1, -1, -1, 0, -1},
{-1, 0, -1, 0, 0, 0, -1, 0, -1, 0, -1, 0, -1, 0, 0, 0, -1},
{-1, 0, -1, 0, -1, -1, -1, 0, -1, -1, -1, 0, -1, -1, -1, 0, -1},
{-1, 0, -1, 0, -1, 0, 0, 0, 0, 0, -1, 0, -1, 0, -1, 0, -1},
{-1, -1, -1, 0, -1, -1, -1, -1, -1, 0, -1, 0, -1, 0, -1, -1, -1},
{-1, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, -1},
{-1, 0, -1, -1, -1, 0, -1, 0, -1, -1, -1, -1, -1, -1, -1, 0, -1},
{-1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, -1},
{-1, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, 0, -1},
{-1, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, -1},
{-1, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1},
{-1, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, -1},
{-1, 0, -1, -1, -1, -1, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1},
{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, -1, 0, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1}
};
int main() {
cout << mazeMap[0][1];
int startCellX = 0;
int startCellY = 0;
handrailAlgo(startCellX, startCellY);
for(int r = 0; r < MAZE_R; r++)
{
for(int c = 0; c < MAZE_C; c++) {
cout << mazeMap[r][c] << "\t";
}
cout << endl;
}
}
void handrailAlgo(int &startCellX, int &startCellY)
{
if (checkBlank())
{
// return EXIT.FAILURE;
}
//Define solution as a modification of the mazeMap, all 0/1
//Solution will be realized by sequence of movements from point to point
//Solve Maze while recording movements
int goalCellX = 0, goalCellY = 0, facingDir = 0;
int cursorCellX = 0, cursorCellY = 0;
// set ends to black
// for (int row = 0; row < MAZE_R; row++)
// {
// if (row == 0 || row == MAZE_R-1)
// {
// for (int col = 0; col < MAZE_C; col++)
// {
// mazeMap[row][col] = -1;
// }
// }
// else
// {
// mazeMap[row][0] = -1;
// mazeMap[row][MAZE_C-1] = -1;
// }
// }
//search mazeMap for start and end points
bool top_left = searchEnds();
if(top_left)
{
startCellX = 1;
startCellY = 0;
// mazeMap[startCellY][startCellX] = 1;
goalCellX = MAZE_C-2;
goalCellY = MAZE_R-1;
mazeMap[goalCellY][goalCellX]=0;
}
else
{
startCellX= 0;
startCellY=MAZE_R-2;
// mazeMap[startCellY][startCellX] = 1;
goalCellX=MAZE_C-1;
goalCellY=1;
mazeMap[goalCellY][goalCellX]=0;
}
// cout << "start cell: [" << startCellY << ", " << startCellX << "]" << endl;
// cout << "goal cell: [" << goalCellY << ", " << goalCellX << "]" << endl;
cursorCellX = startCellX;
cursorCellY = startCellY;
while (cursorCellX != goalCellX || cursorCellY != goalCellY)
{
makeNextMove(cursorCellX, cursorCellY, findNextMove(cursorCellX, cursorCellY, facingDir));
}
}
bool searchEnds() //changed so that it checks the array and not moving the actual track and whatnot
{
return (mazeMap[0][1] == 0);
}
bool isValidMove(int currentCellX, int currentCellY, int facingDir)//need to flip params
{
int count = 1;//because constants don't work here??
int checkCellValue;
if(facingDir == 0 && currentCellY - count >= 0)
{
checkCellValue = mazeMap[currentCellY - count][currentCellX];
if(checkCellValue != WALL)
{
return true;
}
}
else if(facingDir == 1 && currentCellX + count <= MAZE_C)
{
checkCellValue = mazeMap[currentCellY][currentCellX + count];
if(checkCellValue != WALL)
{
return true;
}
}
else if(facingDir == 2 && currentCellY - count <= MAZE_R)
{
checkCellValue = mazeMap[currentCellY + count][currentCellX];
if(checkCellValue != WALL)
{
return true;
}
}
else if(facingDir == 3 && currentCellX - count >= 0)
{
checkCellValue = mazeMap[currentCellY][currentCellX - count];
if(checkCellValue != WALL)
{
return true;
}
}
return false;
}
int findNextMove(int const & cursorCellX, int const & cursorCellY, int & facingDir)
{
facingDir = (3 + facingDir )%4; //equivalent to (facingDir - 1) %4?
for (int attempts = 0; attempts < 4; attempts++)//only 3 checks needed since otherwise go back
{
if(isValidMove(cursorCellX, cursorCellY, facingDir))
{
return facingDir;
}
facingDir = (facingDir + 1) % 4;
}
return -1;
}
void makeNextMove(int ¤tCellX, int ¤tCellY, int facingDir)
{
//dir 0 is up, 1 is right, 2 is down, 3 is left
//update current position after each move (in the move functions)
modifyMazeMap(currentCellX, currentCellY);
if (facingDir == 0) //if we need to go up
{
currentCellY -= 1;
// when are we calling move to cell
}
else if (facingDir == 1) //if we need to go right
{
currentCellX += 1;
}
else if (facingDir == 2) //if we need to go down
{
currentCellY += 1;
}
else if (facingDir == 3) //if we need to go left
{
currentCellX -= 1;
}
}
// y is the rows, x is the cols
bool junctionCheck(int currentCellX, int currentCellY)
{
int validCells = 0;
// check if the four girds beside current cell have more than one valid cell
for(int direction = 0; direction < 4; direction++)
{
if(isValidMove(currentCellX, currentCellY, direction % 4 ))
{
validCells++;
}
}
if(validCells > 2)
{
return true;
}
return false;
}
bool checkBlank()
{
return (mazeMap[0][1]==mazeMap[1][0]);
}
void modifyMazeMap(int const & cursorCellX, int const & cursorCellY)
{
if (mazeMap[cursorCellY][cursorCellX] == 0 || junctionCheck(cursorCellX, cursorCellY))
{
mazeMap[cursorCellY][cursorCellX] = 1;
}
else
{
mazeMap[cursorCellY][cursorCellX] = 2;
}
}