diff --git a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java index d4297bfd65decd4182fc1fcad043bd7120786f70..d68f9136a886ab49a68d7ecafd06ec3f64650319 100644 --- a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java +++ b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java @@ -111,8 +111,12 @@ public class DotsAndBoxesGrid { } // A box is complete if the north and south horizontals and the east and west verticals have all been drawn. + boolean north = horizontals[x][y]; + boolean south = horizontals[x][y + 1]; + boolean east = verticals[x + 1][y]; + boolean west = verticals[x][y]; // FIXME: You'll need to fix this code (after writing a test first). - return false; + return north && south && east && west; } /** Tries to claim a box for a player. If the box is complete, sets the ownership and returns true. */ @@ -140,7 +144,9 @@ public class DotsAndBoxesGrid { } // FIXME: You need to throw an exception if the line was already drawn. - + if (horizontals[x][y]) { + throw new IllegalArgumentException("Already Drawn"); + } this.horizontals[x][y] = true; // Try to claim the north or south boxes @@ -171,6 +177,9 @@ public class DotsAndBoxesGrid { } // You need to throw an exception if the line was already drawn. + if (verticals[x][y]) { + throw new IllegalArgumentException("Already Drawn"); + } this.verticals[x][y] = true; // Try to claim the north or south boxes diff --git a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java index 6d5005ab192e25c1e968ebffe91bf3a74f280efe..68d82f29ec3f738a2ad201b5ae884983b5958935 100644 --- a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java +++ b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java @@ -14,25 +14,29 @@ public class DotsAndBoxesGridTest { @BeforeEach public void setUp() { // Create a new DotsAndBoxesGrid instance before each test - grid = new DotsAndBoxesGrid(4, 3, 2); + grid = new DotsAndBoxesGrid(15, 8, 2); } @Test public void testBoxComplete() { // Test if the boxComplete method works correctly - + grid.drawHorizontal(3,3,1); + grid.drawHorizontal(3,4,1); + grid.drawVertical(3,3,1); + grid.drawVertical(4,3,1); assertTrue(grid.boxComplete(3,3)); } @Test public void whenExceptionThrown_thenAssertionSucceeds() { - Exception exception = assertThrows(Exception.class, () -> { + grid.drawHorizontal(3, 3, 1); + Exception exception = assertThrows(IllegalArgumentException.class, () -> { grid.drawHorizontal(3,3,1); }); String expectedMessage = "Already Drawn"; String actualMessage = exception.getMessage(); - + System.out.println(actualMessage); assertTrue(actualMessage.contains(expectedMessage)); }