package dev.kske.chess.board; /** * Project: Chess
* File: Castling.java
* Created: 2 Nov 2019
* * @since Chess v0.5-alpha * @author Kai S. K. Engelbart */ public class Castling extends Move { private final Move rookMove; /** * Creates a castling move. * * @param pos the position of this castling move * @param dest the destination of this castling move */ public Castling(Position pos, Position dest) { super(pos, dest); rookMove = dest.x == 6 ? new Move(7, pos.y, 5, pos.y) : new Move(0, pos.y, 3, pos.y); } /** * Creates a castling move. * * @param xPos the horizontal position of this castling move * @param yPos the vertical position of this castling move * @param xDest the horizontal destination of this castling move * @param yDest the vertical destination of this castling move */ public Castling(int xPos, int yPos, int xDest, int yDest) { this(new Position(xPos, yPos), new Position(xDest, yDest)); } @Override public void execute(Board board) { // Move the king and the rook super.execute(board); rookMove.execute(board); } @Override public void revert(Board board, Piece capturedPiece) { // Move the king and the rook super.revert(board, capturedPiece); rookMove.revert(board, null); } /** * @return {@code O-O-O} for a queenside castling or {@code O-O} for a * kingside * castling */ @Override public String toSAN(Board board) { return rookMove.pos.x == 0 ? "O-O-O" : "O-O"; } }