Fixed king movements with regards to check detection

This commit is contained in:
Kai S. K. Engelbart 2019-07-02 20:40:28 +02:00
parent ba77a66e48
commit f15898d29e

View File

@ -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++)