Skip to content
Snippets Groups Projects
Commit dbd7b6b5 authored by Steve McKinnon's avatar Steve McKinnon
Browse files

fixed duplicate line and complete box bugs

parent 4450a8e9
No related branches found
No related tags found
No related merge requests found
...@@ -113,8 +113,11 @@ public class DotsAndBoxesGrid { ...@@ -113,8 +113,11 @@ public class DotsAndBoxesGrid {
// A box is complete if the north and south horizontals and the east and west verticals have all been drawn. // A box is complete if the north and south horizontals and the east and west verticals have all been drawn.
// FIXME: You'll need to fix this code (after writing a test first). // FIXME: You'll need to fix this code (after writing a test first).
if (this.horizontals[x][y] && this.horizontals[x][y + 1] && this.verticals[x][y] && this.verticals[x + 1][y]) {
return true; return true;
} }
return false;
}
/** Tries to claim a box for a player. If the box is complete, sets the ownership and returns true. */ /** Tries to claim a box for a player. If the box is complete, sets the ownership and returns true. */
private boolean claimBox(int x, int y, int p) { private boolean claimBox(int x, int y, int p) {
...@@ -140,7 +143,10 @@ public class DotsAndBoxesGrid { ...@@ -140,7 +143,10 @@ public class DotsAndBoxesGrid {
throw new IndexOutOfBoundsException(String.format("y was %d, which is out of range. Range is 0 to %d", y, height)); throw new IndexOutOfBoundsException(String.format("y was %d, which is out of range. Range is 0 to %d", y, height));
} }
// FIXME: You need to throw an exception if the line was already drawn. // Throw an exception if the line was already drawn.
if (this.horizontals[x][y]) {
throw new IllegalStateException("This line has already been drawn");
}
this.horizontals[x][y] = true; this.horizontals[x][y] = true;
...@@ -171,7 +177,10 @@ public class DotsAndBoxesGrid { ...@@ -171,7 +177,10 @@ public class DotsAndBoxesGrid {
throw new IndexOutOfBoundsException(String.format("y was %d, which is out of range. Range is 0 to %d", y, height - 1)); throw new IndexOutOfBoundsException(String.format("y was %d, which is out of range. Range is 0 to %d", y, height - 1));
} }
// You need to throw an exception if the line was already drawn. // Throw an exception if the line was already drawn.
if (this.verticals[x][y]) {
throw new IllegalStateException("This line has already been drawn");
}
this.verticals[x][y] = true; this.verticals[x][y] = true;
// Try to claim the north or south boxes // Try to claim the north or south boxes
......
...@@ -17,7 +17,7 @@ public class DotsAndBoxesGridTest { ...@@ -17,7 +17,7 @@ public class DotsAndBoxesGridTest {
/* /*
* A test to check if a box is correctly marked as complete. * A test to check if a box is correctly marked as complete.
* Asserting False after drawing a complete box * Asserting True after drawing a complete box
*/ */
...@@ -31,13 +31,13 @@ public class DotsAndBoxesGridTest { ...@@ -31,13 +31,13 @@ public class DotsAndBoxesGridTest {
grid.drawVertical(1,1,1); grid.drawVertical(1,1,1);
grid.drawVertical(2,1,1); grid.drawVertical(2,1,1);
assertFalse(grid.boxComplete(1, 1)); assertTrue(grid.boxComplete(1, 1));
} }
/* /*
* A test to see if a line can be redrawn after it has already been drawn, * A test to see if a line can be redrawn after it has already been drawn,
* test should throw an exception * test should throw an exception.
*/ */
@Test @Test
public void testRedrawLineFails() { public void testRedrawLineFails() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment