Skip to content
Snippets Groups Projects
Commit 5e1e2fb2 authored by Martin Schreiber's avatar Martin Schreiber
Browse files

fixed issues

parent a47e1f64
No related branches found
No related tags found
No related merge requests found
......@@ -110,10 +110,12 @@ public class DotsAndBoxesGrid {
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).
if (getHorizontal(x,y) && getHorizontal(x,y+1) && getVertical(x+1,y) && getVertical(x,y)){
return true;
} else {
return false;
}
}
/** Tries to claim a box for a player. If the box is complete, sets the ownership and returns true. */
......@@ -140,8 +142,10 @@ public class DotsAndBoxesGrid {
throw new IndexOutOfBoundsException(String.format("y was %d, which is out of range. Range is 0 to %d", y, height));
}
// FIXME: You need to throw an exception if the line was already drawn.
if(this.horizontals[x][y]){
throw new RuntimeException("Line already drawn.");
}else{
this.horizontals[x][y] = true;
// Try to claim the north or south boxes
......@@ -157,6 +161,9 @@ public class DotsAndBoxesGrid {
}
}
}
/**
* "Draws" a vertical line, from grid point (x, y) to (x, y + 1). (i.e. sets that line to true)
* @param x
......@@ -171,13 +178,16 @@ public class DotsAndBoxesGrid {
throw new IndexOutOfBoundsException(String.format("y was %d, which is out of range. Range is 0 to %d", y, height - 1));
}
// You need to throw an exception if the line was already drawn.
if(this.verticals[x][y]){
throw new RuntimeException("Line already drawn.");
}else{
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);
if (claimE || claimW) {
boolean claimN = claimBox(x, y-1, player);
boolean claimS = claimBox(x, y, player);
if (claimN || claimS) {
notifyObservers();
return true;
} else {
......@@ -185,6 +195,7 @@ public class DotsAndBoxesGrid {
notifyObservers();
return false;
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment