Skip to content
Snippets Groups Projects
Commit c6030892 authored by Angus Cuskelly's avatar Angus Cuskelly
Browse files

Fixed bugs in issue

parent 63e600dd
No related branches found
No related tags found
No related merge requests found
package dotsandboxes; package dotsandboxes;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.function.Consumer; import java.util.function.Consumer;
...@@ -25,11 +24,13 @@ import java.util.function.Consumer; ...@@ -25,11 +24,13 @@ import java.util.function.Consumer;
* - for each column, there are as many horizontals as there are corner dots. * - for each column, there are as many horizontals as there are corner dots.
* - for each column, there is one less box than the number of corner dots * - for each column, there is one less box than the number of corner dots
* *
* For example, in this (4, 3) grid, there are (3, 3) horizontal lines, and (4, 2) vertical lines, and (3, 2) boxes. * For example, in this (4, 3) grid, there are (3, 3) horizontal lines, and (4,
* 2) vertical lines, and (3, 2) boxes.
* *
* We number all lines and boxes by their top-left coordinate. * We number all lines and boxes by their top-left coordinate.
* *
* In Java 14+, we might use a Record class for this, but we're using 11+ as an LTS version, so we don't have that yet. * In Java 14+, we might use a Record class for this, but we're using 11+ as an
* LTS version, so we don't have that yet.
*/ */
public class DotsAndBoxesGrid { public class DotsAndBoxesGrid {
...@@ -50,6 +51,7 @@ public class DotsAndBoxesGrid { ...@@ -50,6 +51,7 @@ public class DotsAndBoxesGrid {
final int players; final int players;
private int player = 1; private int player = 1;
public int getPlayer() { public int getPlayer() {
return player; return player;
} }
...@@ -98,9 +100,9 @@ public class DotsAndBoxesGrid { ...@@ -98,9 +100,9 @@ public class DotsAndBoxesGrid {
return boxOwners[x][y]; return boxOwners[x][y];
} }
/** /**
* Checks whether a box has been fully drawn (all four sides) * Checks whether a box has been fully drawn (all four sides)
*
* @param x coordinate of the left side of the box * @param x coordinate of the left side of the box
* @param y coordinate of the top of the box * @param y coordinate of the top of the box
* @return true if all four sides have been drawn. * @return true if all four sides have been drawn.
...@@ -109,13 +111,17 @@ public class DotsAndBoxesGrid { ...@@ -109,13 +111,17 @@ public class DotsAndBoxesGrid {
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;
} }
// A box is complete if the north and south horizontals and the east and west
// A box is complete if the north and south horizontals and the east and west verticals have all been drawn. // verticals have all been drawn.
// FIXME: You'll need to fix this code (after writing a test first). // FIXME: You'll need to fix this code (after writing a test first).
return true; 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. */ /**
* 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)) {
boxOwners[x][y] = player; boxOwners[x][y] = player;
...@@ -126,20 +132,27 @@ public class DotsAndBoxesGrid { ...@@ -126,20 +132,27 @@ public class DotsAndBoxesGrid {
} }
/** /**
* "Draws" a horizontal line, from grid point (x, y) to (x + 1, y). (i.e. sets that line to true) * "Draws" a horizontal line, from grid point (x, y) to (x + 1, y). (i.e. sets
* that line to true)
*
* @param x * @param x
* @param y * @param y
* @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 >= 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 >= 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.
if (getHorizontal(x, y)) {
throw new IllegalStateException("This line has already been drawn!");
}
this.horizontals[x][y] = true; this.horizontals[x][y] = true;
...@@ -157,20 +170,27 @@ public class DotsAndBoxesGrid { ...@@ -157,20 +170,27 @@ public class DotsAndBoxesGrid {
} }
/** /**
* "Draws" a vertical line, from grid point (x, y) to (x, y + 1). (i.e. sets that line to true) * "Draws" a vertical line, from grid point (x, y) to (x, y + 1). (i.e. sets
* that line to true)
*
* @param x * @param x
* @param y * @param y
* @return true if it completes a box * @return true if it completes a box
*/ */
public boolean drawVertical(int x, int y, int player) { public boolean drawVertical(int x, int y, int player) {
if (x >= width || x < 0) { 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)); 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) { 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)); 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. // You need to throw an exception if the line was already drawn.
if (getVertical(x, y)) {
throw new IllegalStateException("This line has already been drawn!");
}
this.verticals[x][y] = true; this.verticals[x][y] = true;
// Try to claim the north or south boxes // Try to claim the north or south boxes
...@@ -188,10 +208,11 @@ public class DotsAndBoxesGrid { ...@@ -188,10 +208,11 @@ public class DotsAndBoxesGrid {
} }
public boolean gameComplete() { public boolean gameComplete() {
// Students who took COSC250 might recognise this style of code. This is Java's version of higher order functions. // Students who took COSC250 might recognise this style of code. This is Java's
// The grid is complete if "for all rows, all the boxes in that row have a non-zero owner" // version of higher order functions.
// The grid is complete if "for all rows, all the boxes in that row have a
// non-zero owner"
return Arrays.stream(boxOwners).allMatch((row) -> Arrays.stream(row).allMatch((owner) -> owner > 0)); return Arrays.stream(boxOwners).allMatch((row) -> Arrays.stream(row).allMatch((owner) -> owner > 0));
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment