(final Board board,
final int maxDistance,
final BiFunction<Boolean, Boolean, Boolean> isLegal,
final Piece piece)
| 10 | |
| 11 | public 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; |
no test coverage detected