From dbd7b6b59147439d3e5d1f91106b9a259a9289ec Mon Sep 17 00:00:00 2001 From: Steve Mckinnon <smckin27@myune.edu.au> Date: Tue, 20 Jul 2021 13:13:15 +1000 Subject: [PATCH] fixed duplicate line and complete box bugs --- src/main/java/dotsandboxes/DotsAndBoxesGrid.java | 15 ++++++++++++--- .../java/dotsandboxes/DotsAndBoxesGridTest.java | 6 +++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java index 32667af..2918331 100644 --- a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java +++ b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java @@ -113,7 +113,10 @@ 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; + if (this.horizontals[x][y] && this.horizontals[x][y + 1] && this.verticals[x][y] && this.verticals[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,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)); } - // 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; @@ -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)); } - // 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; // 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 6030871..8ea1bda 100644 --- a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java +++ b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java @@ -17,7 +17,7 @@ public class DotsAndBoxesGridTest { /* * 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 { grid.drawVertical(1,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, - * test should throw an exception + * test should throw an exception. */ @Test public void testRedrawLineFails() { -- GitLab