-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKnight.java
83 lines (70 loc) · 2.44 KB
/
Knight.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.HashSet;
import javax.swing.ImageIcon;
@SuppressWarnings("serial")
public class Knight extends Piece {
/* The knight's move pattern is unique (read: simple) because it can only
* move in one of eight specific pathways, and these pathways exist whether
* or not there are pieces between the knight and its destination. Thus,
* these movement vectors can be represented as a constant HashSet of Coord
* objects.
*/
private HashSet<Coord> moveSet = new HashSet<Coord>();
public Knight(Shade clr, int[] startCoords, Chessboard cb){
super(clr, startCoords, cb,"Knight");
moveSet.add(new Coord(2,-1));
moveSet.add(new Coord(2,1));
moveSet.add(new Coord(-2,-1));
moveSet.add(new Coord(-2,1));
moveSet.add(new Coord(1,-2));
moveSet.add(new Coord(1,2));
moveSet.add(new Coord(-1,-2));
moveSet.add(new Coord(-1,2));
if (clr == Shade.WHITE){
try{
setIcon(new ImageIcon("Images/whiteKnight.png"));
setText("");
} catch (Exception e){
//Keep label as-is if image not found
}
} else {
try{
setIcon(new ImageIcon("Images/blackKnight.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 (moveSet.contains(coords.vectorTo(cd)))
return true;
else return false;
}
public HashSet<Coord> getAttack (){
attack.clear();
if (!(this.alive)) return attack;
if (inRange(this.coords.add(new Coord(2,-1))))
attack.add(coords.add(new Coord(2,-1)));
if (inRange(this.coords.add(new Coord(2,1))))
attack.add(coords.add(new Coord(2,1)));
if (inRange(this.coords.add(new Coord(-2,-1))))
attack.add(coords.add(new Coord(-2,-1)));
if (inRange(this.coords.add(new Coord(-2,1))))
attack.add(coords.add(new Coord(-2,1)));
if (inRange(this.coords.add(new Coord(1,-2))))
attack.add(coords.add(new Coord(1,-2)));
if (inRange(this.coords.add(new Coord(-1,-2))))
attack.add(coords.add(new Coord(-1,-2)));
if (inRange(this.coords.add(new Coord(1,2))))
attack.add(coords.add(new Coord(1,2)));
if (inRange(this.coords.add(new Coord(-1,2))))
attack.add(coords.add(new Coord(-1,2)));
return attack;
}
public String toString(){
return (this.color + " Knight at " + c.getSquare(coords).toString());
}
//TODO: Implement squares attacked function
}