From 62480a1d9dc8e9324dd73b6ee377a171357addf8 Mon Sep 17 00:00:00 2001
From: Matthew Thomas <mthoma48@myune.edu.au>
Date: Fri, 23 Jul 2021 16:15:26 -0400
Subject: [PATCH] Initial bugfixes that pass unit tests

---
 .../java/dotsandboxes/DotsAndBoxesGrid.java   | 28 +++++++++++++------
 .../dotsandboxes/DotsAndBoxesGridTest.java    |  8 +++---
 2 files changed, 24 insertions(+), 12 deletions(-)

diff --git a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java
index 32667af..08591b6 100644
--- a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java
+++ b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java
@@ -107,13 +107,20 @@ public class DotsAndBoxesGrid {
      * @return true if all four sides have been drawn.
      */
     public boolean boxComplete(int x, int y) {
-        if (x >= width - 1 || x < 0 || y >= height - 1 || y < 0) {
-            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;
+            if (x >= width - 1 || x < 0 || y >= height - 1 || y < 0) {
+                return false;
+            }
+            else if (this.getVertical(x,y) == true && this.getVertical(x + 1, y) == true) {
+                if (this.getHorizontal(x,y) == true && this.getHorizontal(x, y + 1) == true) {
+                    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
+    return false;
     }
 
     /** Tries to claim a box for a player. If the box is complete, sets the ownership and returns true. */
@@ -142,7 +149,12 @@ public class DotsAndBoxesGrid {
 
         // FIXME: You need to throw an exception if the line was already drawn.
 
-        this.horizontals[x][y] = true;
+        if (this.horizontals[x][y] == true) {
+            throw new RuntimeException("Line already here");
+        }
+        else {
+            this.horizontals[x][y] = true;
+        }
 
         // Try to claim the north or south boxes
         boolean claimN = claimBox(x, y-1, player);
diff --git a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java
index 7631627..8c32f36 100644
--- a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java
+++ b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java
@@ -35,12 +35,12 @@ public class DotsAndBoxesGridTest {
     public void completeBoxTest() {
         logger.info("Test to see if completion algorithm is working");
 
-        DotsAndBoxesGrid grid = new DotsAndBoxesGrid(4,2,2);
+        DotsAndBoxesGrid grid = new DotsAndBoxesGrid(4,3,2);
         // draw a complete square
         grid.drawHorizontal(1,1,1);
-        grid.drawHorizontal(1,2,1);
-        grid.drawVertical(1,2,1);
-        assertTrue(grid.drawVertical(2,2,1));
+        grid.drawHorizontal(1,2,2);
+        grid.drawVertical(1,1,1);
+        assertTrue(grid.drawVertical(2,1,1));
     }
 
     // Test that DotsAndBoxesGrid currently allows drawing a line on an existing line
-- 
GitLab