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

Method getMoves

src/main/java/commons/Utils.java:12–44  ·  view source on GitHub ↗
(final Board board,
                                      final int maxDistance,
                                      final BiFunction<Boolean, Boolean, Boolean> isLegal,
                                      final Piece piece)

Source from the content-addressed store, hash-verified

10
11public class Utils {
12 public static LegalMoves getMoves(final Board board,
13 final int maxDistance,
14 final BiFunction<Boolean, Boolean, Boolean> isLegal,
15 final Piece piece) {
16 final Set<Move> moves = new HashSet<>();
17 final Set<Move> guards = new HashSet<>();
18 for (int rowDiff = -1; rowDiff <= 1; rowDiff++) {
19 for (int colDiff = -1; colDiff <= 1; colDiff++) {
20 boolean straightRow = rowDiff == 0;
21 boolean straightCol = colDiff == 0;
22 if (isLegal.apply(straightRow, straightCol) && !(straightRow && straightCol)) {
23 int row = piece.position.row + rowDiff, col = piece.position.col + colDiff;
24 for (int i = 1; i <= maxDistance; i++, row = row + rowDiff, col = col + colDiff) {
25 if (Utils.withinBoardLimits(row, col)) {
26 if (board.isEmpty(row, col)) {
27 moves.add(Move.get(piece, Cell.get(row, col), false));
28 } else {
29 if (board.getPiece(row, col).color != piece.color) {
30 moves.add(Move.get(piece, Cell.get(row, col), true));
31 } else {
32 guards.add(Move.get(piece, Cell.get(row, col), false));
33 }
34 break;
35 }
36 } else {
37 break;
38 }
39 }
40 }
41 }
42 }
43 return new LegalMoves(moves, guards);
44 }
45
46 public static boolean withinBoardLimits(final int row, final int col) {
47 return row < 8 && row >= 0 && col < 8 && col >= 0;

Callers 4

getMoveListMethod · 0.95
getMoveListMethod · 0.95
getMoveListMethod · 0.95
getMoveListMethod · 0.95

Calls 5

withinBoardLimitsMethod · 0.95
getMethod · 0.95
getMethod · 0.95
isEmptyMethod · 0.80
getPieceMethod · 0.80

Tested by

no test coverage detected