-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKing.java
More file actions
82 lines (80 loc) · 2.53 KB
/
King.java
File metadata and controls
82 lines (80 loc) · 2.53 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
import javax.swing.JLabel;
import java.util.ArrayList;
import java.util.HashSet;
public class King extends Piece{
public String type = "King";
public int x0, y0;
public King(int a, int b, char c){
super(a,b,c);
x0 = a;
y0 = b;
char boardColor = 'W';
if(c=='B')
boardColor = 'B';
}
public String type(){
return "King";
}
public ArrayList<Location> moves(Piece[][] p){
ArrayList<Location> ret = new ArrayList<Location>();
if (p == null)
return ret;
for(int i=-1; i<=1; i++){
for(int j=-1; j<=1; j++){
if(x+i<0||x+i>7||y+j<0||y+j>7)
continue;
else if(p[x+i][y+j]==null)
ret.add(new Location(x+i, y+j));
else if(p[x+i][y+j].color!=color)
ret.add(new Location(x+i, y+j));
}
}
if(x==x0&&y==y0){
if(p[x+1][y]==null && p[x+2][y]==null && p[x+3][y]!=null){
if(p[x+3][y].type().equals("Rook")){
ret.add(new Location(x+2, y));
}
}
if(p[x-1][y]==null && p[x-2][y]==null && p[x-3][y]==null && p[x-4][y]!=null){
if(p[x-4][y].type().equals("Rook"))
ret.add(new Location(x-2, y));
}
}
HashSet<Location> enemy = new HashSet<Location>();
char boardColor = 'B';
if(color == 'B')
boardColor = 'W';
for(int t=0; t<8; t++){
for(int u=0; u<8; u++){
if(p[t][u]!=null){
if(p[t][u].color!=color&&!(p[t][u].type().equals("King"))){
if(!p[t][u].type().equals("Pawn")){
for(Location l: p[t][u].moves(p))
enemy.add(l);
}
else{
int dy = 1;
if(p[t][u].color == 'B')
dy = -1;
if(t>0){
if(p[t-1][u+dy]==null)
enemy.add(new Location(t-1,u+dy));
}
if(t<7){
if(p[t+1][u+dy]==null)
enemy.add(new Location(t+1,u+dy));
}
}
}
}
}
}
ret.removeAll(enemy);
return ret;
}
public JLabel getIcon(){
if(color == 'W')
return new JLabel("\u2654");
return new JLabel("\u265A");
}
}