Commit 5151d8e1 authored by rbirch4's avatar rbirch4
Browse files

Fixed both bugs in code - inital test. Pending futher in-depth tests. Unit tests pass now.

parent 0ab1d2db
......@@ -107,17 +107,57 @@ public class DotsAndBoxesGrid {
* @return true if all four sides have been drawn.
*/
public boolean boxComplete(int x, int y) {
//Tests for invalid box references.
//Tests for invalid box references.\
//not boundary
if (x >= width - 1 || x < 0 || y >= height - 1 || y < 0) {
return false;
}
else if (getHorizontal(x, y) && getHorizontal(x, (y-1)) && getVertical(x, y) && getVertical((x+1), y)) {
//south box
return true;
}
else if (getHorizontal(x, y) && getHorizontal(x, (y+1)) && getVertical(x, y) && getVertical((x+1), y)) {
//north box
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 if box complete, false if not.
return true;
}
public int boundaryLine(int x, int y) {
int boundaryFlag = 0;
if (x == 1) {
if (y > 1) {
boundaryFlag = 1;
}
//left vertical boundary.
}
else if (x == width) {
if (y > 1) {
boundaryFlag = 2;
//check u, d, l
}
//right vertical boundary
}
else if (y == 1) {
boundaryFlag = 3;
//bottom of grid
//u, l, r
}
else if (y == height) {
boundaryFlag = 4;
//top of grid
//d, l, r
}
else {
return boundaryFlag;
}
return boundaryFlag;
}
/** 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) {
if (boxComplete(x, y)) {
......@@ -141,23 +181,29 @@ public class DotsAndBoxesGrid {
if (y >= height || y < 0) {
throw new IndexOutOfBoundsException(String.format("y was %d, which is out of range. Range is 0 to %d", y, height));
}
if (this.horizontals[x][y] == true) {
throw new IllegalStateException(String.format("Warning: This line has already been drawn by a player!"));
}
else {
this.horizontals[x][y] = true;
//Uses the coords from what was just drawn above to see if the drawn line will complete either a top or bottom box. Checking done through boxComplete called in claimBox.
boolean claimN = claimBox(x, y-1, player); //
boolean claimS = claimBox(x, y, player);
if (claimN || claimS) {
notifyObservers();
return true;
} else {
nextPlayer();
notifyObservers();
return false;
}
}
// FIXME: You need to throw an exception if the line was already drawn.
this.horizontals[x][y] = true;
//Uses the coords from what was just drawn above to see if the drawn line will complete either a top or bottom box. Checking done through boxComplete called in claimBox.
boolean claimN = claimBox(x, y-1, player); //
boolean claimS = claimBox(x, y, player);
if (claimN || claimS) {
notifyObservers();
return true;
} else {
nextPlayer();
notifyObservers();
return false;
}
}
/**
......@@ -173,21 +219,27 @@ public class DotsAndBoxesGrid {
if (y >= height - 1 || y < 0) {
throw new IndexOutOfBoundsException(String.format("y was %d, which is out of range. Range is 0 to %d", y, height - 1));
}
if (this.verticals[x][y] == true) {
throw new IllegalStateException(String.format("This line has already been drawn by another player!"));
}
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) {
notifyObservers();
return true;
} else {
nextPlayer();
notifyObservers();
return false;
}
}
// 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;
}
}
......
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