-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPawn.java
83 lines (70 loc) · 2.75 KB
/
Pawn.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
import java.util.*;
public class Pawn extends Piece {
public Pawn(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()+"p";
return s;
}
public List<String> moves(Board b, String loc) {
// throw new UnsupportedOperationException();
if (color==null)
throw new UnsupportedOperationException();
int[] locArray = Helper.getLocArray(loc);
List<String> retList = new ArrayList<>();
// valid moves
if(color.getColorStr().equals("b")){
// black: [1]--
// 1 step vertically
if(locArray[1]>0){
String dest = Helper.getLocStr(locArray[0],locArray[1]-1);
if(b.isOccupied(dest)==null)
retList.add(dest);
}
// 2 step vertically
if(locArray[1]==6){
if(b.isOccupied(Helper.getLocStr(locArray[0],5))==null &&
b.isOccupied(Helper.getLocStr(locArray[0],4))==null)
retList.add(Helper.getLocStr(locArray[0],4));
}
// diagonally capture
for(int i: new int[]{1,-1}) {
if (locArray[0]+i>=0 && locArray[0]+i <8 && locArray[1]-1>=0 && locArray[1]-1 <8){
String dest = Helper.getLocStr(locArray[0]+i,locArray[1]-1);
if(Objects.equals(b.isOccupied(dest),"w")){
retList.add(dest);
}
}
}
}else {
// white: [1]++
// 1 step vertically
if(locArray[1]<7){
String dest = Helper.getLocStr(locArray[0],locArray[1]+1);
if(b.isOccupied(dest)==null)
retList.add(dest);
}
// 2 step vertically
if (locArray[1] == 1) {
if (b.isOccupied(Helper.getLocStr(locArray[0], 2)) == null &&
b.isOccupied(Helper.getLocStr(locArray[0], 3)) == null)
retList.add(Helper.getLocStr(locArray[0], 3));
}
// diagonally capture
for(int i: new int[]{1,-1}) {
if (locArray[0]+i>=0 && locArray[0]+i <8 && locArray[1]+1>=0 && locArray[1]+1 <8){
String dest = Helper.getLocStr(locArray[0]+i,locArray[1]+1);
if(Objects.equals(b.isOccupied(dest), "b"))
retList.add(dest);
}
}
}
return retList;
}
}