Skip to content

Commit a49a81e

Browse files
committed
refactoring the CoolMaze w/ @mattballin
1 parent bc200cc commit a49a81e

5 files changed

Lines changed: 163 additions & 3 deletions

File tree

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package org.teachingextensions.logo.utils.MazeUtils;
2+
3+
public class CoolMaze
4+
{
5+
private int N;
6+
private boolean[][] north;
7+
private boolean[][] east;
8+
private boolean[][] south;
9+
private boolean[][] west;
10+
private boolean[][] visited;
11+
private boolean done = false;
12+
public CoolMaze(int N)
13+
{
14+
this.N = N;
15+
StdDraw.setXscale(0, N + 2);
16+
StdDraw.setYscale(0, N + 2);
17+
createMazeStructure();
18+
generateStartLocation();
19+
}
20+
private void createMazeStructure()
21+
{
22+
visited = new boolean[N + 2][N + 2];
23+
for (int x = 0; x < N + 2; x++)
24+
{
25+
visited[x][0] = true;
26+
visited[x][N + 1] = true;
27+
}
28+
for (int y = 0; y < N + 2; y++)
29+
{
30+
visited[0][y] = true;
31+
visited[N + 1][y] = true;
32+
}
33+
north = new boolean[N + 2][N + 2];
34+
east = new boolean[N + 2][N + 2];
35+
south = new boolean[N + 2][N + 2];
36+
west = new boolean[N + 2][N + 2];
37+
for (int x = 0; x < N + 2; x++)
38+
{
39+
for (int y = 0; y < N + 2; y++)
40+
{
41+
north[x][y] = true;
42+
east[x][y] = true;
43+
south[x][y] = true;
44+
west[x][y] = true;
45+
}
46+
}
47+
}
48+
private void generate(int x, int y)
49+
{
50+
visited[x][y] = true;
51+
while (!visited[x][y + 1] || !visited[x + 1][y] || !visited[x][y - 1] || !visited[x - 1][y])
52+
{
53+
while (true)
54+
{
55+
double r = StdRandom.uniform(4);
56+
if (r == 0 && !visited[x][y + 1])
57+
{
58+
north[x][y] = false;
59+
south[x][y + 1] = false;
60+
generate(x, y + 1);
61+
break;
62+
}
63+
else if (r == 1 && !visited[x + 1][y])
64+
{
65+
east[x][y] = false;
66+
west[x + 1][y] = false;
67+
generate(x + 1, y);
68+
break;
69+
}
70+
else if (r == 2 && !visited[x][y - 1])
71+
{
72+
south[x][y] = false;
73+
north[x][y - 1] = false;
74+
generate(x, y - 1);
75+
break;
76+
}
77+
else if (r == 3 && !visited[x - 1][y])
78+
{
79+
west[x][y] = false;
80+
east[x - 1][y] = false;
81+
generate(x - 1, y);
82+
break;
83+
}
84+
}
85+
}
86+
}
87+
private void generateStartLocation()
88+
{
89+
generate(1, 1);
90+
}
91+
private void solve(int x, int y)
92+
{
93+
if (x == 0 || y == 0 || x == N + 1 || y == N + 1)
94+
return;
95+
if (done || visited[x][y])
96+
return;
97+
visited[x][y] = true;
98+
StdDraw.setPenColor(StdDraw.BLUE);
99+
StdDraw.filledCircle(x + 0.5, y + 0.5, 0.25);
100+
StdDraw.show(30);
101+
if (x == N / 2 && y == N / 2)
102+
done = true;
103+
if (!north[x][y])
104+
solve(x, y + 1);
105+
if (!east[x][y])
106+
solve(x + 1, y);
107+
if (!south[x][y])
108+
solve(x, y - 1);
109+
if (!west[x][y])
110+
solve(x - 1, y);
111+
if (done)
112+
return;
113+
StdDraw.setPenColor(StdDraw.GRAY);
114+
StdDraw.filledCircle(x + 0.5, y + 0.5, 0.25);
115+
StdDraw.show(30);
116+
}
117+
public void solveThisMaze()
118+
{
119+
for (int x = 1; x <= N; x++)
120+
for (int y = 1; y <= N; y++)
121+
visited[x][y] = false;
122+
done = false;
123+
solve(1, 1);
124+
}
125+
public void drawSolution()
126+
{
127+
StdDraw.setPenColor(StdDraw.RED);
128+
StdDraw.filledCircle(N / 2.0 + 0.5, N / 2.0 + 0.5, 0.375);
129+
StdDraw.filledCircle(1.5, 1.5, 0.375);
130+
StdDraw.setPenColor(StdDraw.BLACK);
131+
for (int x = 1; x <= N; x++)
132+
{
133+
for (int y = 1; y <= N; y++)
134+
{
135+
if (south[x][y])
136+
StdDraw.line(x, y, x + 1, y);
137+
if (north[x][y])
138+
StdDraw.line(x, y + 1, x + 1, y + 1);
139+
if (west[x][y])
140+
StdDraw.line(x, y, x, y + 1);
141+
if (east[x][y])
142+
StdDraw.line(x + 1, y, x + 1, y + 1);
143+
}
144+
}
145+
StdDraw.show(1000);
146+
}
147+
public static void main(String[] args)
148+
{
149+
int mazeComplexityDepth = 10;
150+
CoolMaze maze = new CoolMaze(mazeComplexityDepth);
151+
StdDraw.show(0);
152+
maze.drawSolution();
153+
maze.solveThisMaze();
154+
}
155+
}

src/main/java/org/teachingextensions/logo/utils/MazeUtils/Maze.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public static void main(String[] args)
188188
// This line throws an index out bounds with any value in args ???
189189
//int N = Integer.parseInt(args[0]);
190190
//TODO: change this value to increase the complexity of the Maze
191-
int N = 8;
191+
int N = 10;
192192
Maze maze = new Maze(N);
193193
StdDraw.show(0);
194194
maze.draw();

src/main/java/org/teachingkidsprogramming/recipes/completed/section09final/FizzBuzzGoldenMaster.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public void testNumbers() throws Exception
1010
{
1111
// ARRANGE - Create a list of numbers from 1-100
1212
StringBuilder sb = new StringBuilder();
13-
for (int i = 1; i < 101; i++)
13+
for (int i = 1; i < 10; i++)
1414
{
1515
// ACT - Call the convertNumbers method on the list
1616
sb.append(convert(i));

src/main/java/org/teachingkidsprogramming/recipes/completed/section09final/RectangleKataTDD.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,28 @@ public void testShowsTheTortoise() throws Exception
2121
Approvals.verify(bufferedImage);
2222
}
2323
@Test
24-
public void testMovesTheTortoise50()
24+
public void testMovesTheTortoise50() throws Exception
2525
{
2626
//Set up
27+
Tortoise.show();
2728
Tortoise.move(50);
2829
//Verify
30+
String bufferedImage1 = " ";
31+
Approvals.verify(bufferedImage1);
2932
}
3033
@Test
3134
public void testTurnsTheTortoise90()
3235
{
3336
//Set up
37+
Tortoise.show();
3438
Tortoise.turn(90);
3539
//Verify
3640
}
3741
@Test
3842
public void testDrawsFourSides()
3943
{
4044
//Set up
45+
Tortoise.show();
4146
//Tortoise.drawSide();
4247
//Verify
4348
}

src/main/java/org/teachingkidsprogramming/recipes/completed/section09final/RectangleKataTDD.testMovesTheTortoise50.approved.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)