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;
import java.util.ArrayList;
import java.util.Arrays;
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 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.
*
* 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 {
......@@ -50,6 +51,7 @@ public class DotsAndBoxesGrid {
final int players;
private int player = 1;
public int getPlayer() {
return player;
}
......@@ -98,9 +100,9 @@ public class DotsAndBoxesGrid {
return boxOwners[x][y];
}
/**
* Checks whether a box has been fully drawn (all four sides)
*
* @param x coordinate of the left side of the box
* @param y coordinate of the top of the box
* @return true if all four sides have been drawn.
......@@ -109,13 +111,17 @@ public class DotsAndBoxesGrid {
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.
// 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;
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) {
if (boxComplete(x, y)) {
boxOwners[x][y] = player;
......@@ -126,25 +132,32 @@ 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 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));
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));
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 (getHorizontal(x, y)) {
throw new IllegalStateException("This line has already been drawn!");
}
this.horizontals[x][y] = true;
// 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);
if (claimN || claimS) {
notifyObservers();
......@@ -157,25 +170,32 @@ 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 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));
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));
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.
if (getVertical(x, y)) {
throw new IllegalStateException("This line has already been 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);
boolean claimW = claimBox(x - 1, y, player);
if (claimE || claimW) {
notifyObservers();
return true;
......@@ -188,10 +208,11 @@ public class DotsAndBoxesGrid {
}
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"
// 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"
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.
Finish editing this message first!
Please register or to comment