add castling + fix promote undo
This commit is contained in:
@@ -58,7 +58,7 @@ public class ChessBoard {
|
||||
}
|
||||
|
||||
public void undoLastMove() {
|
||||
assert this.lastVirtualMove != null: "Can't undo at the beginning!";
|
||||
assert this.lastVirtualMove != null : "Can't undo at the beginning!";
|
||||
|
||||
undoMove(this.lastVirtualMove, this.lastEjectedPiece);
|
||||
}
|
||||
@@ -181,6 +181,45 @@ public class ChessBoard {
|
||||
return result;
|
||||
}
|
||||
|
||||
private boolean canCastle(Color color, int rookX, Direction kingDirection) {
|
||||
if (isKingInCheck(color))
|
||||
return false;
|
||||
|
||||
int colorLine = color == Color.White ? 7 : 0;
|
||||
|
||||
Coordinate kingCoords = new Coordinate(4, colorLine);
|
||||
Coordinate rookCoords = new Coordinate(rookX, colorLine);
|
||||
Piece king = pieceAt(kingCoords);
|
||||
Piece rook = pieceAt(rookCoords);
|
||||
|
||||
if (king == null || rook == null || king.hasMoved() || rook.hasMoved())
|
||||
return false;
|
||||
|
||||
for (int step = 1; step <= 2; step++) {
|
||||
Coordinate dest = Coordinate.fromIndex(kingCoords.toIndex() + step * kingDirection.getIndexOffset());
|
||||
Piece obstacle = pieceAt(dest);
|
||||
if (obstacle != null)
|
||||
return false;
|
||||
|
||||
applyMove(new Move(kingCoords, dest));
|
||||
if (isKingInCheck(color)) {
|
||||
undoLastMove();
|
||||
return false;
|
||||
}
|
||||
undoLastMove();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean canSmallCastle(Color color) {
|
||||
return canCastle(color, 7, Direction.Right);
|
||||
}
|
||||
|
||||
public boolean canBigCastle(Color color) {
|
||||
return canCastle(color, 0, Direction.Left);
|
||||
}
|
||||
|
||||
public Move getLastMove() {
|
||||
return this.lastMove;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user