Skip to content
Snippets Groups Projects
Commit 385c68ba authored by Rohan Gene Isaac Vance's avatar Rohan Gene Isaac Vance
Browse files

added basic tests

parent 9d9ff935
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.
......@@ -110,12 +112,18 @@ public class DotsAndBoxesGrid {
return false;
}
// A box is complete if the north and south horizontals and the east and west verticals have all been drawn.
// x < 0 || x > width
// 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;
}
/** 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,17 +134,21 @@ 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.
......@@ -157,17 +169,21 @@ 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.
......@@ -188,10 +204,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));
}
}
......@@ -9,8 +9,8 @@ public class Main {
public static void main(String... args) throws Exception {
JFrame mainWindow = new JFrame("Dots and Boxes");
// DotsAndBoxesGrid grid = new DotsAndBoxesGrid(15, 8, 2);
DotsAndBoxesGrid grid = new DotsAndBoxesGrid(15, 8, 2);
// FIXME: Update this label to show your name and student number
JLabel label = new JLabel("Name: (Rohan Vance 220188799)");
......
......@@ -24,11 +24,33 @@ public class DotsAndBoxesGridTest {
*
* This is a dummy test just to show that the test suite itself runs
*/
@Test
public void boxCompleteDetectsCompletedBoxes() {
DotsAndBoxesGrid case1 = new DotsAndBoxesGrid(5, 5, 2);
case1.drawHorizontal(0, 0, 0);
case1.drawHorizontal(0, 0, 0);
case1.drawVertical(0, 0, 1);
case1.drawHorizontal(0, 1, 0);
case1.drawVertical(1, 0, 1);
assertTrue(case1.boxComplete(0, 0));
}
@Test
public void boxCompleteDetectsIncompleteBoxes() {
DotsAndBoxesGrid case1 = new DotsAndBoxesGrid(5, 5, 2);
case1.drawHorizontal(0, 0, 0);
case1.drawVertical(0, 0, 1);
case1.drawHorizontal(0, 1, 0);
case1.drawVertical(1, 0, 1);
assertFalse(case1.boxComplete(2, 2));
}
@Test
public void testTestSuiteRuns() {
logger.info("Dummy test to show the test suite runs");
assertTrue(true);
public void drawMethodsDetectRedrawnLines() {
DotsAndBoxesGrid case2 = new DotsAndBoxesGrid(5, 5, 2);
case2.drawHorizontal(0, 0, 0);
assertThrows(RuntimeException.class, () -> case2.drawHorizontal(0, 0, 0));
}
// FIXME: You need to write tests for the two known bugs in the code.
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment