diff --git a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java index a9e7c5b6639e8a2a8728809d9b17c8d155baf9b6..f273bc44978a86447e2aeb34b7739469ad9440d9 100644 --- a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java +++ b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java @@ -1,6 +1,5 @@ package dotsandboxes; - import java.util.ArrayList; import java.util.Arrays; import java.util.function.Consumer; @@ -50,6 +49,7 @@ public class DotsAndBoxesGrid { final int players; private int player = 1; + public int getPlayer() { return player; } @@ -98,7 +98,6 @@ public class DotsAndBoxesGrid { return boxOwners[x][y]; } - /** * Checks whether a box has been fully drawn (all four sides) * @param x coordinate of the left side of the box @@ -110,9 +109,13 @@ public class DotsAndBoxesGrid { 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; + // Check if all sides of the box are drawn + boolean top = horizontals[x][y]; + boolean bottom = horizontals[x][y + 1]; + boolean left = verticals[x][y]; + boolean right = verticals[x + 1][y]; + + return top && bottom && left && right; } /** Tries to claim a box for a player. If the box is complete, sets the ownership and returns true. */ @@ -139,12 +142,15 @@ public class DotsAndBoxesGrid { throw new IndexOutOfBoundsException(String.format("y was %d, which is out of range. Range is 0 to %d", y, height)); } - // FIXME: You need to throw an exception if the line was already drawn. + // Throw an exception if the line was already drawn + if (horizontals[x][y]) { + throw new IllegalStateException("This horizontal line is already drawn."); + } this.horizontals[x][y] = true; // Try to claim the north or south boxes - boolean claimN = claimBox(x, y-1, player); + boolean claimN = claimBox(x, y - 1, player); boolean claimS = claimBox(x, y, player); if (claimN || claimS) { notifyObservers(); @@ -170,12 +176,16 @@ public class DotsAndBoxesGrid { throw new IndexOutOfBoundsException(String.format("y was %d, which is out of range. Range is 0 to %d", y, height - 1)); } - // You need to throw an exception if the line was already drawn. + // Throw an exception if the line was already drawn + if (verticals[x][y]) { + throw new IllegalStateException("This vertical line is already drawn."); + } this.verticals[x][y] = true; - // Try to claim the north or south boxes + + // Try to claim the east or west boxes boolean claimE = claimBox(x, y, player); - boolean claimW = claimBox(x-1, y, player); + boolean claimW = claimBox(x - 1, y, player); if (claimE || claimW) { notifyObservers(); return true; @@ -184,7 +194,6 @@ public class DotsAndBoxesGrid { notifyObservers(); return false; } - } public boolean gameComplete() { @@ -192,6 +201,4 @@ public class DotsAndBoxesGrid { // The grid is complete if "for all rows, all the boxes in that row have a non-zero owner" return Arrays.stream(boxOwners).allMatch((row) -> Arrays.stream(row).allMatch((owner) -> owner > 0)); } - - } diff --git a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java index 72ff77f9aa08f53841ab8d7d09f03ec66694916c..ccc2378764cc38949d01fef8bb75327573651812 100644 --- a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java +++ b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java @@ -32,19 +32,19 @@ public class DotsAndBoxesGridTest { logger.info("Testing square completion logic"); // Create a new game grid - DotsAndBoxesGrid grid = new DotsAndBoxesGrid(); + DotsAndBoxesGrid grid = new DotsAndBoxesGrid(3, 3, 2); // Draw lines to form a square - grid.drawLine(0, 0, 0, 1); // Top horizontal line - grid.drawLine(0, 0, 1, 0); // Left vertical line - grid.drawLine(0, 1, 1, 1); // Bottom horizontal line - grid.drawLine(1, 0, 1, 1); // Right vertical line + grid.drawHorizontal(0, 0, 1); // Top horizontal line + grid.drawVertical(0, 0, 1); // Left vertical line + grid.drawHorizontal(0, 1, 1); // Bottom horizontal line + grid.drawVertical(1, 0, 1); // Right vertical line // Test if the square at (0, 0) is complete - boolean isComplete = grid.isSquareComplete(0, 0); + boolean isComplete = grid.boxComplete(0, 0); - // The bug is that isSquareComplete always returns true - assertFalse(isComplete, "The square completion check should fail because the method is broken."); + // The test should pass now that the logic is fixed + assertTrue(isComplete, "The square at (0, 0) should be complete."); } @Test @@ -52,14 +52,22 @@ public class DotsAndBoxesGridTest { logger.info("Testing drawing the same line twice throws exception"); // Create a new game grid - DotsAndBoxesGrid grid = new DotsAndBoxesGrid(); + DotsAndBoxesGrid grid = new DotsAndBoxesGrid(3, 3, 2); // Draw a line once - grid.drawLine(0, 0, 0, 1); + grid.drawHorizontal(0, 0, 1); // Attempt to draw the same line again assertThrows(IllegalStateException.class, () -> { - grid.drawLine(0, 0, 0, 1); - }, "Drawing the same line twice should throw IllegalStateException."); + grid.drawHorizontal(0, 0, 1); + }, "Drawing the same horizontal line twice should throw IllegalStateException."); + + // Draw a vertical line once + grid.drawVertical(0, 0, 1); + + // Attempt to draw the same vertical line again + assertThrows(IllegalStateException.class, () -> { + grid.drawVertical(0, 0, 1); + }, "Drawing the same vertical line twice should throw IllegalStateException."); } }