Commit 912741b6 authored by rbirch4's avatar rbirch4
Browse files

Fixed all bugs and tested that they are fixed. Updated unit tests slightly to...

Fixed all bugs and tested that they are fixed. Updated unit tests slightly to draw a true 3x3 grid instead of 2x2. Tests are still logically the same so still fail and pass where required to do so.
parent 5151d8e1
......@@ -86,6 +86,8 @@ public class DotsAndBoxesGrid {
/** Returns whether a horizontal line has been drawn */
public boolean getHorizontal(int x, int y) {
//boolean test = horizontals[x][y];
//System.out.println(test);
return horizontals[x][y];
}
......@@ -112,50 +114,95 @@ public class DotsAndBoxesGrid {
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;
boolean boxFlag = false;
switch (boundaryLine(x, y)) {
case 1:
//left vertical boundary
System.out.println("case 1 exe");
if (getHorizontal(x, y) && getHorizontal(x, (y+1)) && getVertical((x+1), y)) {
boxFlag = true;
}
else {
boxFlag = false;
}
break;
case 2:
System.out.println("case 2 exe");
//right vertical boundary
if (getHorizontal((x-1), y) && getHorizontal((x-1), (y+1)) && getVertical((x-1), y)) {
boxFlag = true;
}
else {
boxFlag = false;
}
break;
case 3:
System.out.println("case 3 exe");
//top of grid
if (getHorizontal(x, (y+1)) && getVertical(x, y) && getVertical((x+1), y)) {
boxFlag = true;
}
else {
boxFlag = false;
}
break;
case 4:
System.out.println("case 4 exe");
//bottom of grid
if (getHorizontal(x, (y-1)) && getVertical(x, y) && getVertical((x+1), y)) {
boxFlag = true;
}
else {
boxFlag = false;
}
break;
default:
//not a boundary
System.out.println("case def exe");
if (getHorizontal(x, y) && getHorizontal(x, (y+1)) && getVertical(x, y) && getVertical((x+1), y)) {
//south box
boxFlag = true;
}
else {
boxFlag = false;
}
break;
}
// 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.
System.out.printf("boxFlag is: %b%n", boxFlag);
return boxFlag;
}
public int boundaryLine(int x, int y) {
int boundaryFlag = 0;
if (x == 1) {
if (y > 1) {
boundaryFlag = 1;
}
System.out.println("called boundaryline");
if (x == 0) {
boundaryFlag = 1;
//left vertical boundary.
}
else if (x == width) {
if (y > 1) {
boundaryFlag = 2;
//check u, d, l
}
else if (x == (width-1)) {
boundaryFlag = 2;
//right vertical boundary
}
else if (y == 1) {
else if (y == 0) {
boundaryFlag = 3;
//bottom of grid
//top of grid
//u, l, r
}
else if (y == height) {
boundaryFlag = 4;
//top of grid
//bottom of grid
//d, l, r
}
else {
System.out.printf("else exe boundaryFlag is: %d%n", boundaryFlag);
return boundaryFlag;
}
System.out.printf("not else exe boundaryFlag is: %d%n", boundaryFlag);
return boundaryFlag;
}
/** Tries to claim a box for a player. If the box is complete, sets the ownership and returns true. */
......@@ -185,11 +232,14 @@ public class DotsAndBoxesGrid {
throw new IllegalStateException(String.format("Warning: This line has already been drawn by a player!"));
}
else {
System.out.printf("Intending to draw (x,y): %d %d\n", x, y);
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); //
System.out.println("called for north box");
boolean claimS = claimBox(x, y, player);
System.out.println("called for south box");
if (claimN || claimS) {
notifyObservers();
return true;
......@@ -223,10 +273,13 @@ public class DotsAndBoxesGrid {
throw new IllegalStateException(String.format("This line has already been drawn by another player!"));
}
else {
System.out.printf("Intending to draw (x,y): %d %d\n", x, y);
this.verticals[x][y] = true;
// Try to claim the north or south boxes
boolean claimE = claimBox(x, y, player);
System.out.println("called for east box");
boolean claimW = claimBox(x-1, y, player);
System.out.println("called for west box");
if (claimE || claimW) {
notifyObservers();
return true;
......
......@@ -44,7 +44,7 @@ public class DotsAndBoxesGridTest {
//should return false. Not dependent on a dots and boxes object existing as method is recursively called when "drawing" a line but is not yet complete.
int knownIncompleteX = 1;
int knownIncompleteY = 1;
DotsAndBoxesGrid test3by3 = new DotsAndBoxesGrid(3, 3, 1);
DotsAndBoxesGrid test3by3 = new DotsAndBoxesGrid(4, 4, 1);
boolean incompleteBox = test3by3.drawHorizontal(knownIncompleteX, knownIncompleteY, 1);
boolean boxCompleteResult = test3by3.boxComplete(knownIncompleteX, knownIncompleteY);
logger.info(boxCompleteResult);
......@@ -59,7 +59,7 @@ public class DotsAndBoxesGridTest {
//Draw the above line again and assert that it throws an IllegalStateException.
int horzX = 1;
int horzY = 2;
DotsAndBoxesGrid test3by3 = new DotsAndBoxesGrid(3, 3, 1);
DotsAndBoxesGrid test3by3 = new DotsAndBoxesGrid(4, 4, 1);
boolean initialLine = test3by3.drawHorizontal(horzX, horzY, 1);
assertThrows(IllegalStateException.class,() -> test3by3.drawHorizontal(horzX, horzY, 1));
......@@ -73,7 +73,7 @@ public class DotsAndBoxesGridTest {
//Draw the above line again and assert that it throws an IllegalStateException.
int vertX = 1;
int vertY = 1;
DotsAndBoxesGrid test3by3 = new DotsAndBoxesGrid(3, 3, 1);
DotsAndBoxesGrid test3by3 = new DotsAndBoxesGrid(4, 4, 1);
boolean initialLine = test3by3.drawVertical(vertX, vertY, 1);
assertThrows(IllegalStateException.class,() -> test3by3.drawVertical(vertX, vertY, 1));
......
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