-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRook.java
105 lines (95 loc) · 3.93 KB
/
Rook.java
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.util.*;
public class Rook extends Piece {
public Rook(Color c) {
this.color=c;
// throw new UnsupportedOperationException();
}
// implement appropriate methods
public String toString() {
// throw new UnsupportedOperationException();
if (color==null)
throw new UnsupportedOperationException();
return color.getColorStr()+"r";
}
public List<String> moves(Board b, String loc) {
// throw new UnsupportedOperationException();
int[] locArray = Helper.getLocArray(loc);
List<String> retList = new ArrayList<>();
// find all valid moves
Map<String, Boolean> isBlocked=new HashMap<>();
isBlocked.put("left", Boolean.FALSE);
isBlocked.put("right", Boolean.FALSE);
isBlocked.put("up", Boolean.FALSE);
isBlocked.put("down", Boolean.FALSE);
// 4 path with different direction, stop until blocked
for(int i = 1;i<8;i++) {
// horizontal: to left & right
// left -i
if (locArray[0] >= i) {
String leftLoc = Helper.getLocStr(locArray[0] - i, locArray[1]);
if (!isBlocked.get("left"))
if (b.isOccupied(leftLoc) == null) {
// empty at left
retList.add(leftLoc);
} else if (!color.getColorStr().equals(b.isOccupied(leftLoc))) {
// enemy at left
retList.add(leftLoc);
isBlocked.put("left", Boolean.TRUE);
} else {
// same color at left
isBlocked.put("left", Boolean.TRUE);
}
}
// right +i
if (locArray[0]+i < 8) {
String rightLoc = Helper.getLocStr(locArray[0] + i, locArray[1]);
if (!isBlocked.get("right"))
if (b.isOccupied(rightLoc) == null) {
// empty at right
retList.add(rightLoc);
} else if (!color.getColorStr().equals(b.isOccupied(rightLoc))) {
// enemy at right
retList.add(rightLoc);
isBlocked.put("right", Boolean.TRUE);
} else {
// same color at right
isBlocked.put("right", Boolean.TRUE);
}
}
// vertical: up & down
// up +1
if (locArray[1]+i < 8) {
String upLoc = Helper.getLocStr(locArray[0], locArray[1] + i);
if (!isBlocked.get("up"))
if (b.isOccupied(upLoc) == null) {
// empty at up
retList.add(upLoc);
} else if (!color.getColorStr().equals(b.isOccupied(upLoc))) {
// enemy at up
retList.add(upLoc);
isBlocked.put("up", Boolean.TRUE);
} else {
// same color at up
isBlocked.put("up", Boolean.TRUE);
}
}
// down -1
if (locArray[1]-i >= 0) {
String downLoc = Helper.getLocStr(locArray[0], locArray[1] - i);
if (!isBlocked.get("down"))
if (b.isOccupied(downLoc) == null) {
// empty at down
retList.add(downLoc);
} else if (!color.getColorStr().equals(b.isOccupied(downLoc))) {
// enemy at down
retList.add(downLoc);
isBlocked.put("down", Boolean.TRUE);
} else {
// same color at down
isBlocked.put("down", Boolean.TRUE);
}
}
}
return retList;
}
}