From 993ab9e1fc915fc601692845d5c0add964afc7c4 Mon Sep 17 00:00:00 2001 From: Nathan Reeves <nreeves5@myune.edu.au> Date: Sun, 25 Jul 2021 14:38:42 +1000 Subject: [PATCH] Committing bug fixed code to main branch --- src/main/java/dotsandboxes/DotsAndBoxesGrid.java | 14 ++++++++++++-- .../java/dotsandboxes/DotsAndBoxesGridTest.java | 4 ++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java index 32667af..99babeb 100644 --- a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java +++ b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java @@ -112,8 +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; + if (this.horizontals[x][y] == true && this.horizontals[x][y+1] == true && this.verticals[x][y] == true && this.verticals[x+1][y] == true) { + return true; + } + + return false; } /** Tries to claim a box for a player. If the box is complete, sets the ownership and returns true. */ @@ -141,6 +144,10 @@ public class DotsAndBoxesGrid { } // FIXME: You need to throw an exception if the line was already drawn. + if (this.horizontals[x][y] == true) { + throw new IllegalStateException(String.format("IllegalStateException - This line has already been drawn")); + } + this.horizontals[x][y] = true; @@ -172,6 +179,9 @@ public class DotsAndBoxesGrid { } // You need to throw an exception if the line was already drawn. + if (this.verticals[x][y]) { + throw new IllegalStateException(String.format("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 bca1591..64b3674 100644 --- a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java +++ b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java @@ -32,13 +32,13 @@ public class DotsAndBoxesGridTest { @Test public void testTestingBoxComplete() { logger.info("Test of box completion test"); - logger.info("Drawing complete box, then asserting incomplete state"); + logger.info("Drawing complete box, then asserting complete state"); DotsAndBoxesGrid g = new DotsAndBoxesGrid(4,4,1); g.drawHorizontal(1,1, 1); g.drawHorizontal(1,2,1); g.drawVertical(1,1,1); g.drawVertical(2,1,1); - assertEquals(false,g.boxComplete(1,1)); + assertEquals(true,g.boxComplete(1,1)); } @Test -- GitLab