I am creating a reversi game for my intro CS class.
I found an error in SearchN() that would cause it to give false playableN flags and created isSame() as a work around.
Now the game is crashing when I attempt a move.
I have the bug isolated to isPlayable() which causes the program to stop running without error message.
I thought it was because the program was searching out of bounds; however, when i run .isPlayable(0,0) it returns a NullPointerException (also something I don’t quite know how to get rid of).
So it must be some error with handling un-played spaces.
Anyone have any thoughts?
/**
* a method to return the color of a tile
*@params x y tile coordinates
*/
public Color getColor(int x, int y){
try{
return buttons[x][y].getBackground();
}
catch(ArrayIndexOutOfBoundsException e){
return null;
}
}
/**
* a method to determine whether a tile has been played
*@params x y tile coordinates
*/
public boolean isPlayed(int x, int y){
if(this.isBlack(x,y) || this.isWhite(x,y)){
return true;
}else{
return false;
}
}
/**
* a method to determine whether a tile has a color opposite to a given color
*@params x y c tile coordinates and color to compare
*/
public boolean isOpposite(int x, int y, Color c){
if(this.isPlayed(x,y)){
return(!(this.getColor(x,y).equals(c)));
} else
return false; // this was giving the false playableN flag
}
/**
* a method to determine weather a tile has the same color as the one given
*@params x y c tile coordinates and color to compare
*/
public boolean isSame(int x, int y, Color c){
if(this.isPlayed(x,y)){
return(this.getColor(x,y).equals(c));
}else{
return false;
}
}
/**
* a method used to check tiles north of an attempted play to verify legal moves
*@params x y c tile coordinates and comparing color
*/
public void searchN(int x, int y, int increment, Color c){
if(increment>1 && (this.isSame(x-increment,y, c))){
playableN = true;
leadN = false;
} else {
if(this.isOpposite(x-increment,y,c)){
leadN=true;
}
}
}
/**
* a method used to determine if a tile is playable
*@params x y tile coordinates
*/
public boolean isPlayable(int x, int y){
this.searchN(x,y,1,turnColor);
// search 7 other directions
while(leadN||leadNE||leadE||leadSE||leadS||leadSW||leadW||leadNW){
int i = 2;
if(leadN)
this.searchN(x,y,i,turnColor);
// search 7 other directions
i++;
}
if(playableN||playableNE||playableE||playableSE||playableS||playableSW||playableW||playableNW)
return true;
else
return false;
}
** all tiles are black, white or the default tile color (green) and in a 2D array of JButtons displayed in gridLayout().
I see two ways that a
NullPointerExceptioncould be happening:You are getting an
ArrayIndexOutOfBoundsExceptioningetColor. In that case you are catching the exception and returningnull.or
getBackgroundis returningnull.In either case
getColorwill returnnull, which causes aNullPointerExceptionto be thrown when you call.equalsinisOppositeorisSame.You should check the result of getColor before attempting to calls
.equalson it. Then you should figure out whygetBackgroundis returningnullorArrayIndexOutOfBoundsExceptionis being thrown.