Fully implemented castling conditions

Closes #8
This commit is contained in:
Kai S. K. Engelbart 2019-10-29 06:24:44 +01:00
parent 4f19244d40
commit aa09674791
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
2 changed files with 40 additions and 23 deletions

View File

@ -227,7 +227,7 @@ public class Board {
} }
/** /**
* Reverts the last move. * Reverts the last move and removes it from the log.
*/ */
public void revert() { public void revert() {
MoveNode moveNode = log.getLast(); MoveNode moveNode = log.getLast();
@ -294,11 +294,20 @@ public class Board {
* @param color The color of the king to check * @param color The color of the king to check
* @return {@code true}, if the king is in check * @return {@code true}, if the king is in check
*/ */
public boolean checkCheck(Color color) { public boolean checkCheck(Color color) { return isAttacked(kingPos.get(color), color.opposite()); }
/**
* Checks, if a field can be attacked by pieces of a certain color.
*
* @param dest the field to check
* @param color the color of a potential attacker piece
* @return {@code true} if a move with the destination {@code dest}
*/
public boolean isAttacked(Position dest, Color color) {
for (int i = 0; i < 8; i++) for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++) { for (int j = 0; j < 8; j++) {
Position pos = new Position(i, j); Position pos = new Position(i, j);
if (get(pos) != null && get(pos).getColor() != color && get(pos).isValidMove(new Move(pos, kingPos.get(color)))) return true; if (get(pos) != null && get(pos).getColor() == color && get(pos).isValidMove(new Move(pos, dest))) return true;
} }
return false; return false;
} }

View File

@ -28,28 +28,9 @@ public class King extends Piece {
return true; return true;
} }
} }
return move.xDist <= 1 && move.yDist <= 1 && checkDestination(move); return move.xDist <= 1 && move.yDist <= 1 && checkDestination(move);
} }
public boolean canCastleKingside() {
if (board.getLog().getCastlingRights()[getColor() == Color.WHITE ? MoveNode.WHITE_KINGSIDE : MoveNode.BLACK_KINGSIDE]) {
int y = getColor() == Color.WHITE ? 7 : 0;
Position rookPos = new Position(7, y);
Piece rook = board.get(rookPos);
return rook != null && rook.getType() == Type.ROOK && isFreePath(new Move(new Position(4, y), new Position(6, y)));
} else return false;
}
public boolean canCastleQueenside() {
if (board.getLog().getCastlingRights()[getColor() == Color.WHITE ? MoveNode.WHITE_QUEENSIDE : MoveNode.BLACK_QUEENSIDE]) {
int y = getColor() == Color.WHITE ? 7 : 0;
Position rookPos = new Position(0, y);
Piece rook = board.get(rookPos);
return rook != null && rook.getType() == Type.ROOK && isFreePath(new Move(new Position(4, y), new Position(1, y)));
} else return false;
}
@Override @Override
protected List<Move> getPseudolegalMoves(Position pos) { protected List<Move> getPseudolegalMoves(Position pos) {
List<Move> moves = new ArrayList<>(); List<Move> moves = new ArrayList<>();
@ -61,13 +42,40 @@ public class King extends Piece {
} }
// Castling // Castling
// TODO: Condition: cannot castle out of, through or into check
if (canCastleKingside()) moves.add(new Move(pos, new Position(6, pos.y), Move.Type.CASTLING)); if (canCastleKingside()) moves.add(new Move(pos, new Position(6, pos.y), Move.Type.CASTLING));
if (canCastleQueenside()) moves.add(new Move(pos, new Position(2, pos.y), Move.Type.CASTLING)); if (canCastleQueenside()) moves.add(new Move(pos, new Position(2, pos.y), Move.Type.CASTLING));
return moves; 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);
} else 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);
} else return false;
}
private boolean canCastle(Position kingPos, Position freeDest, Position rookPos, Position jumpPos) {
Piece rook = board.get(rookPos);
return rook != null && rook.getType() == Type.ROOK && isFreePath(new Move(kingPos, freeDest))
&& !board.isAttacked(kingPos, getColor().opposite()) && !board.isAttacked(jumpPos, getColor().opposite());
}
@Override @Override
public Type getType() { return Type.KING; } public Type getType() { return Type.KING; }
} }