我是开发国际象棋应用程序的初学者“开发者”。
InGameActivity
类中有一个名为currentBoardState
的变量public static
。每个ChessPiece
都有一个boolean[][] possibleMoves
。以下方法将采用possibleMoves
并确定是否有任何移动会导致玩家对其进行检查并将其设置为假,因为它不再是可能的移动。
@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;
}
}
另外,我理解并理解我的代码效率可能非常低,而且国际象棋的设计很糟糕,但这并不是我现在所关注的。
答案 0 :(得分:2)
我假设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。
答案 1 :(得分:1)
如果我正确理解您的代码,我认为您需要将原始代码复制到新数组中,例如:
ChessPiece[][] originalBoard = Arrays.copyOf(
InGameActivity.currentBoardState, InGameActivity.currentBoardState.length);
这可以防止修改原始数组。
答案 2 :(得分:0)
你有问题:
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;
}