// 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.
returntrue;
}
publicintboundaryLine(intx,inty){
intboundaryFlag=0;
if(x==1){
if(y>1){
boundaryFlag=1;
}
//left vertical boundary.
}
elseif(x==width){
if(y>1){
boundaryFlag=2;
//check u, d, l
}
//right vertical boundary
}
elseif(y==1){
boundaryFlag=3;
//bottom of grid
//u, l, r
}
elseif(y==height){
boundaryFlag=4;
//top of grid
//d, l, r
}
else{
returnboundaryFlag;
}
returnboundaryFlag;
}
/** Tries to claim a box for a player. If the box is complete, sets the ownership and returns true. */
privatebooleanclaimBox(intx,inty,intp){
if(boxComplete(x,y)){
...
...
@@ -141,23 +181,29 @@ public class DotsAndBoxesGrid {
if(y>=height||y<0){
thrownewIndexOutOfBoundsException(String.format("y was %d, which is out of range. Range is 0 to %d",y,height));
}
if(this.horizontals[x][y]==true){
thrownewIllegalStateException(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.
booleanclaimN=claimBox(x,y-1,player);//
booleanclaimS=claimBox(x,y,player);
if(claimN||claimS){
notifyObservers();
returntrue;
}else{
nextPlayer();
notifyObservers();
returnfalse;
}
}
// 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.
booleanclaimN=claimBox(x,y-1,player);//
booleanclaimS=claimBox(x,y,player);
if(claimN||claimS){
notifyObservers();
returntrue;
}else{
nextPlayer();
notifyObservers();
returnfalse;
}
}
/**
...
...
@@ -173,21 +219,27 @@ public class DotsAndBoxesGrid {
if(y>=height-1||y<0){
thrownewIndexOutOfBoundsException(String.format("y was %d, which is out of range. Range is 0 to %d",y,height-1));
}
if(this.verticals[x][y]==true){
thrownewIllegalStateException(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
booleanclaimE=claimBox(x,y,player);
booleanclaimW=claimBox(x-1,y,player);
if(claimE||claimW){
notifyObservers();
returntrue;
}else{
nextPlayer();
notifyObservers();
returnfalse;
}
}
// You need to throw an exception if the line was already drawn.