重新链接链表?国际象棋游戏

时间:2017-02-11 03:45:52

标签: java

我的国际象棋游戏中有一部分无法正常工作。为了使解释简单,我不会提供除了重要的细节之外的所有细节。我将棋盘存储为棋子的链表。该程序应该读取一行整数,如“2 2 4 4”。

此示例行会将作品从点2,2移动到点4,4。

如果一件是4号,4号和不同于移动件的颜色,那么4号,4件将被删除,而件2的坐标将改为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;
    }
}

1 个答案:

答案 0 :(得分: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;
}