Skip to content
Snippets Groups Projects
Commit a4fb7d85 authored by gbrown31's avatar gbrown31
Browse files

Fix bugs in code

 - Changed boxComplete method to return a bool indicating if all cardinal directions have lines drawn
 - Changed drawVertical and drawHorizontal to check the (x,y) coordinates to see if a line has already been drawn, and throws if true
parent 38836473
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;
......@@ -50,6 +49,7 @@ public class DotsAndBoxesGrid {
final int players;
private int player = 1;
public int getPlayer() {
return player;
}
......@@ -98,7 +98,6 @@ 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
......@@ -111,10 +110,18 @@ public class DotsAndBoxesGrid {
}
// 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).
if (
getHorizontal(x, y) &&
getVertical(x, y) &&
getHorizontal(x, y + 1) &&
getVertical(x + 1, y)
) {
return true;
}
return false;
}
/** 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)) {
......@@ -133,13 +140,33 @@ public class DotsAndBoxesGrid {
*/
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 RuntimeException(
String.format(
"Error: Horizontal line has already been drawn at (%d,%d).",
x,
y
)
);
}
this.horizontals[x][y] = true;
......@@ -164,13 +191,33 @@ public class DotsAndBoxesGrid {
*/
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 RuntimeException(
String.format(
"Error: Vertical line has already been drawn at (%d,%d).",
x,
y
)
);
}
this.verticals[x][y] = true;
// Try to claim the north or south boxes
......@@ -184,14 +231,13 @@ public class DotsAndBoxesGrid {
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"
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