I'm trying to check whether or not a move is legal in the game Othello, using eclipse and gridworld. The first thing I do to the location is check if it is valid, but in order to check the validity location, it needs to not be null. The problem is, one of the requirements of it being a legal move is that it is null/empty/unoccupied. How do I avoid this? I have pointed out where the error is supposedly at. (Sorry if this confused anyone.)
public boolean isLegal(Location loc1)
{
boolean isLegal = false;
String currentColor = currentPlayer.getColor();
int row = loc1.getRow();
int col = loc1.getCol();
if(board.isValid(loc1))
{
if(board.get(loc1) == null)
{
for(Location tempLoc : board.getValidAdjacentLocations(loc1))
{
**if(!board.get(tempLoc).equals(currentColor))**
{
if((row != tempLoc.getRow()) && (col == tempLoc.getCol()))
{
//count up column
if(tempLoc.getRow() < row)
{
for(int i = row; i > 1;)
{
Location tempLoc2 = new Location(i-2, col);
if(!board.get(tempLoc2).equals(currentColor))
{
i--;
}
else
{
i=-1;
isLegal = true;
}
}
}
//count down column
else
{
for(int i = row; i < 6;)
{
Location tempLoc2 = new Location(i+2, col);
if(!board.get(tempLoc2).equals(currentColor))
{
i++;
}
else
{
i=9;
isLegal = true;
}
}
}
}
else if(col != tempLoc.getCol() && row == tempLoc.getRow())
{
//count right row
if(col > tempLoc.getCol())
{
for(int i = col; i > 1;)
{
Location tempLoc2 = new Location(row, i-2);
if(!board.get(tempLoc2).equals(currentColor))
{
i--;
}
else
{
i=-1;
isLegal = true;
}
}
}
//count left row
else
{
for(int i = col; i < 6;)
{
Location tempLoc2 = new Location(row, i+2);
if(!board.get(tempLoc2).equals(currentColor))
{
i++;
}
else
{
i=9;
isLegal = true;
}
}
}
}
else
{ //count up/right diag
if(row-1 == tempLoc.getRow() && col+1 == tempLoc.getCol())
{
int j = col;
for(int i = row; i > 1;)
{
Location tempLoc2 = new Location(i-1, j+1);
if(!board.get(tempLoc2).equals(currentColor))
{
i--;
j++;
}
else
{
i=-1;
isLegal = true;
}
}
}
//count down/left diag
else if(row+1 == tempLoc.getRow() && col-1 == tempLoc.getCol())
{
int i = row;
for(int j = col; j > 1;)
{
Location tempLoc2 = new Location(i+1, j-1);
if(!board.get(tempLoc2).equals(currentColor))
{
i++;
j--;
}
else
{
i=9;
isLegal = true;
}
}
}
//count up/left diag
else if(row-1 == tempLoc.getRow() && col-1 == tempLoc.getCol())
{
int j = col;
for(int i = row; i > 1;)
{
Location tempLoc2 = new Location(i-1, j-1);
if(!board.get(tempLoc2).equals(currentColor))
{
i--;
j--;
}
else
{
i=-1;
isLegal = true;
}
}
}
//count down/right diag
else
{
int j = col;
for(int i = row; i > 6;)
{
Location tempLoc2 = new Location(i+1, j+1);
if(!board.get(tempLoc2).equals(currentColor))
{
i++;
j++;
}
else
{
i=-1;
isLegal = true;
}
}
}
}
}
}
}
}
return isLegal;
}
One solution is to change your design so that no location is ever
null
.You seem to have equated
null
with "unoccupied" or "empty". Instead create all positions first (there aren't many of them on an Othello board) and initialize them all withboolean occupied = false
or an equivalent member variable. Then you'd have:instead of a null check.
This is better object oriented design because an empty location is still a location, and should be manipulable.