From aa09674791e7ec47f90ee5d6abdc350d01b59fd6 Mon Sep 17 00:00:00 2001 From: kske Date: Tue, 29 Oct 2019 06:24:44 +0100 Subject: [PATCH] Fully implemented castling conditions Closes #8 --- src/dev/kske/chess/board/Board.java | 15 +++++++-- src/dev/kske/chess/board/King.java | 48 +++++++++++++++++------------ 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/src/dev/kske/chess/board/Board.java b/src/dev/kske/chess/board/Board.java index 93fa907..3de825a 100644 --- a/src/dev/kske/chess/board/Board.java +++ b/src/dev/kske/chess/board/Board.java @@ -227,7 +227,7 @@ public class Board { } /** - * Reverts the last move. + * Reverts the last move and removes it from the log. */ public void revert() { MoveNode moveNode = log.getLast(); @@ -294,11 +294,20 @@ public class Board { * @param color The color of the king to 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 j = 0; j < 8; 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; } diff --git a/src/dev/kske/chess/board/King.java b/src/dev/kske/chess/board/King.java index 3e0c913..a864171 100644 --- a/src/dev/kske/chess/board/King.java +++ b/src/dev/kske/chess/board/King.java @@ -28,28 +28,9 @@ public class King extends Piece { return true; } } - 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 protected List getPseudolegalMoves(Position pos) { List moves = new ArrayList<>(); @@ -61,13 +42,40 @@ public class King extends Piece { } // 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 (canCastleQueenside()) moves.add(new Move(pos, new Position(2, pos.y), Move.Type.CASTLING)); 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 public Type getType() { return Type.KING; } }