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

Bug fixed

parent 2dcf32c1
No related merge requests found
...@@ -111,8 +111,12 @@ public class DotsAndBoxesGrid { ...@@ -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. // 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). // 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. */ /** 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 { ...@@ -140,7 +144,9 @@ public class DotsAndBoxesGrid {
} }
// FIXME: You need to throw an exception if the line was already drawn. // 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; this.horizontals[x][y] = true;
// Try to claim the north or south boxes // Try to claim the north or south boxes
...@@ -171,6 +177,9 @@ public class DotsAndBoxesGrid { ...@@ -171,6 +177,9 @@ public class DotsAndBoxesGrid {
} }
// You need to throw an exception if the line was already drawn. // 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; this.verticals[x][y] = true;
// Try to claim the north or south boxes // Try to claim the north or south boxes
......
...@@ -14,25 +14,29 @@ public class DotsAndBoxesGridTest { ...@@ -14,25 +14,29 @@ public class DotsAndBoxesGridTest {
@BeforeEach @BeforeEach
public void setUp() { public void setUp() {
// Create a new DotsAndBoxesGrid instance before each test // Create a new DotsAndBoxesGrid instance before each test
grid = new DotsAndBoxesGrid(4, 3, 2); grid = new DotsAndBoxesGrid(15, 8, 2);
} }
@Test @Test
public void testBoxComplete() { public void testBoxComplete() {
// Test if the boxComplete method works correctly // 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)); assertTrue(grid.boxComplete(3,3));
} }
@Test @Test
public void whenExceptionThrown_thenAssertionSucceeds() { public void whenExceptionThrown_thenAssertionSucceeds() {
Exception exception = assertThrows(Exception.class, () -> { grid.drawHorizontal(3, 3, 1);
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
grid.drawHorizontal(3,3,1); grid.drawHorizontal(3,3,1);
}); });
String expectedMessage = "Already Drawn"; String expectedMessage = "Already Drawn";
String actualMessage = exception.getMessage(); String actualMessage = exception.getMessage();
System.out.println(actualMessage);
assertTrue(actualMessage.contains(expectedMessage)); 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