Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 重新链接链表?棋局_Java - Fatal编程技术网

Java 重新链接链表?棋局

Java 重新链接链表?棋局,java,Java,我的象棋有一部分不对劲。为了让解释更简单,除了那些重要的细节外,我不会给出所有的细节。我有一个棋盘,作为棋子的链表存储。程序应该读取一行像“2 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.le

我的象棋有一部分不对劲。为了让解释更简单,除了那些重要的细节外,我不会给出所有的细节。我有一个棋盘,作为棋子的链表存储。程序应该读取一行像“2 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);

            }
        }

您可能有其他错误,但您的删除例程肯定不正确。修复while循环中的逻辑条件可以修复此错误。顺便说一句,您不需要在
updateLink
方法中修复类似的外观条件,因为它不是否定连词,因此不受限制


在游戏中单独测试链接列表。验证是否可以在不同情况下添加、删除元素。例如,添加3个元素,删除3个元素,并验证您仍然可以添加更多元素。每次修改后打印列表。此外,假设您希望能够删除链接列表中特定位置的节点,请确保可以删除列表开头的节点、第二项、最后一项和最后一项的第二项。此外,将链表代码与游戏逻辑完全分离是个好主意。链表应该使用抽象数据类型的接口来实现,该接口具有定义良好的操作,例如添加、删除和toString方法来打印其内容。我正在为1,1和6,1调用我的delete方法。但当我打印列表时,它只显示1,1处的片段(应该删除)。这意味着出了问题(对于链表代码,最好手边有笔和纸,在手动“执行”代码的每一行时浏览代码并更新节点和指针的图形。链表可能非常棘手。
  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;
    }
}
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;
}