diff --git a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java index a9e7c5b6639e8a2a8728809d9b17c8d155baf9b6..e26912b484f3da35e3a7684454d1295cb2c35369 100644 --- a/src/main/java/dotsandboxes/DotsAndBoxesGrid.java +++ b/src/main/java/dotsandboxes/DotsAndBoxesGrid.java @@ -105,15 +105,13 @@ public class DotsAndBoxesGrid { * @param y coordinate of the top of the box * @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; + public boolean boxComplete(int x, int y) { + if (x >= width - 1 || x < 0 || y >= height - 1 || y < 0) { + return false; } + return getHorizontal(x, y) && getHorizontal(x, y + 1) && getVertical(x, y) && getVertical(x + 1, y); +} + /** 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) { @@ -131,30 +129,32 @@ public class DotsAndBoxesGrid { * @param y * @return true if it completes a box */ - public boolean drawHorizontal(int x, int y, int player) { - 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 >= height || y < 0) { - throw new IndexOutOfBoundsException(String.format("y was %d, which is out of range. Range is 0 to %d", y, height)); - } + public boolean drawHorizontal(int x, int y, int player) { + 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 >= 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. + if (horizontals[x][y]) { + throw new IllegalStateException("Line already drawn"); + } - this.horizontals[x][y] = true; + 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; - } + boolean claimN = claimBox(x, y-1, player); + boolean claimS = claimBox(x, y, player); + if (claimN || claimS) { + notifyObservers(); + return true; + } else { + nextPlayer(); + notifyObservers(); + return false; } +} + /** * "Draws" a vertical line, from grid point (x, y) to (x, y + 1). (i.e. sets that line to true) @@ -162,31 +162,30 @@ public class DotsAndBoxesGrid { * @param y * @return true if it completes a box */ - public boolean drawVertical(int x, int y, int player) { - if (x >= width || x < 0) { - throw new IndexOutOfBoundsException(String.format("x was %d, which is out of range. Range is 0 to %d", x, width)); - } - 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)); - } - - // 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; - } + public boolean drawVertical(int x, int y, int player) { + if (x >= width || x < 0) { + throw new IndexOutOfBoundsException(String.format("x was %d, which is out of range. Range is 0 to %d", x, width)); + } + 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)); + } + if (verticals[x][y]) { + throw new IllegalStateException("Line already drawn"); } + this.verticals[x][y] = true; + boolean claimE = claimBox(x, y, player); + boolean claimW = claimBox(x-1, y, player); + if (claimE || claimW) { + notifyObservers(); + return true; + } else { + nextPlayer(); + notifyObservers(); + return false; + } +} public boolean gameComplete() { // Students who took COSC250 might recognise this style of code. This is Java's version of higher order functions. // The grid is complete if "for all rows, all the boxes in that row have a non-zero owner"