Skip to content
Snippets Groups Projects
Commit a13cd1bc authored by gsjollem's avatar gsjollem
Browse files

tests now work and bugs corrected

parent cfe39cdc
No related branches found
No related tags found
No related merge requests found
...@@ -19,7 +19,7 @@ import java.util.function.Consumer; ...@@ -19,7 +19,7 @@ import java.util.function.Consumer;
* *
* Notice that: * Notice that:
* *
* - for each row, there is one less horizontal than the number of corner dots * - for each row, there is one less horizontal than the numbgraer of corner dots
* - for each row, there are as many verticals as there are 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 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 is one less vertical than the number of corner dots.
...@@ -111,10 +111,19 @@ public class DotsAndBoxesGrid { ...@@ -111,10 +111,19 @@ public class DotsAndBoxesGrid {
return false; return false;
} }
// A box is complete if the north and south horizontals and the east and west verticals have all been drawn. else if((getHorizontal(x, y)) && (getHorizontal(x, y+1)) && (getVertical(x, y))
// FIXME: You'll need to fix this code (after writing a test first). && (getVertical(x + 1, y)))
{
return 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.
// FIXME: You'll need to fix this code (after writing a test first)
}
/** 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. */
private boolean claimBox(int x, int y, int p) { private boolean claimBox(int x, int y, int p) {
...@@ -142,6 +151,11 @@ public class DotsAndBoxesGrid { ...@@ -142,6 +151,11 @@ 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(getHorizontal(x, y))
{
throw new IndexOutOfBoundsException("That connection has already been 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
...@@ -173,6 +187,12 @@ public class DotsAndBoxesGrid { ...@@ -173,6 +187,12 @@ 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(getVertical(x, y))
{
throw new IndexOutOfBoundsException("That connection has already been 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
boolean claimE = claimBox(x, y, player); boolean claimE = claimBox(x, y, player);
......
...@@ -45,7 +45,8 @@ public class DotsAndBoxesGridTest { ...@@ -45,7 +45,8 @@ public class DotsAndBoxesGridTest {
grid.drawVertical(0,0,1); grid.drawVertical(0,0,1);
grid.drawVertical(1,0,1); grid.drawVertical(1,0,1);
assertFalse(grid.drawHorizontal(0, 0, 1)); assertThrows(IndexOutOfBoundsException.class, () -> { grid.drawHorizontal(0, 0, 1);} );
//assertFalse(grid.drawHorizontal(0, 0, 1));
} }
...@@ -54,6 +55,12 @@ public class DotsAndBoxesGridTest { ...@@ -54,6 +55,12 @@ public class DotsAndBoxesGridTest {
public void testBoxComplete() public void testBoxComplete()
{ {
// will need to draw a box, then give it to box complete and assert that it should be true // will need to draw a box, then give it to box complete and assert that it should be true
grid.drawHorizontal(0, 0, 1);
grid.drawHorizontal(0,1, 1);
grid.drawVertical(0,0,1);
grid.drawVertical(1,0,1);
logger.info("Testing box completion"); logger.info("Testing box completion");
assertTrue(grid.boxComplete(0,0)); assertTrue(grid.boxComplete(0,0));
assertFalse(grid.boxComplete(2,2)); assertFalse(grid.boxComplete(2,2));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment