From d22a24aa426302e90386c492e1f59d45b5d8a189 Mon Sep 17 00:00:00 2001 From: hkaur28 <hkaur28@myune.edu.au> Date: Sun, 30 Jul 2023 15:06:57 +0500 Subject: [PATCH] Bug fixed --- src/main/java/dotsandboxes/DotsAndBoxesGrid.java | 13 +++++++++++-- .../java/dotsandboxes/DotsAndBoxesGridTest.java | 12 ++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java index d4297bf..d68f913 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 6d5005a..68d82f2 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)); } -- GitLab