Skip to content

Commit 6e301da

Browse files
committed
refactoring CoolMaze and StdDraw for TurtleMaze
1 parent 26a39c8 commit 6e301da

2 files changed

Lines changed: 73 additions & 167 deletions

File tree

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

Lines changed: 54 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,20 @@ public class CoolMaze
66
{
77
public static void main(String[] args)
88
{
9-
int mazeComplexity = 10;
10-
CoolMaze maze = new CoolMaze(mazeComplexity);
9+
int mazeComplexityScale = 10;
10+
CoolMaze maze = new CoolMaze(mazeComplexityScale);
1111
StdDraw.show(0);
1212
maze.drawWallsAndStartAndEndPoints();
1313
maze.solveThisMaze();
1414
}
15-
public CoolMaze(int N)
15+
public CoolMaze(int mazeComplexity)
1616
{
17-
setMazeScale(N);
17+
this.mazeComplexity = mazeComplexity;
18+
StdDraw.setXscale(0, mazeComplexity + 2);
19+
StdDraw.setYscale(0, mazeComplexity + 2);
1820
createMazeStructure();
1921
generateStartLocation(1, 1);
2022
}
21-
public void solveThisMaze()
22-
{
23-
int startingX = 1;
24-
int startingY = 1;
25-
for (int x = startingX; x <= N; x++)
26-
for (int y = startingY; y <= N; y++)
27-
visited[x][y] = false;
28-
done = false;
29-
solve(startingX, startingY);
30-
}
3123
public void drawWallsAndStartAndEndPoints()
3224
{
3325
Color colorOfStartAndEndPoints = StdDraw.RED;
@@ -37,32 +29,26 @@ public void drawWallsAndStartAndEndPoints()
3729
drawAndColorMazeWalls();
3830
StdDraw.show(1000);
3931
}
40-
private void setMazeScale(int N)
41-
{
42-
this.N = N;
43-
StdDraw.setXscale(0, N + 2);
44-
StdDraw.setYscale(0, N + 2);
45-
}
4632
private void createMazeStructure()
4733
{
48-
visited = new boolean[N + 2][N + 2];
49-
for (int x = 0; x < N + 2; x++)
34+
visited = new boolean[mazeComplexity + 2][mazeComplexity + 2];
35+
for (int x = 0; x < mazeComplexity + 2; x++)
5036
{
5137
visited[x][0] = true;
52-
visited[x][N + 1] = true;
38+
visited[x][mazeComplexity + 1] = true;
5339
}
54-
for (int y = 0; y < N + 2; y++)
40+
for (int y = 0; y < mazeComplexity + 2; y++)
5541
{
5642
visited[0][y] = true;
57-
visited[N + 1][y] = true;
43+
visited[mazeComplexity + 1][y] = true;
5844
}
59-
north = new boolean[N + 2][N + 2];
60-
east = new boolean[N + 2][N + 2];
61-
south = new boolean[N + 2][N + 2];
62-
west = new boolean[N + 2][N + 2];
63-
for (int x = 0; x < N + 2; x++)
45+
north = new boolean[mazeComplexity + 2][mazeComplexity + 2];
46+
east = new boolean[mazeComplexity + 2][mazeComplexity + 2];
47+
south = new boolean[mazeComplexity + 2][mazeComplexity + 2];
48+
west = new boolean[mazeComplexity + 2][mazeComplexity + 2];
49+
for (int x = 0; x < mazeComplexity + 2; x++)
6450
{
65-
for (int y = 0; y < N + 2; y++)
51+
for (int y = 0; y < mazeComplexity + 2; y++)
6652
{
6753
north[x][y] = true;
6854
east[x][y] = true;
@@ -110,28 +96,6 @@ else if (r == 3 && !visited[x - 1][y])
11096
}
11197
}
11298
}
113-
private void solve(int x, int y)
114-
{
115-
if (x == 0 || y == 0 || x == N + 1 || y == N + 1)
116-
return;
117-
if (done || visited[x][y])
118-
return;
119-
visited[x][y] = true;
120-
renderCorrectSolveDots(x, y);
121-
if (x == N / 2 && y == N / 2)
122-
done = true;
123-
if (!north[x][y])
124-
solve(x, y + 1);
125-
if (!east[x][y])
126-
solve(x + 1, y);
127-
if (!south[x][y])
128-
solve(x, y - 1);
129-
if (!west[x][y])
130-
solve(x - 1, y);
131-
if (done)
132-
return;
133-
renderIncorrectSolveDots(x, y);
134-
}
13599
private void renderCorrectSolveDots(int x, int y)
136100
{
137101
Color colorOfCorrectSolvePath = StdDraw.BLUE;
@@ -152,23 +116,23 @@ private void drawAndSizeSolvePath(int x, int y)
152116
}
153117
private void createAndSizeEndPoint()
154118
{
155-
StdDraw.filledCircle(N / 2.0 + 0.5, N / 2.0 + 0.5, 0.375);
119+
StdDraw.filledCircle(mazeComplexity / 2.0 + 0.5, mazeComplexity / 2.0 + 0.5, 0.375);
156120
}
157121
private void createAndSizeStartPoint()
158122
{
159123
StdDraw.filledCircle(1.5, 1.5, 0.375);
160124
}
161125
private void drawAndColorMazeWalls()
162126
{
163-
Color colorOfMazeWalls = StdDraw.BLACK;
127+
Color colorOfMazeWalls = StdDraw.DARK_GRAY;
164128
StdDraw.setPenColor(colorOfMazeWalls);
165129
drawMazeWalls();
166130
}
167131
private void drawMazeWalls()
168132
{
169-
for (int x = 1; x <= N; x++)
133+
for (int x = 1; x <= mazeComplexity; x++)
170134
{
171-
for (int y = 1; y <= N; y++)
135+
for (int y = 1; y <= mazeComplexity; y++)
172136
{
173137
if (south[x][y])
174138
StdDraw.line(x, y, x + 1, y);
@@ -181,7 +145,39 @@ private void drawMazeWalls()
181145
}
182146
}
183147
}
184-
private int N;
148+
public void solveThisMaze()
149+
{
150+
int startingX = 1;
151+
int startingY = 1;
152+
for (int x = startingX; x <= mazeComplexity; x++)
153+
for (int y = startingY; y <= mazeComplexity; y++)
154+
visited[x][y] = false;
155+
done = false;
156+
solve(startingX, startingY);
157+
}
158+
private void solve(int x, int y)
159+
{
160+
if (x == 0 || y == 0 || x == mazeComplexity + 1 || y == mazeComplexity + 1)
161+
return;
162+
if (done || visited[x][y])
163+
return;
164+
visited[x][y] = true;
165+
renderCorrectSolveDots(x, y);
166+
if (x == mazeComplexity / 2 && y == mazeComplexity / 2)
167+
done = true;
168+
if (!north[x][y])
169+
solve(x, y + 1);
170+
if (!east[x][y])
171+
solve(x + 1, y);
172+
if (!south[x][y])
173+
solve(x, y - 1);
174+
if (!west[x][y])
175+
solve(x - 1, y);
176+
if (done)
177+
return;
178+
renderIncorrectSolveDots(x, y);
179+
}
180+
private int mazeComplexity;
185181
private boolean[][] north;
186182
private boolean[][] east;
187183
private boolean[][] south;

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

Lines changed: 19 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,5 @@
11
package org.teachingextensions.logo.utils.MazeUtils;
22

3-
/******************************************************************************
4-
* Compilation: javac StdDraw.java
5-
* Execution: java StdDraw
6-
* Dependencies: none
7-
*
8-
* Standard drawing library. This class provides a basic capability for
9-
* creating drawings with your programs. It uses a simple graphics model that
10-
* allows you to create drawings consisting of points, lines, and curves
11-
* in a window on your computer and to save the drawings to a file.
12-
*
13-
* Todo
14-
* ----
15-
* - Add support for gradient fill, etc.
16-
* - Fix setCanvasSize() so that it can only be called once.
17-
* - On some systems, drawing a line (or other shape) that extends way
18-
* beyond canvas (e.g., to infinity) dimensions does not get drawn.
19-
*
20-
* Remarks
21-
* -------
22-
* - don't use AffineTransform for rescaling since it inverts
23-
* images and strings
24-
*
25-
******************************************************************************/
263
import java.awt.BasicStroke;
274
import java.awt.Color;
285
import java.awt.FileDialog;
@@ -65,22 +42,10 @@
6542
import javax.swing.KeyStroke;
6643

6744
/**
68-
* The {@code StdDraw} class provides a basic capability for
69-
* creating drawings with your programs. It uses a simple graphics model that
70-
* allows you to create drawings consisting of points, lines, squares,
71-
* circles, and other geometric shapes in a window on your computer and
72-
* to save the drawings to a file. Standard drawing also includes
73-
* facilities for text, color, pictures, and animation, along with
74-
* user interaction via the keyboard and mouse.
7545
* <p>
7646
* <b>Getting started.</b>
77-
* To use standard drawing, you must have <tt>StdDraw.class</tt> in your
78-
* Java classpath. If you used our autoinstaller, you should be all set.
79-
* Otherwise, download
80-
* <a href = "http://introcs.cs.princeton.edu/java/stdlib/StdDraw.java">StdDraw.java</a>
81-
* and put a copy in your working directory.
8247
* <p>
83-
* Now, type the following short program into your editor:
48+
* Type the following short program into your editor:
8449
* <pre>
8550
* public class TestStdDraw {
8651
* public static void main(String[] args) {
@@ -92,14 +57,6 @@
9257
* }
9358
* }
9459
* </pre>
95-
* If you compile and execute the program, you should see a window
96-
* appear with a thick magenta line and a blue point.
97-
* This program illustrates the two main types of methods in standard
98-
* drawing&mdash;methods that draw geometric shapes and methods that
99-
* control drawing parameters.
100-
* The methods {@code StdDraw.line()} and {@code StdDraw.point()}
101-
* draw lines and points; the methods {@code StdDraw.setPenRadius()}
102-
* and {@code StdDraw.setPenColor()} control the line thickness and color.
10360
* <p>
10461
* <b>Points and lines.</b>
10562
* You can draw points and line segments with the following methods:
@@ -442,57 +399,18 @@
442399
*/
443400
public final class StdDraw implements ActionListener, MouseListener, MouseMotionListener, KeyListener
444401
{
445-
/**
446-
* The color black.
447-
*/
448402
public static final Color BLACK = Color.BLACK;
449-
/**
450-
* The color blue.
451-
*/
452403
public static final Color BLUE = Color.BLUE;
453-
/**
454-
* The color cyan.
455-
*/
456404
public static final Color CYAN = Color.CYAN;
457-
/**
458-
* The color dark gray.
459-
*/
460405
public static final Color DARK_GRAY = Color.DARK_GRAY;
461-
/**
462-
* The color gray.
463-
*/
464406
public static final Color GRAY = Color.GRAY;
465-
/**
466-
* The color green.
467-
*/
468407
public static final Color GREEN = Color.GREEN;
469-
/**
470-
* The color light gray.
471-
*/
472408
public static final Color LIGHT_GRAY = Color.LIGHT_GRAY;
473-
/**
474-
* The color magenta.
475-
*/
476409
public static final Color MAGENTA = Color.MAGENTA;
477-
/**
478-
* The color orange.
479-
*/
480410
public static final Color ORANGE = Color.ORANGE;
481-
/**
482-
* The color pink.
483-
*/
484411
public static final Color PINK = Color.PINK;
485-
/**
486-
* The color red.
487-
*/
488412
public static final Color RED = Color.RED;
489-
/**
490-
* The color white.
491-
*/
492413
public static final Color WHITE = Color.WHITE;
493-
/**
494-
* The color yellow.
495-
*/
496414
public static final Color YELLOW = Color.YELLOW;
497415
/**
498416
* Shade of blue used in <em>Introduction to Programming in Java</em>.
@@ -557,15 +475,31 @@ public final class StdDraw implements ActionListener, MouseListener, MouseMotion
557475
// time in milliseconds (from currentTimeMillis()) when we can draw again
558476
// used to control the frame rate
559477
private static long nextDraw = -1;
560-
// singleton pattern: client can't instantiate
561478
private StdDraw()
562479
{
563480
}
564-
// static initializer
565481
static
566482
{
567483
init();
568484
}
485+
public static void main(String[] args)
486+
{
487+
StdDraw.square(.2, .8, .1);
488+
StdDraw.filledSquare(.8, .8, .2);
489+
StdDraw.circle(.8, .2, .2);
490+
StdDraw.setPenColor(StdDraw.BOOK_RED);
491+
StdDraw.setPenRadius(.02);
492+
StdDraw.arc(.8, .2, .1, 200, 45);
493+
StdDraw.setPenRadius();
494+
StdDraw.setPenColor(StdDraw.BOOK_BLUE);
495+
double[] x = {.1, .2, .3, .2};
496+
double[] y = {.2, .3, .2, .1};
497+
StdDraw.filledPolygon(x, y);
498+
StdDraw.setPenColor(StdDraw.BLACK);
499+
StdDraw.text(0.2, 0.5, "black text");
500+
StdDraw.setPenColor(StdDraw.WHITE);
501+
StdDraw.text(0.8, 0.8, "white text");
502+
}
569503
/**
570504
* Sets the canvas (drawing area) to be 512-by-512 pixels.
571505
* This also erases the current drawing and resets the coordinate system,
@@ -1539,7 +1473,6 @@ public static void save(String filename)
15391473
throw new NullPointerException();
15401474
File file = new File(filename);
15411475
String suffix = filename.substring(filename.lastIndexOf('.') + 1);
1542-
// png files
15431476
if (suffix.toLowerCase().equals("png"))
15441477
{
15451478
try
@@ -1787,27 +1720,4 @@ public void keyReleased(KeyEvent e)
17871720
keysDown.remove(e.getKeyCode());
17881721
}
17891722
}
1790-
/**
1791-
* Test client.
1792-
*/
1793-
public static void main(String[] args)
1794-
{
1795-
StdDraw.square(.2, .8, .1);
1796-
StdDraw.filledSquare(.8, .8, .2);
1797-
StdDraw.circle(.8, .2, .2);
1798-
StdDraw.setPenColor(StdDraw.BOOK_RED);
1799-
StdDraw.setPenRadius(.02);
1800-
StdDraw.arc(.8, .2, .1, 200, 45);
1801-
// draw a blue diamond
1802-
StdDraw.setPenRadius();
1803-
StdDraw.setPenColor(StdDraw.BOOK_BLUE);
1804-
double[] x = {.1, .2, .3, .2};
1805-
double[] y = {.2, .3, .2, .1};
1806-
StdDraw.filledPolygon(x, y);
1807-
// text
1808-
StdDraw.setPenColor(StdDraw.BLACK);
1809-
StdDraw.text(0.2, 0.5, "black text");
1810-
StdDraw.setPenColor(StdDraw.WHITE);
1811-
StdDraw.text(0.8, 0.8, "white text");
1812-
}
18131723
}

0 commit comments

Comments
 (0)