Commit 2f3a6d78 authored by rbirch4's avatar rbirch4
Browse files

Wrote my unit tests for the three methods boxComplete, drawHorizontal and drawVertical.

parent 1d014469
package dotsandboxes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Observer;
import java.util.function.Consumer;
/**
* The state of a dots and boxes grid.
*
* A (4, 3) dots and boxes grid looks like this:
*
* *-*-*-*
* | | | |
* *-*-*-*
* | | | |
* *-*-*-*
*
* Notice that:
*
* - for each row, there is one less horizontal than the number of corner dots
* - for each row, there are as many verticals as there are corner dots
* - for each row, there is one less box than the number of corner dots
* - for each column, there is one less vertical than the number of 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 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.
*/
public class DotsAndBoxesGrid {
final int width;
final int height;
/** The horizontal lines in the grid. True if drawn. */
private boolean[][] horizontals;
/** The vertical lines in the grid. True if drawn. */
private boolean[][] verticals;
/** Which owner (if any) claimed any given box. */
private int[][] boxOwners;
/** A list of functions to notify when there is an update */
private ArrayList<Consumer<DotsAndBoxesGrid>> watchers = new ArrayList<>();
final int players;
private int player = 1;
public int getPlayer() {
return player;
}
/** Moves to the next player */
private void nextPlayer() {
player++;
if (player > players) {
player = 1;
}
}
public DotsAndBoxesGrid(int width, int height, int players) {
this.width = width;
this.height = height;
this.players = players;
this.horizontals = new boolean[width - 1][height];
this.verticals = new boolean[width][height - 1];
this.boxOwners = new int[width - 1][height - 1];
}
private void notifyObservers() {
for (Consumer<DotsAndBoxesGrid> consumer : watchers) {
consumer.accept(this);
}
}
/** Listens to this grid for changes */
public void addConsumer(Consumer<DotsAndBoxesGrid> consumer) {
watchers.add(consumer);
}
/** Returns whether a horizontal line has been drawn */
public boolean getHorizontal(int x, int y) {
return horizontals[x][y];
}
/** Returns whether a vertical line has been drawn */
public boolean getVertical(int x, int y) {
return verticals[x][y];
}
/** Returns which player owns a box. By convention, 0 is unowned. */
public int getBoxOwner(int x, int y) {
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.
*/
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;
}
/** 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;
return true;
} else {
return false;
}
}
/**
* "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));
}
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.
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;
}
}
/**
* "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));
}
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 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));
}
}
package dotsandboxes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Observer;
import java.util.function.Consumer;
/**
* The state of a dots and boxes grid.
*
* A (4, 3) dots and boxes grid looks like this:
*
* *-*-*-*
* | | | |
* *-*-*-*
* | | | |
* *-*-*-*
*
* Notice that:
*
* - for each row, there is one less horizontal than the number of corner dots
* - for each row, there are as many verticals as there are corner dots
* - for each row, there is one less box than the number of corner dots
* - for each column, there is one less vertical than the number of 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 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.
*/
public class DotsAndBoxesGrid {
final int width;
final int height;
/** The horizontal lines in the grid. True if drawn. */
private boolean[][] horizontals;
/** The vertical lines in the grid. True if drawn. */
private boolean[][] verticals;
/** Which owner (if any) claimed any given box. */
private int[][] boxOwners;
/** A list of functions to notify when there is an update */
private ArrayList<Consumer<DotsAndBoxesGrid>> watchers = new ArrayList<>();
final int players;
private int player = 1;
public int getPlayer() {
return player;
}
/** Moves to the next player */
private void nextPlayer() {
player++;
if (player > players) {
player = 1;
}
}
public DotsAndBoxesGrid(int width, int height, int players) {
this.width = width;
this.height = height;
this.players = players;
this.horizontals = new boolean[width - 1][height];
this.verticals = new boolean[width][height - 1];
this.boxOwners = new int[width - 1][height - 1];
}
private void notifyObservers() {
for (Consumer<DotsAndBoxesGrid> consumer : watchers) {
consumer.accept(this);
}
}
/** Listens to this grid for changes */
public void addConsumer(Consumer<DotsAndBoxesGrid> consumer) {
watchers.add(consumer);
}
/** Returns whether a horizontal line has been drawn */
public boolean getHorizontal(int x, int y) {
return horizontals[x][y];
}
/** Returns whether a vertical line has been drawn */
public boolean getVertical(int x, int y) {
return verticals[x][y];
}
/** Returns which player owns a box. By convention, 0 is unowned. */
public int getBoxOwner(int x, int y) {
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.
*/
public boolean boxComplete(int x, int y) {
//Tests for invalid box references.
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 if box complete, false if not.
return 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;
return true;
} else {
return false;
}
}
/**
* "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));
}
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.
this.horizontals[x][y] = true;
//Uses the coords from what was just drawn above to see if the drawn line will complete either a top or bottom box. Checking done through boxComplete called in claimBox.
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)
* @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));
}
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 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));
}
}
package dotsandboxes;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class DotsAndBoxesGridTest {
/*
* Because Test classes are classes, they can have fields, and can have static fields.
* This field is a logger. Loggers are like a more advanced println, for writing messages out to the console or a log file.
*/
private static final Logger logger = LogManager.getLogger(DotsAndBoxesGridTest.class);
/*
* Tests are functions that have an @Test annotation before them.
* The typical format of a test is that it contains some code that does something, and then one
* or more assertions to check that a condition holds.
*
* This is a dummy test just to show that the test suite itself runs
*/
@Test
public void testTestSuiteRuns() {
logger.info("Dummy test to show the test suite runs");
assertTrue(true);
}
// FIXME: You need to write tests for the two known bugs in the code.
}
package dotsandboxes;
import org.junit.jupiter.api.*;
import jdk.jfr.Timestamp;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class DotsAndBoxesGridTest {
/*
* Because Test classes are classes, they can have fields, and can have static fields.
* This field is a logger. Loggers are like a more advanced println, for writing messages out to the console or a log file.
*/
private static final Logger logger = LogManager.getLogger(DotsAndBoxesGridTest.class);
/*
* Tests are functions that have an @Test annotation before them.
* The typical format of a test is that it contains some code that does something, and then one
* or more assertions to check that a condition holds.
*
* This is a dummy test just to show that the test suite itself runs
*/
@Test
public void testTestSuiteRuns() {
logger.info("Dummy test to show the test suite runs");
assertTrue(true);
}
// FIXME: You need to write tests for the two known bugs in the code.
@Test
public void testBoxCompleteCorrect() {
//Draw an arbitrary line that obviously does not complete a box - only one is required because it clearly does not complete a box.
/*Call the boxComplete method to try to validate whether a box has been drawn. It will always return true.
*We want to assert FALSE becuase we technically want the method to return false, but the method will ways return true resulting in a fail.
*We know that this is correct becuase we know for a fact that the box has NOT been completed, yet the method will return true and the test will fail
*because technically we want it to return FALSE.
*/
//Imagine that only a single arbitrary line exists on a dots and boxes grid of any size. Then, we know for sure that the box has not been drawn and
//should return false. Not dependent on a dots and boxes object existing as method is recursively called when "drawing" a line but is not yet complete.
int knownIncompleteX = 1;
int knownIncompleteY = 2;
boolean boxCompleteResult = boxComplete(knownIncompleteX, knownIncompleteY);
assertFalse(boxCompleteResult);
}
@Test
public void testNoDrawOverwriteHorz() {
//Create a dots and boxes grid - a 3x3 will do.
//Create an example player.
//Draw a horizontal line from 1, 2 to 2,2.
//Draw the above line again and assert that it throws an IllegalStateException.
DotsAndBoxesGrid test3by3 = DotsAndBoxesGrid(3, 3, 1);
boolean initialLine = drawHorizontal(1, 2, 1);
assertThrows(sameLine = drawHorizontal(1, 2, 1), IllegalStateException);
}
public void testNoDrawOverwriteVert() {
//Create a dots and boxes grid - a 3x3 will do.
//Create an example player.
//Draw a horizontal line from 1, 2 to 2,2.
//Draw the above line again and assert that it throws an IllegalStateException.
DotsAndBoxesGrid test3by3 = DotsAndBoxesGrid(3, 3, 1);
boolean initialLine = drawVertical(3, 1, 1);
assertThrows(sameLine = drawHorizontal(3, 1, 1), IllegalStateException);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment