Added King's castling

This commit is contained in:
Filip Znachor 2023-04-18 19:13:57 +02:00
parent bb9c4a9467
commit e2dd3f29f0

View file

@ -45,7 +45,33 @@ public class King extends AbstractPiece {
if(!chessboard.isEndangered(player, i, j)) setPossibleMove(moves, i, j);
}
}
if(checkCastling(-1)) setPossibleMove(moves, 1, y);
if(checkCastling(1)) setPossibleMove(moves, 6, y);
return moves;
}
public boolean checkCastling(int direction) {
boolean rookNoMove = (player.getLeftRook().moveCount == 0 && direction == -1) || (player.getRightRook().moveCount == 0 && direction == 1);
if(moveCount == 0 && !player.inCheck() && rookNoMove) {
for (int pX = x + direction; (pX > 0 && pX < 7); pX += direction) {
if(chessboard.isEndangered(player, pX, y) || chessboard.getPiece(new PiecePosition(pX, y)) != null) return false;
}
return true;
}
return false;
}
@Override
public boolean move(PiecePosition pos) {
boolean canMove = super.move(pos);
if(moveCount == 1 && x == 6) {
chessboard.grabPiece(new PiecePosition(7, y));
chessboard.returnPiece(new PiecePosition(5, y));
}
if(moveCount == 1 && x == 1) {
chessboard.grabPiece(new PiecePosition(0, y));
chessboard.returnPiece(new PiecePosition(3, y));
}
return canMove;
}
}