2017-02-11 77 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; 
}