Skip to content
Snippets Groups Projects
Commit f0a97991 authored by Jennica Llamera's avatar Jennica Llamera
Browse files

Fixed box completion and redrawn line errors

parent ddfb287b
No related branches found
No related tags found
No related merge requests found
......@@ -112,8 +112,12 @@ 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).
else if (getHorizontal(x, y) && getHorizontal(x, y + 1)
&& getVertical(x, y) && 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) {
......@@ -140,6 +144,9 @@ public class DotsAndBoxesGrid {
}
// FIXME: You need to throw an exception if the line was already drawn.
if (horizontals[x][y]) {
throw new IllegalStateException("Line has been already drawn!");
}
this.horizontals[x][y] = true;
......@@ -171,8 +178,11 @@ public class DotsAndBoxesGrid {
}
// You need to throw an exception if the line was already drawn.
if (verticals[x][y]) {
throw new IllegalStateException("Line has been 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);
......
......@@ -24,7 +24,8 @@ public class DotsAndBoxesGridTest {
*
* This is a dummy test just to show that the test suite itself runs
*/
public void setUp() {
@BeforeEach
public void setup() {
// Setting up a new DotsAndBoxesGrid for every test
gridTest = new DotsAndBoxesGrid(4, 4, 2);
}
......@@ -37,41 +38,24 @@ public class DotsAndBoxesGridTest {
// FIXME: You need to write tests for the two known bugs in the code.
@Test
public void testScoreCalculation() {
logger.info("Testing score calculation on box completion");
public void testBoxCompletion() {
logger.info("Testing box completion");
DotsAndBoxesGrid grid = new DotsAndBoxesGrid(4, 3, 2);
grid.drawHorizontal(0, 0, 1);
grid.drawVertical(0, 0, 1);
grid.drawVertical(1, 0, 1);
boolean boxCompleted = grid.drawHorizontal(1, 0, 1); // This should complete the box
gridTest.drawHorizontal(0, 0, 1);
gridTest.drawVertical(0, 0, 1);
gridTest.drawHorizontal(0, 1, 1);
gridTest.drawVertical(1, 0, 1);
// Check if the box was completed and claimed by the correct player
assertTrue(boxCompleted, "Box should be completed and claimed");
assertEquals(1, grid.getBoxOwner(0, 0), "The box owner should be player 1");
}
@Test
public void testInvalidMoveHandling() {
logger.info("Testing invalid move handling");
assertTrue(gridTest.boxComplete(0,0), "Box should be completed");
DotsAndBoxesGrid grid = new DotsAndBoxesGrid(4, 3, 2);
grid.drawHorizontal(0, 0, 1);
// Attempt to draw the same horizontal line again
Exception exception = assertThrows(IndexOutOfBoundsException.class, () -> {
grid.drawHorizontal(0, 0, 1); // This should throw an exception
});
assertEquals("Line has already been drawn", exception.getMessage());
}
@Test
public void testBoxCompletionLogic() {
logger.info("Testing box completion logic");
public void testVerticalLineRedrawn() {
logger.info("Testing if throws exception if redrawn a line ");
gridTest.drawVertical(0, 0, 1);
assertThrows(IllegalStateException.class, () -> gridTest.drawVertical(0, 0, 1));
DotsAndBoxesGrid grid = new DotsAndBoxesGrid(4, 3, 2);
// Attempt to complete a box without drawing all lines
boolean isComplete = grid.boxComplete(0, 0);
assertFalse(isComplete, "Box should not be complete without all lines drawn");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment