-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPA4MazeSolver.java
More file actions
191 lines (162 loc) · 7.7 KB
/
Copy pathPA4MazeSolver.java
File metadata and controls
191 lines (162 loc) · 7.7 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
/**
* PA4MazeSolver.java
*
* Maze canonical example for PA4.
* Initializes a maze that should have at least one path from <1,6> to <6,1>.
* Puts a point at <1,6> and navigates maze with a recursive function.
* Walls are VIOLET, non-walls are the default DARK
* Solution path is BLUE
* Portions of path that didn't work are YELLOW. *
* The stack size is bound because there are a constant number of pixels
* on the LED screen.
*
* MMS, 1/13/11
*/
import meggy.Meggy;
class PA4MazeSolver {
public static void main(String[] whatever){
// Only want to construct one instance of class
new Solver().run();
}
}
class Solver {
// no maze walls, the solver should check boundaries of LED screen
public void initMaze1() {
}
// Example with only a single path.
public void initMaze2() {
this.mazeRow((byte)0, (byte)7, (byte)7); // top row
this.mazeRow((byte)0, (byte)5, (byte)5); // partial of 2 rows under
this.mazeCol((byte)7, (byte)0, (byte)7); // rightmost col
this.mazeCol((byte)5, (byte)0, (byte)5); // partial of 2 cols to the left
}
// Cooler example
public void initMazeCool() {
this.mazeRow((byte)0, (byte)7, (byte)7); // top row
this.mazeRow((byte)0, (byte)7, (byte)0); // bottom row
this.mazeCol((byte)7, (byte)0, (byte)7); // rightmost col
this.mazeCol((byte)0, (byte)0, (byte)7); // leftmost col
// 2,5 to 5,5
this.mazeRow((byte)2, (byte)5, (byte)5);
// 2,2 to 2,5
this.mazeCol((byte)2, (byte)2, (byte)5);
// 2,2 to 5,2
this.mazeRow((byte)2, (byte)5, (byte)2);
Meggy.setPixel((byte)5, (byte)1, Meggy.Color.VIOLET);
}
public void run() {
this.initMazeCool();
this.move((byte)1,(byte)6,(byte)6,(byte)1);
}
public boolean inBounds(byte x, byte y) {
return ((byte)(0-1) < x) && (x < (byte)8) && ((byte)(0-1) < y) && (y < (byte)8);
}
public boolean isDark(byte x, byte y) {
return this.inBounds(x,y) && (Meggy.getPixel(x, y) == Meggy.Color.DARK);
}
public boolean isBLUE(byte x, byte y) {
return this.inBounds(x,y) && (Meggy.getPixel(x, y) == Meggy.Color.BLUE);
}
public void findTargetOrMove(byte nextX, byte nextY, byte targetX, byte targetY) {
// If target then turn target BLUE and fall to end of function.
if (nextX==targetX && nextY==targetY ) {
Meggy.setPixel(nextX, nextY, Meggy.Color.BLUE);
}
// if is not target then move there to continue search
else {
this.move(nextX,nextY,targetX,targetY);
}
}
// Makes currentX and currentY BLUE.
// Will attempt to move the BLUE path to any open neighboring
// DARK LED. If can't do that then will backtrack with
// a YELLOW path. Searches neighbors in this order:
// south, east, north, west
public void move(byte currentX, byte currentY, byte targetX, byte targetY) {
Meggy.delay(256);
Meggy.setPixel(currentX, currentY, Meggy.Color.BLUE);
// Is south neighbor dark?
if (this.isDark(currentX,(byte)(currentY-(byte)1))) {
this.findTargetOrMove(currentX, (byte)(currentY-(byte)1),
targetX, targetY);
} else {
// Is east neighbor dark?
if (this.isDark((byte)(currentX+(byte)1),currentY)) {
this.findTargetOrMove((byte)(currentX+(byte)1), currentY,
targetX, targetY);
} else {
// Is north neighbor dark?
if (this.isDark(currentX,(byte)(currentY+(byte)1))) {
this.findTargetOrMove(currentX, (byte)(currentY+(byte)1),
targetX, targetY);
} else {
// Is west neighbor dark?
if (this.isDark((byte)(currentX-(byte)1),currentY)) {
this.findTargetOrMove((byte)(currentX-(byte)1), currentY,
targetX, targetY);
}
// Did not find a DARK neighbor so need to try backtracking.
// This must be in a nested if otherwise if we happened to find
// the target above we might also backtrack.
// For backtracking we look for a BLUE neighbor
// and then turn ourselves yellow before moving there.
// If don't find a BLUE neighbor will fall to end.
else {
// Is south neighbor BLUE?
if (this.isBLUE(currentX,(byte)(currentY-(byte)1))) {
Meggy.setPixel(currentX,currentY,
Meggy.Color.YELLOW);
this.move(currentX,(byte)(currentY-(byte)1),
targetX, targetY);
} else {
// Is east neighbor BLUE?
if (this.isBLUE((byte)(currentX+(byte)1),currentY)) {
Meggy.setPixel(currentX,currentY,
Meggy.Color.YELLOW);
this.move((byte)(currentX+(byte)1),currentY,
targetX, targetY);
} else {
// Is north neighbor BLUE?
if (this.isBLUE(currentX,(byte)(currentY+(byte)1))) {
Meggy.setPixel(currentX,currentY,
Meggy.Color.YELLOW);
this.move((byte)(currentX),(byte)(currentY+(byte)1),
targetX, targetY);
} else {
// Is west neighbor BLUE?
if (this.isBLUE((byte)(currentX-(byte)1),currentY)) {
Meggy.setPixel(currentX,currentY,
Meggy.Color.YELLOW);
this.move((byte)(currentX-(byte)1),currentY,
targetX, targetY);
}
// else there is nothing we can do
// just fall to return
else {}
}
}
}
}
}
}
}
}
// Starting and stopping at the given columns,
// make the given row a maze border.
// Assuming that startcol, endcol, and row are in bounds.
public void mazeRow(byte startcol, byte endcol, byte row) {
Meggy.setPixel(startcol, row, Meggy.Color.VIOLET);
if (startcol<endcol) {
this.mazeRow((byte)(startcol+(byte)1), endcol, row);
} else {}
}
// Starting and stopping at the given rows,
// make the given col a maze border.
// Assuming that startrow, endrow, and col are in bounds.
public void mazeCol(byte col, byte startrow, byte endrow) {
Meggy.setPixel(col, startrow, Meggy.Color.VIOLET);
if (startrow<endrow) {
this.mazeCol(col,(byte)(startrow+(byte)1), endrow);
} else {}
}
}