-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKing.java
142 lines (130 loc) · 4.82 KB
/
King.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import java.util.HashSet;
import javax.swing.ImageIcon;
@SuppressWarnings("serial")
public class King extends Piece {
//private boolean inCheck;
private boolean canCastle;
public King(Shade clr, int[] startCoords, Chessboard cb){
super(clr, startCoords, cb,"KING");
//inCheck = false;
canCastle = true;
if (clr == Shade.WHITE){
try{
setIcon(new ImageIcon("Images/whiteKing.png"));
setText("");
} catch (Exception e){
//Keep label as-is if image not found
}
} else {
try{
setIcon(new ImageIcon("Images/blackKing.png"));
setText("");
} catch (Exception e){
//Keep label as-is if image not found
}
}
}
public boolean validMove(Coord cd){
if (cd == null || !(c.getSquare(cd).canMove(this.color))) return false;
if (coords.vectorTo(cd).equals(new Coord(0,0))) return false;
if (c.getSquare(cd).isAttacked(this.color)) return false;
if (coords.vectorTo(cd).equals(new Coord(2,0))){
if (this.color == Shade.BLACK){
if ((c.getSquare(new Coord (7,7)).getOccupant() instanceof Rook)){
if (this.canCastle
&& ((Rook) c.getSquare(new Coord(7,7)).getOccupant()).canCastle()){
if (!(c.getSquare(new Coord(5,7)).isOccupied()
|| c.getSquare(new Coord(6,7)).isOccupied())){
if(!(c.getSquare(new Coord(5,7)).isAttacked(this.color)
|| c.getSquare(new Coord(6,7)).isAttacked(this.color)
|| c.getSquare(this.coords).isAttacked(this.color))){
return true;
} else return false;
} else return false;
} else return false;
} else return false;
} else {
if ((c.getSquare(new Coord (7,0)).getOccupant() instanceof Rook)){
if (this.canCastle
&& ((Rook) c.getSquare(new Coord(7,0)).getOccupant()).canCastle()){
if (!(c.getSquare(new Coord(5,0)).isOccupied()
|| c.getSquare(new Coord(6,0)).isOccupied())){
if(!(c.getSquare(new Coord(5,0)).isAttacked(this.color)
|| c.getSquare(new Coord(6,0)).isAttacked(this.color)
|| c.getSquare(this.coords).isAttacked(this.color))){
return true;
} else return false;
} else return false;
} else return false;
} else return false;
}
} else if (coords.vectorTo(cd).equals(new Coord(-2,0))){
if (this.color == Shade.BLACK){
if ((c.getSquare(new Coord (0,7)).getOccupant() instanceof Rook)){
if (this.canCastle &&
((Rook) c.getSquare(new Coord(0,7)).getOccupant()).canCastle()){
if (!(c.getSquare(new Coord(1,7)).isOccupied() ||
c.getSquare(new Coord(2,7)).isOccupied()
|| c.getSquare(new Coord(3,7)).isOccupied())){
if(!(c.getSquare(new Coord(1,7)).isAttacked(this.color)
|| c.getSquare(new Coord(2,7)).isAttacked(this.color)
|| c.getSquare(new Coord(3,7)).isAttacked(this.color)
|| c.getSquare(this.coords).isAttacked(this.color))){
return true;
} else return false;
} else return false;
} else return false;
} else return false;
} else {
if ((c.getSquare(new Coord (0,0)).getOccupant() instanceof Rook)){
if (this.canCastle &&
((Rook) c.getSquare(new Coord(0,0)).getOccupant()).canCastle()){
if (!(c.getSquare(new Coord(1,0)).isOccupied() ||
c.getSquare(new Coord(2,0)).isOccupied()
|| c.getSquare(new Coord(3,0)).isOccupied())){
if(!(c.getSquare(new Coord(1,0)).isAttacked(this.color)
|| c.getSquare(new Coord(2,0)).isAttacked(this.color)
|| c.getSquare(new Coord(3,0)).isAttacked(this.color)
|| c.getSquare(this.coords).isAttacked(this.color))){
return true;
} else return false;
} else return false;
} else return false;
} else return false;
}
}
if (Math.abs(coords.vectorTo(cd).getFile()) > 1 ||
Math.abs(coords.vectorTo(cd).getRank()) > 1) return false;
return true;
}
public void moved(){
canCastle = false;
}
public HashSet<Coord> getAttack (){
attack.clear();
if (!(this.alive)) return attack;
if (inRange(this.coords.add(new Coord(0,1))))
attack.add(coords.add(new Coord(0,1)));
if (inRange(this.coords.add(new Coord(1,1))))
attack.add(coords.add(new Coord(1,1)));
if (inRange(this.coords.add(new Coord(1,0))))
attack.add(coords.add(new Coord(1,0)));
if (inRange(this.coords.add(new Coord(1,-1))))
attack.add(coords.add(new Coord(1,-1)));
if (inRange(this.coords.add(new Coord(0,-1))))
attack.add(coords.add(new Coord(0,-1)));
if (inRange(this.coords.add(new Coord(-1,-1))))
attack.add(coords.add(new Coord(-1,-1)));
if (inRange(this.coords.add(new Coord(-1,0))))
attack.add(coords.add(new Coord(-1,0)));
if (inRange(this.coords.add(new Coord(-1,1))))
attack.add(coords.add(new Coord(-1,1)));
return attack;
}
public boolean canCastle(){
return canCastle;
}
public String toString(){
return (this.color + " King at " + c.getSquare(coords).toString());
}
}