diff --git a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java index 1946beda974d180686c65c0259a7b881e9a4eb5a..20f12cefafae668a90fb6f1ae5a20afda7767db1 100644 --- a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java +++ b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java @@ -27,5 +27,150 @@ public class DotsAndBoxesGridTest { assertTrue(true); } - // FIXME: You need to write tests for the two known bugs in the code. + /** + * Tests correct detection of completed boxes + */ + @Test + public void boxCompleteDetectsCompletedBoxes() { + // test top left corner box of grid + DotsAndBoxesGrid case1 = new DotsAndBoxesGrid(5, 5, 2); + case1.drawHorizontal(0, 0, 0); + case1.drawVertical(0, 0, 1); + case1.drawHorizontal(0, 1, 0); + case1.drawVertical(1, 0, 1); + + assertTrue(case1.boxComplete(0,0)); + + // test bottom right corner box of grid + DotsAndBoxesGrid case2 = new DotsAndBoxesGrid(5, 5, 2); + case2.drawHorizontal(3, 3, 0); + case2.drawVertical(3, 3, 1); + case2.drawHorizontal(3, 4, 0); + case2.drawVertical(4, 3, 1); + + assertTrue(case2.boxComplete(3,3)); + + // test grid with 1 box + DotsAndBoxesGrid case3 = new DotsAndBoxesGrid(2, 2, 2); + case3.drawHorizontal(0, 0, 0); + case3.drawVertical(0, 0, 1); + case3.drawHorizontal(0, 1, 0); + case3.drawVertical(1, 0, 1); + + assertTrue(case3.boxComplete(0,0)); + + // test a box around the center of grid + DotsAndBoxesGrid case4 = new DotsAndBoxesGrid(5, 5, 2); + case4.drawHorizontal(2, 2, 0); + case4.drawVertical(2, 2, 1); + case4.drawHorizontal(2, 3, 0); + case4.drawVertical(3, 2, 1); + + assertTrue(case4.boxComplete(2,2)); + } + + /** + * Tests correct detection of incomplete boxes + */ + @Test + public void boxCompleteDetectsIncompleteBoxes() { + // test top left corner box of grid missing top horizontal line + DotsAndBoxesGrid case1 = new DotsAndBoxesGrid(5, 5, 2); + case1.drawVertical(0, 0, 1); + case1.drawHorizontal(0, 1, 0); + case1.drawVertical(1, 0, 1); + + assertFalse(case1.boxComplete(0,0)); + + // test top left corner box of grid missing bottom horizontal line + DotsAndBoxesGrid case2 = new DotsAndBoxesGrid(5, 5, 2); + case2.drawHorizontal(0, 0, 0); + case2.drawVertical(0, 0, 1); + case2.drawVertical(1, 0, 1); + + assertFalse(case2.boxComplete(0,0)); + + // test top left corner box of grid missing left vertical line + DotsAndBoxesGrid case3 = new DotsAndBoxesGrid(5, 5, 2); + case3.drawHorizontal(0, 0, 0); + case3.drawHorizontal(0, 1, 0); + case3.drawVertical(1, 0, 1); + + assertFalse(case3.boxComplete(0,0)); + + // test top left corner box of grid missing right vertical line + DotsAndBoxesGrid case4 = new DotsAndBoxesGrid(5, 5, 2); + case4.drawHorizontal(0, 0, 0); + case4.drawVertical(0, 0, 1); + case4.drawHorizontal(0, 1, 0); + + assertFalse(case4.boxComplete(0,0)); + + // test bottom right corner box of grid missing top horizontal line + DotsAndBoxesGrid case5 = new DotsAndBoxesGrid(5, 5, 2); + case5.drawVertical(3, 3, 1); + case5.drawHorizontal(3, 4, 0); + case5.drawVertical(4, 3, 1); + + assertFalse(case5.boxComplete(3,3)); + + // test grid with 1 box missing top horizontal line + DotsAndBoxesGrid case6 = new DotsAndBoxesGrid(2, 2, 2); + case6.drawVertical(0, 0, 1); + case6.drawHorizontal(0, 1, 0); + case6.drawVertical(1, 0, 1); + + assertFalse(case6.boxComplete(0,0)); + + // test a box around the center of grid missing top horizontal line + DotsAndBoxesGrid case7 = new DotsAndBoxesGrid(5, 5, 2); + case7.drawVertical(2, 2, 1); + case7.drawHorizontal(2, 3, 0); + case7.drawVertical(3, 2, 1); + + assertFalse(case7.boxComplete(2,2)); + } + + /** + * Tests that IllegalStateException is thrown when drawHorizontal() or drawVertical() is called + * on a line that was already drawn. + */ + @Test + public void drawMethodsDetectRedrawnLines() { + DotsAndBoxesGrid case1 = new DotsAndBoxesGrid(5, 5, 2); + + // tests drawing on previously-drawn horizontal lines + + case1.drawHorizontal(0, 0, 0); + assertThrows(IllegalStateException.class, () -> { + case1.drawHorizontal(0,0,1); + }); + + case1.drawHorizontal(2, 2, 0); + assertThrows(IllegalStateException.class, () -> { + case1.drawHorizontal(2,2,1); + }); + + case1.drawHorizontal(3, 4, 0); + assertThrows(IllegalStateException.class, () -> { + case1.drawHorizontal(3,4,1); + }); + + // tests drawing on previously-drawn vertical lines + + case1.drawVertical(0, 0, 0); + assertThrows(IllegalStateException.class, () -> { + case1.drawVertical(0,0,1); + }); + + case1.drawVertical(2, 2, 0); + assertThrows(IllegalStateException.class, () -> { + case1.drawVertical(2,2,1); + }); + + case1.drawVertical(4, 3, 0); + assertThrows(IllegalStateException.class, () -> { + case1.drawVertical(4,3,1); + }); + } }