-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRook.java
126 lines (108 loc) · 3.05 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.util.HashSet;
import javax.swing.ImageIcon;
@SuppressWarnings("serial")
public class Rook extends Piece {
private boolean canCastle;
public Rook(Shade clr, int[] startCoords, Chessboard cb){
super(clr, startCoords, cb,"Rook");
canCastle = true;
if (clr == Shade.WHITE){
try{
setIcon(new ImageIcon("Images/whiteRook.png"));
setText("");
} catch (Exception e){
//Keep label as-is if image not found
}
} else {
try{
setIcon(new ImageIcon("Images/blackRook.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 (coords.vectorTo(cd).getFile() == 0){
for (int x = (Math.min(coords.getRank(), cd.getRank()) + 1);
x < Math.max(coords.getRank(), cd.getRank()); x++){
if (c.getSquare(new Coord(coords.getFile(),x)).isOccupied())
return false;
}
return true;
} else if(coords.vectorTo(cd).getRank() == 0) {
for (int x = (Math.min(coords.getFile(), cd.getFile()) + 1);
x < Math.max(coords.getFile(), cd.getFile()); x++){
if (c.getSquare(new Coord(x, coords.getRank())).isOccupied())
return false;
}
return true;
} else return false;
}
//A method to be called when the rook is actually moved, implying it can no longer be castled.
public void moved(){
canCastle = false;
}
public boolean canCastle(){
return canCastle;
}
public HashSet<Coord> getAttack (){
attack.clear();
if (!(this.alive)) return attack;
for (int i = 1; i < 7; i++){
if (!(inRange(this.coords.add(new Coord(0,i))))){
break;
} else {
if (c.getSquare(this.coords.add(new Coord(0,i))).isOccupied()) {
attack.add(this.coords.add(new Coord(0,i)));
break;
} else {
attack.add(this.coords.add(new Coord(0,i)));
}
}
}
for (int i = 1; i < 7; i++){
if (!(inRange(this.coords.add(new Coord(0,-i))))){
break;
} else {
if (c.getSquare(this.coords.add(new Coord(0,-i))).isOccupied()) {
attack.add(this.coords.add(new Coord(0,-i)));
break;
} else {
attack.add(this.coords.add(new Coord(0,-i)));
}
}
}
for (int i = 1; i < 7; i++){
if (!(inRange(this.coords.add(new Coord(i,0))))){
break;
} else {
if (c.getSquare(this.coords.add(new Coord(i,0))).isOccupied()) {
attack.add(this.coords.add(new Coord(i,0)));
break;
} else {
attack.add(this.coords.add(new Coord(i,0)));
}
}
}
for (int i = 1; i < 7; i++){
if (!(inRange(this.coords.add(new Coord(-i,0))))){
break;
} else {
if (c.getSquare(this.coords.add(new Coord(-i,0))).isOccupied()) {
attack.add(this.coords.add(new Coord(-i,0)));
break;
} else {
attack.add(this.coords.add(new Coord(-i,0)));
}
}
}
return attack;
}
public String toString(){
return (this.color + " Rook at " + c.getSquare(coords).toString());
}
//TODO: Implement squares attacked function
}