MCPcopy Create free account
hub / github.com/coding-parrot/chess-engine / castlingMoves

Method castlingMoves

src/main/java/pieces/King.java:137–163  ·  view source on GitHub ↗
(final Board board, final Piece king, final Set<Move> kingMoves)

Source from the content-addressed store, hash-verified

135 }
136
137 private static Move[] castlingMoves(final Board board, final Piece king, final Set<Move> kingMoves) {
138 final Move[] castleMoves = new Move[2];
139 int castles = 0;
140 final int row = king.color.ordinal() * 7;
141 if (king.position.row == row) {
142 for (int i = 0; i <= 1; i++) {
143 if (board.canCastle[king.color.ordinal()][i]) {
144 final Piece rook = board.getPiece(row, i * 7);
145 if (rook != null && rook.sameType(PieceType.ROOK)) {
146 final int colDiff = i == 0 ? -1 : 1;
147 final int col = 4 + colDiff;
148 final boolean clear = board.isEmpty(row, col)
149 && kingMoves.stream()
150 .map(move -> move.target)
151 .anyMatch(cell -> Cell.get(row, col).equals(cell));
152 final Cell kingCastleCell = Cell.get(row, col + colDiff);
153 if (clear && board.isEmpty(row, col + colDiff) && !getIllegalSquares(board, Color.opponent(king.color), Collections.singleton(kingCastleCell), kingCastleCell).contains(kingCastleCell)) {
154 if (i == 1 || board.isEmpty(row, 1)) {
155 castleMoves[castles++] = Move.get(king, Cell.get(row, 4 + 2 * colDiff), false);
156 }
157 }
158 }
159 }
160 }
161 }
162 return castleMoves;
163 }
164
165 private static boolean xRay(Move move, Board board, Piece king) {
166 final Line line = new Line(move.piece.position, move.target);

Callers 1

filterIllegalMovesMethod · 0.95

Calls 8

sameTypeMethod · 0.95
getMethod · 0.95
getIllegalSquaresMethod · 0.95
opponentMethod · 0.95
getMethod · 0.95
getPieceMethod · 0.80
isEmptyMethod · 0.80
equalsMethod · 0.45

Tested by

no test coverage detected