我是开发国际象棋应用程序的初学者android“开发人员”。
InGameActivity 类中有一个名为 currentBoardState 的变量,即 public static 。每个 ChessPiece 都有一个 boolean[][] possibleMoves 。以下方法将采用 possibleMoves 并确定是否有任何移动会导致玩家检查自己并将其设置为 false,因为它不再是可能的移动。

@Override
public void eliminateMovesThatPutYouInCheck() {
    ChessPiece[][] originalBoard = InGameActivity.currentBoardState;
    final ChessPiece emptyPiece = new EmptyPiece(Color.EMPTY);
    final ChessPiece tempKnight = new Knight(side);
    //This block eliminates moves that will cause a player
    //to put him/her self in check.
    InGameActivity.currentBoardState[x][y] = emptyPiece;
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            if (possibleMoves[i][j] == true) {
                tempKnight.x = i;
                tempKnight.y = j;
                InGameActivity.currentBoardState[i][j] = tempKnight;
                if (InGameActivity.isPlayerInCheck(side)) {
                    possibleMoves[i][j] = false;
                }
                InGameActivity.currentBoardState[i][j] = emptyPiece;
            }
        }
    }
    InGameActivity.currentBoardState = originalBoard;
}

问题是,我的 currentBoardState 变量被搞砸了,我不知道为什么,我保存了该值,然后在方法结束时将其重置,为什么它会丢失其值?

编辑:如果您需要,这里是 isPlayerInCheck 方法,谢谢。
public static boolean isPlayerInCheck(Color side) {
        List<boolean[][]> opponentsMoves = new ArrayList<boolean[][]>();
        int xKing = -1;
        int yKing = -1;

        if (side.equals(Color.WHITE)) {
            xKing = whiteKing.x;
            yKing = whiteKing.y;
        } else {
            xKing = blackKing.x;
            yKing = blackKing.y;
        }

        if (side.equals(Color.WHITE)) {
            for (int i = 0; i < 8; i++) {
                for (int j = 0; j < 8; j++) {
                    if (currentBoardState[i][j].isBlack()) {
                        opponentsMoves.add(currentBoardState[i][j].possibleMoves);
                    }
                }
            }
            for (boolean[][] b : opponentsMoves) {
                for (int i = 0; i < 8; i++) {
                    for (int j = 0; j < 8; j++) {
                        if (b[xKing][yKing] == true) {
                            return true;
                        }
                    }
                }
            }
            return false;

        } else {
            for (int i = 0; i < 8; i++) {
                for (int j = 0; j < 8; j++) {
                    if (currentBoardState[i][j].isWhite()) {
                        opponentsMoves.add(currentBoardState[i][j].possibleMoves);
                    }
                    if (currentBoardState[i][j].isBlack() && currentBoardState[i][j].getType().equals(Type.KING)) {
                        xKing = currentBoardState[i][j].x;
                        yKing = currentBoardState[i][j].y;
                    }
                }
            }
            for (boolean[][] b : opponentsMoves) {
                for (int i = 0; i < 8; i++) {
                    for (int j = 0; j < 8; j++) {
                        if (b[xKing][yKing] == true) {
                            return true;
                        }
                    }
                }
            }
            return false;

        }
    }

此外,我理解并欣赏我的代码可能非常低效,而且设计对于国际象棋来说很糟糕,但这并不是我目前真正关心的问题。

最佳答案

我假设 messed up 你的意思是 currentBoardState 变量没有被“重置”到原始板。

这是由于这一行:

ChessPiece[][] originalBoard = InGameActivity.currentBoardState;

将当前棋盘状态的 引用 传递给变量 originalBoard

当您修改 currentBoardState 时,由于 originalBoard 变量指向同一个对象,它也会被修改。

将数组复制到 originalBoard 变量,如下所示:
ChessPiece[][] originalBoard = new ChessPiece[InGameActivity.currentBoardState.length][];
for(int i = 0; i < InGameActivity.currentBoardState.length; i++){
    ChessPiece[] pieces = InGameActivity.currentBoardState[i];
    int len = pieces.length;
    originalBoard[i] = new ChessPiece[len];
    System.arraycopy(pieces, 0, originalBoard[i], 0, len);
}

//Rest of code here...

只有当您通过 进行复制时,数据的实际副本才存在。不考虑原始类型和一些不可变数据,使用赋值运算符 = 只是将 RHS 的引用分配给 LHS。

关于java - 二维对象数组不保留值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20141429/

10-09 12:56