Skip to content
Snippets Groups Projects
Commit 6abdcd8c authored by Asmita Adhikari's avatar Asmita Adhikari
Browse files

Merge branch '1-fix-assignment-errors' into main

parent 59a20041
No related branches found
No related tags found
No related merge requests found
......@@ -4,32 +4,6 @@ import java.util.ArrayList;
import java.util.Arrays;
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;
......@@ -84,21 +58,14 @@ 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.
*/
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;
return getHorizontal(x, y) &&
getHorizontal(x, y + 1) &&
getVertical(x, y) &&
getVertical(x + 1, y);
}
private boolean claimBox(int x, int y, int p) {
......@@ -117,11 +84,12 @@ public class DotsAndBoxesGrid {
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("Line 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) {
......@@ -142,10 +110,11 @@ public class DotsAndBoxesGrid {
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("Line 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) {
......@@ -159,8 +128,6 @@ 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"
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));
}
}
......@@ -14,5 +14,42 @@ public class DotsAndBoxesGridTest {
assertTrue(true);
}
// FIXME: You need to write tests for the two known bugs in the code.
@Test
public void testSquareCompletion() {
DotsAndBoxesGrid grid = new DotsAndBoxesGrid(2, 2, 2); // Create a grid with appropriate dimensions
// Draw lines to complete a square
grid.drawHorizontal(0, 0, 1); // Top
grid.drawVertical(0, 0, 1); // Left
grid.drawHorizontal(0, 1, 1); // Bottom
grid.drawVertical(1, 0, 1); // Right
// Check if the square is complete
boolean isComplete = grid.boxComplete(0, 0);
// Log the result
logger.info("Square completion test: " + (isComplete ? "Passed" : "Failed"));
// Assert that the square is correctly identified as complete
assertTrue(isComplete, "Square should be complete.");
}
@Test
public void testDuplicateLineDrawing() {
DotsAndBoxesGrid grid = new DotsAndBoxesGrid(2, 2, 2); // Create a grid with appropriate dimensions
// Draw a line
grid.drawHorizontal(0, 0, 1);
// Try to draw the same line again and check for an exception
Exception exception = assertThrows(IllegalStateException.class, () -> {
grid.drawHorizontal(0, 0, 1);
});
// Log the exception
logger.info("Duplicate line drawing test: " + exception.getMessage());
// Assert that the exception message is correct
assertEquals("Line already drawn!", exception.getMessage());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment