Fixed king movements with regards to check detection
This commit is contained in:
parent
11f1795d99
commit
ab6fa7e52f
@ -40,24 +40,37 @@ public class Board {
|
|||||||
else {
|
else {
|
||||||
// Move piece
|
// Move piece
|
||||||
// Save destination piece for possible canceling of the move
|
// Save destination piece for possible canceling of the move
|
||||||
Piece tmpPiece = getDest(move);
|
Piece capturePiece = getDest(move);
|
||||||
setDest(move, getPos(move));
|
setDest(move, getPos(move));
|
||||||
setPos(move, null);
|
setPos(move, null);
|
||||||
|
|
||||||
// Revert move if it caused a check for its team
|
// Update the king's position if the moved piece is the king
|
||||||
if (checkCheck(piece.getColor())) {
|
|
||||||
setPos(move, getDest(move));
|
|
||||||
setDest(move, tmpPiece);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update the king's position if the moved piece is a king
|
|
||||||
if (piece.getType() == Type.KING) kingPos.put(piece.getColor(), move.dest);
|
if (piece.getType() == Type.KING) kingPos.put(piece.getColor(), move.dest);
|
||||||
|
|
||||||
|
// Revert move if it caused a check for its team
|
||||||
|
if (checkCheck(piece.getColor())) {
|
||||||
|
revert(move, capturePiece);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverts a move.
|
||||||
|
*
|
||||||
|
* @param move The move to revert
|
||||||
|
* @param capturedPiece The piece that has been captured when the move has been
|
||||||
|
* applied
|
||||||
|
*/
|
||||||
|
public void revert(Move move, Piece capturedPiece) {
|
||||||
|
setPos(move, getDest(move));
|
||||||
|
setDest(move, capturedPiece);
|
||||||
|
|
||||||
|
// Update the king's position if the moved piece is the king
|
||||||
|
if (getPos(move).getType() == Type.KING) kingPos.put(getPos(move).getColor(), move.pos);
|
||||||
|
}
|
||||||
|
|
||||||
public boolean checkCheck(Color color) {
|
public boolean checkCheck(Color color) {
|
||||||
for (int i = 0; i < 8; i++)
|
for (int i = 0; i < 8; i++)
|
||||||
for (int j = 0; j < 8; j++)
|
for (int j = 0; j < 8; j++)
|
||||||
|
Reference in New Issue
Block a user