Commit 5e1e2fb2 authored by Martin Schreiber's avatar Martin Schreiber
Browse files

fixed issues

parent a47e1f64
......@@ -110,10 +110,12 @@ public class DotsAndBoxesGrid {
if (x >= width - 1 || x < 0 || y >= height - 1 || y < 0) {
return false;
}
if (getHorizontal(x,y) && getHorizontal(x,y+1) && getVertical(x+1,y) && getVertical(x,y)){
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).
return true;
}
/** Tries to claim a box for a player. If the box is complete, sets the ownership and returns true. */
......@@ -140,21 +142,26 @@ 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
boolean claimN = claimBox(x, y-1, player);
boolean claimS = claimBox(x, y, player);
if (claimN || claimS) {
notifyObservers();
return true;
} else {
nextPlayer();
notifyObservers();
return false;
}
}
this.horizontals[x][y] = true;
// Try to claim the north or south boxes
boolean claimN = claimBox(x, y-1, player);
boolean claimS = claimBox(x, y, player);
if (claimN || claimS) {
notifyObservers();
return true;
} else {
nextPlayer();
notifyObservers();
return false;
}
}
/**
......@@ -171,19 +178,23 @@ 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.
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) {
notifyObservers();
return true;
} else {
nextPlayer();
notifyObservers();
return false;
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 claimN = claimBox(x, y-1, player);
boolean claimS = claimBox(x, y, player);
if (claimN || claimS) {
notifyObservers();
return true;
} else {
nextPlayer();
notifyObservers();
return false;
}
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment