2017-02-11 34 views
-2

我的國際象棋遊戲有一部分工作不正常。爲了使解釋簡單,我不會給出除了重要之外的所有細節。我有一個棋盤存儲爲棋子的鏈接列表。該程序應該讀取像「2 2 4 4」這樣的整數行。重新鏈接鏈表?國際象棋遊戲

這個例子線將所述片從點2,2移到點4,4。

如果一塊是在4,4和不同的顏色比所述運動件,該件在4,4將得到刪除,並在2,2的片段的座標將被改爲4,4.

我想我沒有正確重新鏈接列表後,刪除鏈接持有需要刪除的片斷。下面的代碼很重要。

for (int a = 0, b = 1, c = 2, d = 3; a < token2.length && b < token2.length && c < token2.length 
       && d < token2.length; a += 4, b += 4, c += 4, d += 4) { 
      //reads integers to determine which piece to move and where. 

      int MoveFromCol = Integer.parseInt(token2[a]); 
      int MoveFromRow = Integer.parseInt(token2[b]); 
      int MoveToCol = Integer.parseInt(token2[c]); 
      int MoveToRow = Integer.parseInt(token2[d]); 

      //finds the chesspiece object that is moving. 
      chessPiece MovingPiece = theBoard.findMovingPiece(MoveFromCol, MoveFromRow); 
      //stores the piece type (rook, queen, etc). 
      String SpacePieceType = theBoard.CheckPieceAtSpot(MoveToCol, MoveToRow); 

      //if there isn't another piece at the spot we're moving to, just change the moving piece's coordinates 
      //to the new spot if the piece could move. 
      if (SpacePieceType.equals("no piece") && MovingPiece.canMove(theBoard, MoveToCol, MoveToRow, SpacePieceType)) { 
       theBoard.updateLink(MoveFromCol, MoveFromRow, MoveToCol, MoveToRow); 
      } 
      //If there was a piece at spot we're moving to, different color, and we can move there, delete that piece 
      //and change moving pieces coordinates to new spot. 
      if (MovingPiece.canMove(theBoard, MoveToCol, MoveToRow, SpacePieceType) && !SpacePieceType.equals("no space")) { 

       theBoard.delete(MoveToCol, MoveToRow); 
       theBoard.updateLink(MoveFromCol, MoveFromRow, MoveToCol, MoveToRow); 

      } 
     } 

在包含類列表方法

public Link delete(int Col, int Row) { 

    Link current = head; 
    Link previous = head; 

    while (current.piece.col != Col && current.piece.row != Row) { 

     if (current.next == null) { 
      return null; 
     } else { 

      previous = current; 
      current = current.next; 
     } 
    } 

    if (current == head) { 

     head = head.next; 
    } else { 

     previous.next = current.next; 
    } 

    return current; 
} 


    public void updateLink(int oldCol, int oldRow, int newCol, int newRow) { 

    Link current = head; 

    while (current != null) { 

     if (oldCol == current.piece.col && oldRow == current.piece.row) { 

      current.piece.col = newCol; 
      current.piece.row = newRow; 
     } 
     current = current.next; 
    } 
} 
+0

與遊戲分開測試鏈接列表。確認您可以在不同情況下添加,刪除元素。例如,添加3個元素,刪除3個元素,並確認您仍然可以添加更多元素。每次修改後打印列表。 –

+0

此外,假設您希望能夠刪除鏈接列表中特定位置的節點,請確保您可以刪除列表的開頭,第二個項目,最後一個項目和倒數第二個項目中的節點。 –

+0

此外,將鏈表列表與遊戲邏輯完全分開是個好主意。鏈接列表應該與抽象數據類型的接口一起實現,這些抽象數據類型具有定義明確的操作,例如add,remove和toString方法來打印其內容。 –

回答

0

你可能有其他錯誤,但你刪除程序是絕對不正確。修復while循環中的邏輯條件可修復此錯誤。順便說一下,您不需要修正updateLink方法中的相似外觀條件,因爲它不是否定聯合,因此不受De Morgan's Laws的約束。

public Link delete(int Col, int Row) { 

    Link current = head; 
    Link previous = head; 

    while (current.piece.col != Col || current.piece.row != Row) { 

     if (current.next == null) { 
      return null; 
     } else { 

      previous = current; 
      current = current.next; 
     } 
    } 

    if (current == head) { 

     head = head.next; 
    } else { 

     previous.next = current.next; 
    } 

    return current; 
}