我正在尝试使用eclipse和gridworld检查游戏奥赛罗中的移动是否合法。我对位置做的第一件事是检查它是否有效,但为了检查有效位置,它必须不为空。问题是,它是合法移动的要求之一是它是空的/空的/未被占用的。我该如何避免这种情况?我已经指出了错误应该在哪里。(对不起,如果这让任何人感到困惑。)
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;
}