This repository has been archived on 2021-02-18. You can view files and clone it, but cannot push or open issues or pull requests.
chess/src/main/java/dev/kske/chess/board/Castling.java

63 lines
1.6 KiB
Java

package dev.kske.chess.board;
/**
* Project: <strong>Chess</strong><br>
* File: <strong>Castling.java</strong><br>
* Created: <strong>2 Nov 2019</strong><br>
*
* @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";
}
}