2012-01-15 78 views
0

我試圖在遊戲「黑白棋」中使用eclipse和gridworld來檢查棋子是否合法。我對該位置做的第一件事是檢查它是否有效,但爲了檢查有效位置,它不需要爲空。問題是,它的一個合法要求是空/空/空閒。我如何避免這種情況?我已經指出錯誤在哪裏。 (很抱歉,如果這個人感到困惑。)如何圍繞NullPointerException跳舞?

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; 
} 
+3

你可以用'if(ref == null)'來測試引用是否爲空... – 2012-01-15 23:00:19

+0

我已經完成了。問題是我需要使用這個空位置,但是我不能,如果它是空的。這是我的問題。我試圖找出將完成相同的事情,但沒有NullPointerException的替代代碼。 – frozenxdreamer 2012-01-15 23:03:01

+0

一旦您確定引用爲空,則不要試圖通過它調用任何成員方法。 – 2012-01-15 23:04:10

回答

3

一種解決辦法是改變設計,使沒有位置是有史以來null

您似乎已將null等同爲「未佔用」或「空白」。相反,首先創建所有職位(其中奧賽羅董事會中的職位並不多),並用boolean occupied = false或同等成員變量初始化所有職位。那麼你會得到:

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

而不是空檢查。

這是更好的面向對象的設計,因爲空的位置仍然是一個位置,應該是可操作的。

+0

謝謝!我現在要休息一下,但我很高興嘗試這個! – frozenxdreamer 2012-01-15 23:23:48

+0

我甚至會更進一步。我會在Board類中留下一組位置,但我也會創建一個Coordinates類。然後一個Location將有座標,你可以使用board.get(座標),它將返回一個Location,它會讓paislee提到的方法是佔用()(可能還有其他方法)。傳遞給一個方法獲取位置的位置對我來說似乎很奇怪。如果我是你,我也會重構這段代碼。 – 2012-01-15 23:34:21

+0

這似乎是一個好主意。我一定會嘗試。感謝您的洞察力! – frozenxdreamer 2012-01-15 23:59:55

1

您不應該使用null作爲您的邏輯的一部分。
null它不是一個狀態,它是一個符號,表示沒有狀態
你應該離開你的邏輯null,那麼如果一些參考是null,你知道確實有一些真正令人討厭的事情發生,與您的模型無關。在Location內部,您可以創建例如isEmpty()或類似的方法,因此您可以輕鬆避免與null進行比較。

0

使用與價值BLACKWHITEVACANT,而不是Stringenum,存儲是在每個位置上什麼顏色的標記。在Player類中返回相同的enumgetColor()