-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKnight.java
48 lines (41 loc) · 1.52 KB
/
Knight.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
import java.util.*;
public class Knight extends Piece {
public Knight(Color c) {
color = c;
// throw new UnsupportedOperationException();
}
// implement appropriate methods
public String toString() {
// throw new UnsupportedOperationException();
if (color==null)
throw new UnsupportedOperationException();
String s = color.getColorStr()+"n";
return s;
}
public List<String> moves(Board b, String loc) {
// throw new UnsupportedOperationException();
int[] locArray = Helper.getLocArray(loc);
List<String> retList = new ArrayList<>();
// valid moves
for(int i: new int[]{2,-2}) {
for (int j: new int[]{1,-1}) {
//check boundary
if (locArray[0]+i>=0 && locArray[0]+i <8 && locArray[1]+j>=0 && locArray[1]+j <8){
String dest = Helper.getLocStr(locArray[0]+i,locArray[1]+j);
if(!color.getColorStr().equals(b.isOccupied(dest)))
retList.add(dest);
}
}
}
for(int j: new int[]{2,-2}) {
for (int i: new int[]{1,-1}) {
if (locArray[0]+i>=0 && locArray[0]+i <8 && locArray[1]+j>=0 && locArray[1]+j <8) {
String dest = Helper.getLocStr(locArray[0]+i,locArray[1]+j);
if(!color.getColorStr().equals(b.isOccupied(dest)))
retList.add(dest);
}
}
}
return retList;
}
}