diff --git a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java index 1946beda974d180686c65c0259a7b881e9a4eb5a..4bf9fcf8df3e421f791dbcf250ddf8d9fc77ebc4 100644 --- a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java +++ b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java @@ -7,6 +7,8 @@ import static org.junit.jupiter.api.Assumptions.*; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import java.util.Random; + public class DotsAndBoxesGridTest { /* * Because Test classes are classes, they can have fields, and can have static fields. @@ -28,4 +30,54 @@ public class DotsAndBoxesGridTest { } // FIXME: You need to write tests for the two known bugs in the code. + + @Test + public void testBoxComplete() { + logger.info("Test to show that game identifies when box is complete"); + Random rand= new Random(); + int width = rand.nextInt(100)+3; + int height = rand.nextInt(100)+3; + DotsAndBoxesGrid testGrid = new DotsAndBoxesGrid(width, height, 2); + int x = rand.nextInt(testGrid.width-1); + int y = rand.nextInt(testGrid.height-1); + int a = rand.nextInt(testGrid.width-1); + int b = rand.nextInt(testGrid.height-1); + int c = rand.nextInt(testGrid.width-1); + int d = rand.nextInt(testGrid.height-1); + // A complete square which should assert to True + testGrid.drawHorizontal(x, y, 1); + testGrid.drawHorizontal(x, y+1, 1); + testGrid.drawVertical(x, y, 1); + testGrid.drawVertical(x+1, y, 2); + // An incomplete square which should assert to False + testGrid.drawHorizontal(a, b, 2); + testGrid.drawVertical(a+1, b, 1); + testGrid.drawVertical(a, b, 1); + assertTrue(testGrid.boxComplete(x, y)); + assertFalse(testGrid.boxComplete(a, b)); + // An unattempted square which should assert to False + assertFalse(testGrid.boxComplete(c, d)); + } + + @Test + public void testLineAlreadyDrawn() { + logger.info("Test to make sure exception is thrown when playing tries to draw an line that has already been drawn"); + DotsAndBoxesGrid testGrid = new DotsAndBoxesGrid(15, 8, 2); + Random rand= new Random(); + int x = rand.nextInt(testGrid.width-1); + int y = rand.nextInt(testGrid.height-1); + int a = rand.nextInt(testGrid.width-1); + int b = rand.nextInt(testGrid.height-1); + testGrid.drawHorizontal(x, y, 1); + testGrid.drawVertical(a, b, 2); + assertThrows(IllegalStateException.class, () -> testGrid.drawHorizontal(x,y, 1), "There " + + "is already a line drawn here."); + assertThrows(IllegalStateException.class, () -> testGrid.drawHorizontal(x,y, 2), "There " + + "is already a line drawn here."); + assertThrows(IllegalStateException.class, () -> testGrid.drawVertical(a,b, 1), "There " + + "is already a line drawn here."); + assertThrows(IllegalStateException.class, () -> testGrid.drawVertical(a,b, 2), "There " + + "is already a line drawn here."); + + } }