Skip to content
Snippets Groups Projects
Commit 62480a1d authored by mthoma48's avatar mthoma48
Browse files

Initial bugfixes that pass unit tests

parent 3cff0b8d
No related branches found
No related tags found
No related merge requests found
......@@ -107,13 +107,20 @@ public class DotsAndBoxesGrid {
* @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;
if (x >= width - 1 || x < 0 || y >= height - 1 || y < 0) {
return false;
}
else if (this.getVertical(x,y) == true && this.getVertical(x + 1, y) == true) {
if (this.getHorizontal(x,y) == true && this.getHorizontal(x, y + 1) == true) {
return true;
}
}
else {
return false;
}
// A box is complete if the north and south horizontals and the east and west verticals have all been drawn
return false;
}
/** Tries to claim a box for a player. If the box is complete, sets the ownership and returns true. */
......@@ -142,7 +149,12 @@ public class DotsAndBoxesGrid {
// FIXME: You need to throw an exception if the line was already drawn.
this.horizontals[x][y] = true;
if (this.horizontals[x][y] == true) {
throw new RuntimeException("Line already here");
}
else {
this.horizontals[x][y] = true;
}
// Try to claim the north or south boxes
boolean claimN = claimBox(x, y-1, player);
......
......@@ -35,12 +35,12 @@ public class DotsAndBoxesGridTest {
public void completeBoxTest() {
logger.info("Test to see if completion algorithm is working");
DotsAndBoxesGrid grid = new DotsAndBoxesGrid(4,2,2);
DotsAndBoxesGrid grid = new DotsAndBoxesGrid(4,3,2);
// draw a complete square
grid.drawHorizontal(1,1,1);
grid.drawHorizontal(1,2,1);
grid.drawVertical(1,2,1);
assertTrue(grid.drawVertical(2,2,1));
grid.drawHorizontal(1,2,2);
grid.drawVertical(1,1,1);
assertTrue(grid.drawVertical(2,1,1));
}
// Test that DotsAndBoxesGrid currently allows drawing a line on an existing line
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment