Skip to content

Commit

Permalink
Fix to end logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
gchallen committed Sep 12, 2017
1 parent 48d8d9b commit 5921da5
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 15 deletions.
81 changes: 68 additions & 13 deletions src/main/java/edu/illinois/cs/cs125/lib/mazemaker/Maze.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ public void setRelativeDirection(final String setRelativeDirection) {
}

/**
* Instantiates a new cell.
* Create a new cell.
*
* @param setLocation the cell's location
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
}
4 changes: 2 additions & 2 deletions src/test/java/TestMaze.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 5921da5

Please sign in to comment.