From f0a9799171e919288b43c61219f7435203f03777 Mon Sep 17 00:00:00 2001 From: Jennica Llamera <jllamera@myune.edu.au> Date: Wed, 31 Jul 2024 21:02:50 +1000 Subject: [PATCH] Fixed box completion and redrawn line errors --- .../java/dotsandboxes/DotsAndBoxesGrid.java | 14 +++++- .../dotsandboxes/DotsAndBoxesGridTest.java | 44 ++++++------------- 2 files changed, 26 insertions(+), 32 deletions(-) diff --git a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java index a9e7c5b..6de0ef3 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 b465c33..8818853 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"); } } -- GitLab