0

我正在尝试使用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;
}
4

3 回答 3

3

一种解决方案是更改您的设计,以便永远没有位置null

你似乎等同于null“无人”或“空”。而是首先创建所有位置(在黑白棋板上没有很多位置)并使用boolean occupied = false或等效的成员变量将它们全部初始化。然后你会有:

if ( !board.get(loc1).isOccupied() ) { /*stuff*/ }

而不是空检查。

这是更好的面向对象设计,因为空位置仍然是位置,并且应该是可操作的。

于 2012-01-15T23:20:56.530 回答
1

您不应该将null其用作逻辑的一部分。
null它不是一个状态,它是没有状态的象征
你应该离开null你的逻辑,然后如果一些参考是null,你知道肯定会发生一些非常讨厌的事情,与你的模型无关。在内部Location,您可以创建例如方法isEmpty()或类似方法,因此您可以轻松避免与null.

于 2012-01-15T23:29:47.873 回答
0

使用enum带有值BLACKWHITEVACANT代替 aString来存储每个位置的颜色标记。enum从课堂上返回相同getColor()的内容。Player

于 2012-01-16T00:48:08.037 回答