diff --git a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java index a9e7c5b6639e8a2a8728809d9b17c8d155baf9b6..6de0ef38d710d38df2aaa4049f35563b5cc26116 100644 --- a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java +++ b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java @@ -112,7 +112,11 @@ public class DotsAndBoxesGrid { // 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). - return true; + else if (getHorizontal(x, y) && getHorizontal(x, y + 1) + && getVertical(x, y) && getVertical(x + 1, y)) { + return true; + } + return false; } /** Tries to claim a box for a player. If the box is complete, sets the ownership and returns true. */ @@ -140,6 +144,9 @@ public class DotsAndBoxesGrid { } // FIXME: You need to throw an exception if the line was already drawn. + if (horizontals[x][y]) { + throw new IllegalStateException("Line has been already drawn!"); + } this.horizontals[x][y] = true; @@ -171,8 +178,11 @@ public class DotsAndBoxesGrid { } // You need to throw an exception if the line was already drawn. - + if (verticals[x][y]) { + throw new IllegalStateException("Line has been already drawn!"); + } this.verticals[x][y] = true; + // Try to claim the north or south boxes boolean claimE = claimBox(x, y, player); boolean claimW = claimBox(x-1, y, player); diff --git a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java index b465c33da9d326f9b9ffbb2ac787560767bb813b..8818853b35412ad8647b75ddfb5715ce2da3bb00 100644 --- a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java +++ b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java @@ -24,7 +24,8 @@ public class DotsAndBoxesGridTest { * * This is a dummy test just to show that the test suite itself runs */ - public void setUp() { + @BeforeEach + public void setup() { // Setting up a new DotsAndBoxesGrid for every test gridTest = new DotsAndBoxesGrid(4, 4, 2); } @@ -37,41 +38,24 @@ public class DotsAndBoxesGridTest { // FIXME: You need to write tests for the two known bugs in the code. @Test - public void testScoreCalculation() { - logger.info("Testing score calculation on box completion"); + public void testBoxCompletion() { + logger.info("Testing box completion"); - DotsAndBoxesGrid grid = new DotsAndBoxesGrid(4, 3, 2); - grid.drawHorizontal(0, 0, 1); - grid.drawVertical(0, 0, 1); - grid.drawVertical(1, 0, 1); - boolean boxCompleted = grid.drawHorizontal(1, 0, 1); // This should complete the box + gridTest.drawHorizontal(0, 0, 1); + gridTest.drawVertical(0, 0, 1); + gridTest.drawHorizontal(0, 1, 1); + gridTest.drawVertical(1, 0, 1); - // Check if the box was completed and claimed by the correct player - assertTrue(boxCompleted, "Box should be completed and claimed"); - assertEquals(1, grid.getBoxOwner(0, 0), "The box owner should be player 1"); - } - - @Test - public void testInvalidMoveHandling() { - logger.info("Testing invalid move handling"); + assertTrue(gridTest.boxComplete(0,0), "Box should be completed"); - DotsAndBoxesGrid grid = new DotsAndBoxesGrid(4, 3, 2); - grid.drawHorizontal(0, 0, 1); - // Attempt to draw the same horizontal line again - Exception exception = assertThrows(IndexOutOfBoundsException.class, () -> { - grid.drawHorizontal(0, 0, 1); // This should throw an exception - }); - - assertEquals("Line has already been drawn", exception.getMessage()); } @Test - public void testBoxCompletionLogic() { - logger.info("Testing box completion logic"); + public void testVerticalLineRedrawn() { + logger.info("Testing if throws exception if redrawn a line "); + gridTest.drawVertical(0, 0, 1); + + assertThrows(IllegalStateException.class, () -> gridTest.drawVertical(0, 0, 1)); - DotsAndBoxesGrid grid = new DotsAndBoxesGrid(4, 3, 2); - // Attempt to complete a box without drawing all lines - boolean isComplete = grid.boxComplete(0, 0); - assertFalse(isComplete, "Box should not be complete without all lines drawn"); } }