Skip to content
Snippets Groups Projects
Commit 222de015 authored by Krishkumar Sandipkumar Patel's avatar Krishkumar Sandipkumar Patel
Browse files

Added more comments to DotsAndBoxesGrid.java class to understand the code better

parent 2fa845ba
No related branches found
No related tags found
No related merge requests found
...@@ -66,12 +66,16 @@ public class DotsAndBoxesGrid { ...@@ -66,12 +66,16 @@ public class DotsAndBoxesGrid {
this.width = width; this.width = width;
this.height = height; this.height = height;
this.players = players; this.players = players;
/** For 4 by 3 grid, horiz is [3][3] array */
this.horizontals = new boolean[width - 1][height]; this.horizontals = new boolean[width - 1][height];
/** For 4 by 3 grid, verti is [4][2] array */
this.verticals = new boolean[width][height - 1]; this.verticals = new boolean[width][height - 1];
/** forms [3][2] boxes */
this.boxOwners = new int[width - 1][height - 1]; 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() { private void notifyObservers() {
for (Consumer<DotsAndBoxesGrid> consumer : watchers) { for (Consumer<DotsAndBoxesGrid> consumer : watchers) {
consumer.accept(this); consumer.accept(this);
...@@ -106,6 +110,7 @@ public class DotsAndBoxesGrid { ...@@ -106,6 +110,7 @@ public class DotsAndBoxesGrid {
* @return true if all four sides have been drawn. * @return true if all four sides have been drawn.
*/ */
public boolean boxComplete(int x, int y) { 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) { if (x >= width - 1 || x < 0 || y >= height - 1 || y < 0) {
return false; return false;
} }
...@@ -117,11 +122,11 @@ public class DotsAndBoxesGrid { ...@@ -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. */ /** 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) { 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; boxOwners[x][y] = player;
return true; return true; // claimed the box
} else { } else {
return false; return false; // couldn't claim the box
} }
} }
...@@ -132,21 +137,23 @@ public class DotsAndBoxesGrid { ...@@ -132,21 +137,23 @@ public class DotsAndBoxesGrid {
* @return true if it completes a box * @return true if it completes a box
*/ */
public boolean drawHorizontal(int x, int y, int player) { public boolean drawHorizontal(int x, int y, int player) {
/** if x coordinate is out of bounds */
if (x >= width - 1 || x < 0) { 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)); 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) { 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)); 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. // 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 // 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); boolean claimS = claimBox(x, y, player);
if (claimN || claimS) { if (claimN || claimS) { // upon drawing a horizontal line, is North or South box complete
notifyObservers(); notifyObservers();
return true; return true;
} else { } else {
...@@ -170,10 +177,12 @@ public class DotsAndBoxesGrid { ...@@ -170,10 +177,12 @@ public class DotsAndBoxesGrid {
throw new IndexOutOfBoundsException(String.format("y was %d, which is out of range. Range is 0 to %d", y, height - 1)); 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. // You need to throw an exception if the line was already drawn.
this.verticals[x][y] = true; this.verticals[x][y] = true; // otherwise set the vertical line to true
// Try to claim the north or south boxes
// Try to claim the East or West boxes
boolean claimE = claimBox(x, y, player); boolean claimE = claimBox(x, y, player);
boolean claimW = claimBox(x-1, y, player); boolean claimW = claimBox(x-1, y, player);
if (claimE || claimW) { if (claimE || claimW) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment