-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBishop.java
110 lines (98 loc) · 3.03 KB
/
Bishop.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
import java.util.HashSet;
import javax.swing.ImageIcon;
@SuppressWarnings("serial")
public class Bishop extends Piece {
public Bishop(Shade clr, int[] startCoords, Chessboard cb){
super(clr, startCoords, cb,"Bishop");
if (clr == Shade.WHITE){
try{
setIcon(new ImageIcon("Images/whiteBishop.png"));
setText("");
} catch (Exception e){
//Keep label as-is if image not found
}
} else {
try{
setIcon(new ImageIcon("Images/blackBishop.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() == coords.vectorTo(cd).getRank()){
int maxRank = Math.max(coords.getRank(), cd.getRank());
int minRank = Math.min(coords.getRank(), cd.getRank());
int minFile = Math.min(coords.getFile(), cd.getFile());
for(int x = 1; x < maxRank - minRank; x++)
if (c.getSquare(new Coord((minFile + x),(minRank + x))).isOccupied()) return false;
return true;
} else if (coords.vectorTo(cd).getFile() == (coords.vectorTo(cd).getRank() * -1)){
int maxFile = Math.max(coords.getFile(), cd.getFile());
int minFile = Math.min(coords.getFile(), cd.getFile());
int maxRank = Math.max(coords.getRank(), cd.getRank());
for(int x = 1; x < maxFile - minFile; x++)
if (c.getSquare(new Coord((minFile + x),(maxRank - x))).isOccupied()) return false;
return true;
} else return false;
}
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(i,i))))){
break;
} else {
if (c.getSquare(this.coords.add(new Coord(i,i))).isOccupied()) {
attack.add(this.coords.add(new Coord(i,i)));
break;
} else {
attack.add(this.coords.add(new Coord(i,i)));
}
}
}
for (int i = 1; i < 7; i++){
if (!(inRange(this.coords.add(new Coord(-i,i))))){
break;
} else {
if (c.getSquare(this.coords.add(new Coord(-i,i))).isOccupied()) {
attack.add(this.coords.add(new Coord(-i,i)));
break;
} else {
attack.add(this.coords.add(new Coord(-i,i)));
}
}
}
for (int i = 1; i < 7; i++){
if (!(inRange(this.coords.add(new Coord(i,-i))))){
break;
} else {
if (c.getSquare(this.coords.add(new Coord(i,-i))).isOccupied()) {
attack.add(this.coords.add(new Coord(i,-i)));
break;
} else {
attack.add(this.coords.add(new Coord(i,-i)));
}
}
}
for (int i = 1; i < 7; i++){
if (!(inRange(this.coords.add(new Coord(-i,-i))))){
break;
} else {
if (c.getSquare(this.coords.add(new Coord(-i,-i))).isOccupied()) {
attack.add(this.coords.add(new Coord(-i,-i)));
break;
} else {
attack.add(this.coords.add(new Coord(-i,-i)));
}
}
}
return attack;
}
public String toString(){
return (this.color + " Bishop at " + c.getSquare(coords).toString());
}
}