-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathBishop.java
More file actions
69 lines (57 loc) · 2.16 KB
/
Bishop.java
File metadata and controls
69 lines (57 loc) · 2.16 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
package chess.piece;
import chess.Color;
import chess.Movement;
import chess.Position;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class Bishop implements ChessPiece {
private final Color color;
private final List<Movement> movements = List.of(
Movement.LEFT_UP,
Movement.RIGHT_UP,
Movement.LEFT_DOWN,
Movement.RIGHT_DOWN
);
public Bishop(Color color) {
this.color = color;
}
@Override
public Position move(Position from, Position to, Map<Position, ChessPiece> positions) {
if (!isValidPath(from, to, positions)) {
throw new IllegalArgumentException("경로가 유효하지 않습니다.");
}
return to;
}
private boolean isValidPath(Position from, Position to, Map<Position, ChessPiece> positions) {
List<Position> availableDestinations = getAvailableDestinations(from, positions);
return availableDestinations.contains(to);
}
public List<Position> getAvailableDestinations(Position startPosition, Map<Position, ChessPiece> positions) {
List<Position> destinations = new ArrayList<>();
for (Movement movement : movements) {
Position currentPosition = startPosition;
while (currentPosition.canMove(movement)) {
currentPosition = currentPosition.move(movement);
if (!canMove(currentPosition, positions)) {
break;
}
destinations.add(currentPosition);
if (positions.containsKey(currentPosition)) {
break;
}
}
}
return destinations;
}
private boolean canMove(Position targetPosition, Map<Position, ChessPiece> positions) {
return !positions.containsKey(targetPosition) || canCatch(targetPosition, positions);
}
private boolean canCatch(Position targetPosition, Map<Position, ChessPiece> positions) {
return positions.containsKey(targetPosition) && positions.get(targetPosition).getColor() != color;
}
@Override
public Color getColor() {
return color;
}
}