-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathBishop.java
More file actions
83 lines (72 loc) · 2.65 KB
/
Bishop.java
File metadata and controls
83 lines (72 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package chess.piece;
import chess.Color;
import chess.Column;
import chess.Position;
import chess.Row;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
public class Bishop extends Piece {
private static final List<Position> WHITE_BISHOP_POSITIONS = List.of(
new Position(Row.ONE, Column.C), new Position(Row.ONE, Column.F)
);
private static final List<Position> BLACK_BISHOP_POSITIONS = List.of(
new Position(Row.EIGHT, Column.C), new Position(Row.EIGHT, Column.F)
);
private static final List<Function<Position, Position>> MOVEMENTS = List.of(
Position::moveLeftUp,
Position::moveLeftDown,
Position::moveRightUp,
Position::moveRightDown
);
public Bishop(final Position position, final Color color) {
super(position, color);
}
public static List<Piece> initialize() {
final List<Piece> pieces = new ArrayList<>();
final List<Bishop> whiteBishops = WHITE_BISHOP_POSITIONS.stream()
.map(position -> new Bishop(position, Color.WHITE))
.toList();
final List<Bishop> blackBishops = BLACK_BISHOP_POSITIONS.stream()
.map(position -> new Bishop(position, Color.BLACK))
.toList();
pieces.addAll(whiteBishops);
pieces.addAll(blackBishops);
return pieces;
}
@Override
public List<Position> calculateAvailablePositions(final Board board) {
final List<Position> positions = new ArrayList<>();
for (Function<Position, Position> movement : MOVEMENTS) {
addAvailablePositions(board, getPosition(), movement, positions);
}
return positions;
}
@Override
public Piece copyOf(final Position position) {
return new Bishop(position, getColor());
}
@Override
public PieceType getType() {
return PieceType.BISHOP;
}
private void addAvailablePositions(final Board board, final Position position, final Function<Position, Position> function, final List<Position> positions) {
Position movedPosition;
try {
movedPosition = function.apply(position);
} catch (IllegalStateException e) {
return;
}
if (!isAvailablePosition(board, movedPosition)) {
return;
}
if (!board.isEmptyPosition(movedPosition)) {
if (board.isEnemy(movedPosition, this)) {
positions.add(movedPosition);
}
return;
}
positions.add(movedPosition);
addAvailablePositions(board, movedPosition, function, positions);
}
}