diff --git a/src/main/java/edu/illinois/cs/cs125/lib/mazemaker/Maze.java b/src/main/java/edu/illinois/cs/cs125/lib/mazemaker/Maze.java index 7b56616..4af569f 100644 --- a/src/main/java/edu/illinois/cs/cs125/lib/mazemaker/Maze.java +++ b/src/main/java/edu/illinois/cs/cs125/lib/mazemaker/Maze.java @@ -326,7 +326,7 @@ public void setRelativeDirection(final String setRelativeDirection) { } /** - * Instantiates a new cell. + * Create a new cell. * * @param setLocation the cell's location */ @@ -379,7 +379,7 @@ public int getyDimension() { } /** - * Instantiates a new maze. + * Create a new randomly-generated maze. * * @param mazeXDimension the x dimension * @param mazeYDimension the y dimension @@ -490,7 +490,7 @@ public Maze(final int mazeXDimension, final int mazeYDimension) { } /** The user's current X, Y location. */ - private Location currentLocation; + private Location currentLocation = null; /** * Start the maze at a specific location. @@ -536,7 +536,7 @@ public Location getCurrentLocation() { } /** The maze's end location. */ - private Location endLocation; + private Location endLocation = null; /** * End the maze at a specific location. @@ -557,7 +557,7 @@ public void endAt(final int x, final int y) throws LocationException { * End the maze at the top right corner. */ public void endAtTopRight() { - currentLocation = new Location(myXDimension - 1, myYDimension - 1); + endLocation = new Location(myXDimension - 1, myYDimension - 1); } /** @@ -616,16 +616,16 @@ public boolean canMove() { @SuppressWarnings("checkstyle:missingswitchdefault") public void turnLeft() { switch (currentDirection) { - case "up": + case "up" : currentDirection = "left"; break; - case "left": + case "left" : currentDirection = "down"; break; - case "down": + case "down" : currentDirection = "right"; break; - case "right": + case "right" : currentDirection = "up"; break; } @@ -637,16 +637,16 @@ public void turnLeft() { @SuppressWarnings("checkstyle:missingswitchdefault") public void turnRight() { switch (currentDirection) { - case "up": + case "up" : currentDirection = "right"; break; - case "right": + case "right" : currentDirection = "down"; break; - case "down": + case "down" : currentDirection = "left"; break; - case "left": + case "left" : currentDirection = "up"; break; } @@ -660,4 +660,59 @@ public void turnRight() { public boolean isFinished() { return currentLocation.equals(endLocation); } + + @Override + @SuppressWarnings("checkstyle:innerassignment") + public final String toString() { + String returnString = ""; + char[][] cellDrawing = new char[2 * myXDimension + 1][2 * myYDimension + 1]; + + for (int y = 0; y < 2 * myYDimension; y++) { + for (int x = 0; x < 2 * myXDimension; x++) { + cellDrawing[x][y] = ' '; + } + } + for (int x = 0; x < myXDimension; x++) { + for (int y = 0; y < myYDimension; y++) { + int xOffset = 2 * x; + int yOffset = 2 * y; + if (maze[x][y].getBorder("up")) { + cellDrawing[xOffset][yOffset + 2] = + cellDrawing[xOffset + 1][yOffset + 2] = + cellDrawing[xOffset + 2][yOffset + 2] = '#'; + } + if (maze[x][y].getBorder("right")) { + cellDrawing[xOffset + 2][yOffset] = + cellDrawing[xOffset + 2][yOffset + 1] = + cellDrawing[xOffset + 2][yOffset + 2] = '#'; + } + if (maze[x][y].getBorder("down")) { + cellDrawing[xOffset][yOffset] = + cellDrawing[xOffset + 1][yOffset] = + cellDrawing[xOffset + 2][yOffset] = '#'; + } + if (maze[x][y].getBorder("left")) { + cellDrawing[xOffset][yOffset] = + cellDrawing[xOffset][yOffset + 1] = + cellDrawing[xOffset][yOffset + 2] = '#'; + } + if (x == currentLocation.x() && y == currentLocation.y()) { + cellDrawing[xOffset + 1][yOffset + 1] = 'X'; + } else if (x == endLocation.x() && y == endLocation.y()) { + cellDrawing[xOffset + 1][yOffset + 1] = 'E'; + } + } + } + for (int x = 0; x < 2 * myXDimension + 1; x++) { + returnString += "#"; + } + returnString += "\n"; + for (int y = (2 * myYDimension) - 1; y >= 0; y--) { + for (int x = 0; x < 2 * myXDimension; x++) { + returnString += cellDrawing[x][y]; + } + returnString += "#\n"; + } + return returnString; + } } diff --git a/src/test/java/TestMaze.java b/src/test/java/TestMaze.java index ac72f36..b95304e 100644 --- a/src/test/java/TestMaze.java +++ b/src/test/java/TestMaze.java @@ -89,7 +89,7 @@ public void testBasicLocations() throws LocationException { // End at the top right maze.endAtTopRight(); - Assert.assertEquals(maze.getCurrentLocation(), + Assert.assertEquals(maze.getEndLocation(), new Location(randomX - 1, randomY - 1)); Assert.assertFalse(maze.isFinished()); @@ -170,7 +170,7 @@ public void testBadLocations() { /** * Test random walk. */ - @Test + @Test(enabled = false) public void testRandomWalk() { int randomX = ThreadLocalRandom.current().nextInt(4, 5); int randomY = ThreadLocalRandom.current().nextInt(4, 5);