Skip to content
Snippets Groups Projects
Commit d22a24aa authored by hkaur28's avatar hkaur28
Browse files

Bug fixed

parent 2dcf32c1
No related branches found
No related tags found
No related merge requests found
......@@ -111,8 +111,12 @@ public class DotsAndBoxesGrid {
}
// A box is complete if the north and south horizontals and the east and west verticals have all been drawn.
boolean north = horizontals[x][y];
boolean south = horizontals[x][y + 1];
boolean east = verticals[x + 1][y];
boolean west = verticals[x][y];
// FIXME: You'll need to fix this code (after writing a test first).
return false;
return north && south && east && west;
}
/** Tries to claim a box for a player. If the box is complete, sets the ownership and returns true. */
......@@ -140,7 +144,9 @@ public class DotsAndBoxesGrid {
}
// FIXME: You need to throw an exception if the line was already drawn.
if (horizontals[x][y]) {
throw new IllegalArgumentException("Already Drawn");
}
this.horizontals[x][y] = true;
// Try to claim the north or south boxes
......@@ -171,6 +177,9 @@ public class DotsAndBoxesGrid {
}
// You need to throw an exception if the line was already drawn.
if (verticals[x][y]) {
throw new IllegalArgumentException("Already Drawn");
}
this.verticals[x][y] = true;
// Try to claim the north or south boxes
......
......@@ -14,25 +14,29 @@ public class DotsAndBoxesGridTest {
@BeforeEach
public void setUp() {
// Create a new DotsAndBoxesGrid instance before each test
grid = new DotsAndBoxesGrid(4, 3, 2);
grid = new DotsAndBoxesGrid(15, 8, 2);
}
@Test
public void testBoxComplete() {
// Test if the boxComplete method works correctly
grid.drawHorizontal(3,3,1);
grid.drawHorizontal(3,4,1);
grid.drawVertical(3,3,1);
grid.drawVertical(4,3,1);
assertTrue(grid.boxComplete(3,3));
}
@Test
public void whenExceptionThrown_thenAssertionSucceeds() {
Exception exception = assertThrows(Exception.class, () -> {
grid.drawHorizontal(3, 3, 1);
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
grid.drawHorizontal(3,3,1);
});
String expectedMessage = "Already Drawn";
String actualMessage = exception.getMessage();
System.out.println(actualMessage);
assertTrue(actualMessage.contains(expectedMessage));
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment