107 lines
2.9 KiB
Java
107 lines
2.9 KiB
Java
package dev.kske.chess.board;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* Project: <strong>Chess</strong><br>
|
|
* File: <strong>King.java</strong><br>
|
|
* Created: <strong>01.07.2019</strong><br>
|
|
*
|
|
* @since Chess v0.1-alpha
|
|
* @author Kai S. K. Engelbart
|
|
*/
|
|
public class King extends Piece {
|
|
|
|
/**
|
|
* Creates king {@link Piece}.
|
|
*
|
|
* @param color the color of this king
|
|
* @param board the board on which this king will be placed
|
|
*/
|
|
public King(Color color, Board board) {
|
|
super(color, board);
|
|
}
|
|
|
|
@Override
|
|
public boolean isValidMove(Move move) {
|
|
return move.getxDist() == 2 && move.getyDist() == 0
|
|
&& (move.getDest().x == 6 && canCastleKingside()
|
|
|| move.getDest().x == 2 && canCastleQueenside())
|
|
|| move.getxDist() <= 1 && move.getyDist() <= 1
|
|
&& checkDestination(move);
|
|
}
|
|
|
|
@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));
|
|
if (
|
|
board.getDest(move) == null
|
|
|| board.getDest(move).getColor() != getColor()
|
|
)
|
|
moves.add(move);
|
|
}
|
|
|
|
// Castling
|
|
if (canCastleKingside())
|
|
moves.add(new Castling(pos, new Position(6, pos.y)));
|
|
if (canCastleQueenside())
|
|
moves.add(new Castling(pos, new Position(2, pos.y)));
|
|
|
|
return moves;
|
|
}
|
|
|
|
private boolean canCastleKingside() {
|
|
if (
|
|
board.getLog().getCastlingRights()[getColor() == Color.WHITE
|
|
? MoveNode.WHITE_KINGSIDE
|
|
: MoveNode.BLACK_KINGSIDE]
|
|
) {
|
|
int y = getColor() == Color.WHITE ? 7 : 0;
|
|
Position kingPos = new Position(4, y);
|
|
Position jumpPos = new Position(5, y);
|
|
Position kingDest = new Position(6, y);
|
|
Position rookPos = new Position(7, y);
|
|
return canCastle(kingPos, kingDest, rookPos, jumpPos);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private boolean canCastleQueenside() {
|
|
if (
|
|
board.getLog().getCastlingRights()[getColor() == Color.WHITE
|
|
? MoveNode.WHITE_QUEENSIDE
|
|
: MoveNode.BLACK_QUEENSIDE]
|
|
) {
|
|
int y = getColor() == Color.WHITE ? 7 : 0;
|
|
Position kingPos = new Position(4, y);
|
|
Position jumpPos = new Position(3, y);
|
|
Position freeDest = new Position(1, y);
|
|
Position rookPos = new Position(0, y);
|
|
return canCastle(kingPos, freeDest, rookPos, jumpPos);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private boolean canCastle(
|
|
Position kingPos, Position freeDest, Position rookPos, Position jumpPos
|
|
) {
|
|
Piece rook = board.get(rookPos);
|
|
return rook != null && rook instanceof Rook && isFreePath(
|
|
new Move(kingPos, freeDest)
|
|
) && !board.isAttacked(kingPos, getColor().opposite())
|
|
&& !board.isAttacked(jumpPos, getColor().opposite());
|
|
}
|
|
|
|
@Override
|
|
public int getValue() { return 0; }
|
|
}
|