diff --git a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java index 32667af6c63a0287bd4f91c37682acb30c08e9a4..25e6f8ae4e90b94d178677ba81b4aee4aed7b7c4 100644 --- a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java +++ b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java @@ -19,7 +19,7 @@ import java.util.function.Consumer; * * Notice that: * - * - for each row, there is one less horizontal than the number of corner dots + * - for each row, there is one less horizontal than the numbgraer of corner dots * - for each row, there are as many verticals as there are corner dots * - for each row, there is one less box than the number of corner dots * - for each column, there is one less vertical than the number of corner dots. @@ -111,9 +111,18 @@ public class DotsAndBoxesGrid { return false; } + else if((getHorizontal(x, y)) && (getHorizontal(x, y+1)) && (getVertical(x, y)) + && (getVertical(x + 1, y))) + { + return true; + } + else + { + return false; + } + // 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; + // FIXME: You'll need to fix this code (after writing a test first) } /** Tries to claim a box for a player. If the box is complete, sets the ownership and returns true. */ @@ -142,6 +151,11 @@ public class DotsAndBoxesGrid { // FIXME: You need to throw an exception if the line was already drawn. + if(getHorizontal(x, y)) + { + throw new IndexOutOfBoundsException("That connection has already been drawn"); + } + this.horizontals[x][y] = true; // Try to claim the north or south boxes @@ -173,6 +187,12 @@ public class DotsAndBoxesGrid { // You need to throw an exception if the line was already drawn. + if(getVertical(x, y)) + { + throw new IndexOutOfBoundsException("That connection has already been drawn"); + } + + this.verticals[x][y] = true; // Try to claim the north or south boxes boolean claimE = claimBox(x, y, player); diff --git a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java index 31d751b95c9fc7e2f7ad5dc98f4f63d7e9796e40..daa4b478907b659bd7275afd3469d8ff28fd96de 100644 --- a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java +++ b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java @@ -36,7 +36,7 @@ public class DotsAndBoxesGridTest { public void testLineDraw(){ //This test will draw the lines that will also be used in the second test. It should draw the box - //then attempt to draw one of the lines again. + //then attempt to draw one of the lines again. logger.info("Testing duplicate line drawing"); @@ -45,7 +45,8 @@ public class DotsAndBoxesGridTest { grid.drawVertical(0,0,1); grid.drawVertical(1,0,1); - assertFalse(grid.drawHorizontal(0, 0, 1)); + assertThrows(IndexOutOfBoundsException.class, () -> { grid.drawHorizontal(0, 0, 1);} ); + //assertFalse(grid.drawHorizontal(0, 0, 1)); } @@ -54,6 +55,12 @@ public class DotsAndBoxesGridTest { public void testBoxComplete() { // will need to draw a box, then give it to box complete and assert that it should be true + + grid.drawHorizontal(0, 0, 1); + grid.drawHorizontal(0,1, 1); + grid.drawVertical(0,0,1); + grid.drawVertical(1,0,1); + logger.info("Testing box completion"); assertTrue(grid.boxComplete(0,0)); assertFalse(grid.boxComplete(2,2));