From 222de015bc538a0d2c3c1bf2e38e29a2a2e041a1 Mon Sep 17 00:00:00 2001
From: Krishkumar Patel <kpatel9@myune.edu.au>
Date: Thu, 17 Aug 2023 23:15:47 +1000
Subject: [PATCH] Added more comments to DotsAndBoxesGrid.java class to
 understand the code better

---
 .../java/dotsandboxes/DotsAndBoxesGrid.java   | 27 ++++++++++++-------
 1 file changed, 18 insertions(+), 9 deletions(-)

diff --git a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java
index a9e7c5b..922fae9 100644
--- a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java
+++ b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java
@@ -66,12 +66,16 @@ public class DotsAndBoxesGrid {
         this.width = width;
         this.height = height;
         this.players = players;
-
+        /** For 4 by 3 grid, horiz is [3][3] array */
         this.horizontals = new boolean[width - 1][height];
+        /** For 4 by 3 grid, verti is [4][2] array */
         this.verticals = new boolean[width][height - 1];
+        /** forms [3][2] boxes */
         this.boxOwners = new int[width - 1][height - 1];
     }
 
+    /** this function called when a user draws a line. Maybe its telling
+     * the interface to perform some animation */
     private void notifyObservers() {
         for (Consumer<DotsAndBoxesGrid> consumer : watchers) {
             consumer.accept(this);
@@ -106,6 +110,7 @@ public class DotsAndBoxesGrid {
      * @return true if all four sides have been drawn.
      */
     public boolean boxComplete(int x, int y) {
+        /** checks if x or y is out of bounds */
         if (x >= width - 1 || x < 0 || y >= height - 1 || y < 0) {
             return false;
         }
@@ -117,11 +122,11 @@ public class DotsAndBoxesGrid {
 
     /** Tries to claim a box for a player. If the box is complete, sets the ownership and returns true. */
     private boolean claimBox(int x, int y, int p) {
-        if (boxComplete(x, y)) {
+        if (boxComplete(x, y)) { // returns true if a BOX is complete
             boxOwners[x][y] = player;
-            return true;
+            return true;  // claimed the box
         } else {
-            return false;
+            return false;  // couldn't claim the box
         }
     }
 
@@ -132,21 +137,23 @@ public class DotsAndBoxesGrid {
      * @return true if it completes a box
      */
     public boolean drawHorizontal(int x, int y, int player) {
+        /** if x coordinate is out of bounds */
         if (x >= width - 1 || x < 0) {
             throw new IndexOutOfBoundsException(String.format("x was %d, which is out of range. Range is 0 to %d", x, width - 1));
         }
+        /** if y coordinate is out of bounds */
         if (y >= height || y < 0) {
             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.
 
-        this.horizontals[x][y] = true;
+        this.horizontals[x][y] = true; // otherwise set the horizontal line to 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) {
+        if (claimN || claimS) { // upon drawing a horizontal line, is North or South box complete
             notifyObservers();
             return true;
         } else {
@@ -169,11 +176,13 @@ public class DotsAndBoxesGrid {
         if (y >= height - 1 || y < 0) {
             throw new IndexOutOfBoundsException(String.format("y was %d, which is out of range. Range is 0 to %d", y, height - 1));
         }
-
+        
+        // FIX this too, whether vertical line was already drawn or not
         // 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
+        this.verticals[x][y] = true; // otherwise set the vertical line to true
+
+        // Try to claim the East or West boxes
         boolean claimE = claimBox(x, y, player);
         boolean claimW = claimBox(x-1, y, player);
         if (claimE || claimW) {
-- 
GitLab