From 57597a4acce561cfb99dd82e58cfd176ae08f7d9 Mon Sep 17 00:00:00 2001
From: Scott Crooks <scrooks2@myune.edu.au>
Date: Thu, 1 Aug 2024 20:29:35 +1000
Subject: [PATCH] Fixed bugs in boxComplete and drawVertical/Horizontal

---
 src/main/java/dotsandboxes/DotsAndBoxesGrid.java   | 14 ++++++++++----
 .../java/dotsandboxes/DotsAndBoxesGridTest.java    |  2 +-
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java
index a9e7c5b..43210de 100644
--- a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java
+++ b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java
@@ -110,9 +110,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;
+        // Could be shorter but this is more readable
+        boolean north = horizontals[x][y];
+        boolean south = horizontals[x][y + 1];
+        boolean west = verticals[x][y];
+        boolean east = verticals[x + 1][y];
+
+        return north && south && west && east;
     }
 
     /** Tries to claim a box for a player. If the box is complete, sets the ownership and returns true. */
@@ -139,7 +143,8 @@ 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.
+        // You need to throw an exception if the line was already drawn.
+        if (horizontals[x][y]) throw new IllegalArgumentException("Line is already taken.");
 
         this.horizontals[x][y] = true;
 
@@ -171,6 +176,7 @@ public class DotsAndBoxesGrid {
         }
 
         // You need to throw an exception if the line was already drawn.
+        if (verticals[x][y]) throw new IllegalArgumentException("Line is already taken.");
 
         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 ee51c59..d7454fb 100644
--- a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java
+++ b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java
@@ -78,7 +78,7 @@ public class DotsAndBoxesGridTest {
         DotsAndBoxesGrid grid = new DotsAndBoxesGrid(3, 3, 2);
         grid.drawHorizontal(1, 1, 1);
 
-        Exception exception = assertThrows(IllegalStateException.class, () -> {
+        Exception exception = assertThrows(IllegalArgumentException.class, () -> {
             grid.drawHorizontal(1, 1, 2);
         });
 
-- 
GitLab