diff --git a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java index 32667af6c63a0287bd4f91c37682acb30c08e9a4..99babeb42962386d105fdbb7152c6cd10338bf30 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 bca1591613cb005fb51cdb9ba11bab337c715b0e..64b3674dbfcf90e41fc489f885ba7fc215078c0d 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