81 lines
2.6 KiB
Java
81 lines
2.6 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>
|
|
* Author: <strong>Kai S. K. Engelbart</strong>
|
|
*/
|
|
public class King extends Piece {
|
|
|
|
public King(Color color, Board board) {
|
|
super(color, board);
|
|
}
|
|
|
|
@Override
|
|
public boolean isValidMove(Move move) {
|
|
// Castling
|
|
if (getMoveCounter() == 0 && move.xDist == 2 && move.yDist == 0) {
|
|
|
|
// Kingside
|
|
if (board.getBoardArr()[7][move.pos.y] != null && board.getBoardArr()[7][move.pos.y].getType() == Type.ROOK
|
|
&& isFreePath(new Move(new Position(5, move.pos.y), new Position(7, move.pos.y)))) {
|
|
move.type = Move.Type.CASTLING;
|
|
return true;
|
|
}
|
|
|
|
// Queenside
|
|
if (board.getBoardArr()[0][move.pos.y] != null && board.getBoardArr()[0][move.pos.y].getType() == Type.ROOK
|
|
&& isFreePath(new Move(new Position(1, move.pos.y), new Position(4, move.pos.y)))) {
|
|
move.type = Move.Type.CASTLING;
|
|
return true;
|
|
}
|
|
|
|
}
|
|
|
|
return move.xDist <= 1 && move.yDist <= 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
|
|
// TODO: Check attacked squares in between
|
|
// TODO: Castling out of check?
|
|
if (getMoveCounter() == 0) {
|
|
|
|
// Kingside
|
|
if (board.getBoardArr()[7][pos.y] != null && board.getBoardArr()[7][pos.y].getType() == Type.ROOK
|
|
&& isFreePath(new Move(new Position(5, pos.y), new Position(7, pos.y))))
|
|
moves.add(new Move(pos, new Position(6, pos.y), Move.Type.CASTLING));
|
|
|
|
// Queenside
|
|
if (board.getBoardArr()[0][pos.y] != null && board.getBoardArr()[0][pos.y].getType() == Type.ROOK
|
|
&& isFreePath(new Move(new Position(1, pos.y), new Position(4, pos.y))))
|
|
moves.add(new Move(pos, new Position(2, pos.y), Move.Type.CASTLING));
|
|
}
|
|
|
|
return moves;
|
|
}
|
|
|
|
@Override
|
|
protected boolean isFreePath(Move move) {
|
|
for (int i = move.pos.x, j = move.pos.y; i != move.dest.x || j != move.dest.y; i += move.xSign, j += move.ySign)
|
|
if (board.getBoardArr()[i][j] != null) return false;
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public Type getType() { return Type.KING; }
|
|
}
|