2019-07-06 18:05:00 +02:00
|
|
|
package dev.kske.chess.board;
|
2019-07-01 19:50:42 +02:00
|
|
|
|
2019-07-05 14:14:48 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2019-07-01 19:50:42 +02:00
|
|
|
/**
|
|
|
|
* Project: <strong>Chess</strong><br>
|
|
|
|
* File: <strong>King.java</strong><br>
|
|
|
|
* Created: <strong>01.07.2019</strong><br>
|
|
|
|
* Author: <strong>Kai S. K. Engelbart</strong>
|
|
|
|
*/
|
|
|
|
public class King extends Piece {
|
|
|
|
|
2019-10-25 11:34:07 +02:00
|
|
|
public King(Color color, Board board) { super(color, board); }
|
2019-07-01 19:50:42 +02:00
|
|
|
|
2019-07-01 21:46:30 +02:00
|
|
|
@Override
|
2019-07-02 13:49:36 +02:00
|
|
|
public boolean isValidMove(Move move) {
|
2019-07-13 11:38:44 +02:00
|
|
|
// Castling
|
2019-07-22 21:40:25 +02:00
|
|
|
if (move.xDist == 2 && move.yDist == 0) {
|
|
|
|
if (canCastleKingside()) {
|
2019-07-13 11:38:44 +02:00
|
|
|
move.type = Move.Type.CASTLING;
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-22 21:40:25 +02:00
|
|
|
if (canCastleQueenside()) {
|
2019-07-13 11:38:44 +02:00
|
|
|
move.type = Move.Type.CASTLING;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return move.xDist <= 1 && move.yDist <= 1 && checkDestination(move);
|
2019-07-01 21:46:30 +02:00
|
|
|
}
|
|
|
|
|
2019-07-22 21:40:25 +02:00
|
|
|
public boolean canCastleKingside() {
|
2019-10-25 11:34:07 +02:00
|
|
|
if (board.getLog().getCastlingRights()[getColor() == Color.WHITE ? MoveNode.WHITE_KINGSIDE : MoveNode.BLACK_KINGSIDE]) {
|
2019-07-22 21:40:25 +02:00
|
|
|
int y = getColor() == Color.WHITE ? 7 : 0;
|
|
|
|
Position rookPos = new Position(7, y);
|
|
|
|
Piece rook = board.get(rookPos);
|
2019-10-25 11:34:07 +02:00
|
|
|
return rook != null && rook.getType() == Type.ROOK && isFreePath(new Move(new Position(4, y), new Position(6, y)));
|
2019-07-22 21:40:25 +02:00
|
|
|
} else return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean canCastleQueenside() {
|
2019-10-25 11:34:07 +02:00
|
|
|
if (board.getLog().getCastlingRights()[getColor() == Color.WHITE ? MoveNode.WHITE_QUEENSIDE : MoveNode.BLACK_QUEENSIDE]) {
|
2019-07-22 21:40:25 +02:00
|
|
|
int y = getColor() == Color.WHITE ? 7 : 0;
|
|
|
|
Position rookPos = new Position(0, y);
|
|
|
|
Piece rook = board.get(rookPos);
|
2019-10-25 11:34:07 +02:00
|
|
|
return rook != null && rook.getType() == Type.ROOK && isFreePath(new Move(new Position(4, y), new Position(1, y)));
|
2019-07-22 21:40:25 +02:00
|
|
|
} else return false;
|
|
|
|
}
|
|
|
|
|
2019-07-05 14:14:48 +02:00
|
|
|
@Override
|
|
|
|
protected List<Move> getPseudolegalMoves(Position pos) {
|
|
|
|
List<Move> moves = new ArrayList<>();
|
|
|
|
for (int i = Math.max(0, pos.x - 1); i < Math.min(8, pos.x + 2); i++)
|
|
|
|
for (int j = Math.max(0, pos.y - 1); j < Math.min(8, pos.y + 2); j++)
|
|
|
|
if (i != pos.x || j != pos.y) {
|
|
|
|
Move move = new Move(pos, new Position(i, j));
|
2019-07-06 12:42:21 +02:00
|
|
|
if (board.getDest(move) == null || board.getDest(move).getColor() != getColor()) moves.add(move);
|
2019-07-05 14:14:48 +02:00
|
|
|
}
|
2019-07-13 11:38:44 +02:00
|
|
|
|
|
|
|
// Castling
|
2019-07-22 21:40:25 +02:00
|
|
|
// TODO: Condition: cannot castle out of, through or into check
|
|
|
|
if (canCastleKingside()) moves.add(new Move(pos, new Position(6, pos.y), Move.Type.CASTLING));
|
|
|
|
if (canCastleQueenside()) moves.add(new Move(pos, new Position(2, pos.y), Move.Type.CASTLING));
|
2019-07-13 11:38:44 +02:00
|
|
|
|
2019-07-05 14:14:48 +02:00
|
|
|
return moves;
|
|
|
|
}
|
|
|
|
|
2019-07-01 19:50:42 +02:00
|
|
|
@Override
|
|
|
|
public Type getType() { return Type.KING; }
|
|
|
|
}
|