2d Array of non-persistent objects

I am a beginner android "developer" developing a chess application.

In a class InGameActivitycalled currentBoardStatethere is one variable that public static. Everyone ChessPiecehas boolean[][] possibleMoves. This next method will accept possibleMovesand determine if any of the moves can force the player to put themselves to the test and set it to false, because this is no longer a possible move.

@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;
}

The problem is that my variable currentBoardStategot messed up and I don’t know why, I saved the value, then reset at the end of the method, why does it lose its values?

EDIT: Here's a method isPlayerInCheckif you need it, thanks.

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;

        }
    }

, , , , , , , .

+4
3

, messed up , currentBoardState "reset" .

:

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.

+2

, , , :

ChessPiece[][] originalBoard = Arrays.copyOf(
    InGameActivity.currentBoardState, InGameActivity.currentBoardState.length);

.

+1

You have a problem:

if (possibleMoves[i][j]) 
{ 
    tempKnight.x = i;
    tempKnight.y = j;
    InGameActivity.currentBoardState[i][j] = tempKnight;
    if (InGameActivity.isPlayerInCheck(side))
    {
       possibleMoves[i][j] = false;
    }
    /** whatever was before next line resets the value at i,j: puts empty piece here */
    InGameActivity.currentBoardState[i][j] = emptyPiece;
}
0
source

All Articles