From 5e1e2fb27f1e54b6dcac18f17f9bd659dad6abcf Mon Sep 17 00:00:00 2001
From: Martin Schreiber <mschreib@myune.edu.au>
Date: Thu, 22 Jul 2021 17:36:30 +0930
Subject: [PATCH] fixed issues

---
 .../java/dotsandboxes/DotsAndBoxesGrid.java   | 69 +++++++++++--------
 1 file changed, 40 insertions(+), 29 deletions(-)

diff --git a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java
index 32667af..a0e915e 100644
--- a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java
+++ b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java
@@ -110,10 +110,12 @@ public class DotsAndBoxesGrid {
         if (x >= width - 1 || x < 0 || y >= height - 1 || y < 0) {
             return false;
         }
+        if (getHorizontal(x,y) && getHorizontal(x,y+1) && getVertical(x+1,y) && getVertical(x,y)){
+            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.
-        // FIXME: You'll need to fix this code (after writing a test first).
-        return true;
     }
 
     /** Tries to claim a box for a player. If the box is complete, sets the ownership and returns true. */
@@ -140,21 +142,26 @@ 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.
+        if(this.horizontals[x][y]){
+            throw new RuntimeException("Line already drawn.");
+
+        }else{
+            this.horizontals[x][y] = true;
+
+            // Try to claim the north or south boxes
+            boolean claimN = claimBox(x, y-1, player);
+            boolean claimS = claimBox(x, y, player);
+            if (claimN || claimS) {
+                notifyObservers();
+                return true;
+            } else {
+                nextPlayer();
+                notifyObservers();
+                return false;
+            }
+        }
 
-        this.horizontals[x][y] = true;
 
-        // Try to claim the north or south boxes
-        boolean claimN = claimBox(x, y-1, player);
-        boolean claimS = claimBox(x, y, player);
-        if (claimN || claimS) {
-            notifyObservers();
-            return true;
-        } else {
-            nextPlayer();
-            notifyObservers();
-            return false;
-        }
     }
 
     /**
@@ -171,19 +178,23 @@ 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.
-
-        this.verticals[x][y] = true;
-        // Try to claim the north or south boxes
-        boolean claimE = claimBox(x, y, player);
-        boolean claimW = claimBox(x-1, y, player);
-        if (claimE || claimW) {
-            notifyObservers();
-            return true;
-        } else {
-            nextPlayer();
-            notifyObservers();
-            return false;
+        if(this.verticals[x][y]){
+            throw new RuntimeException("Line already drawn.");
+
+        }else{
+            this.verticals[x][y] = true;
+
+            // Try to claim the north or south boxes
+            boolean claimN = claimBox(x, y-1, player);
+            boolean claimS = claimBox(x, y, player);
+            if (claimN || claimS) {
+                notifyObservers();
+                return true;
+            } else {
+                nextPlayer();
+                notifyObservers();
+                return false;
+            }
         }
 
     }
-- 
GitLab