-
Notifications
You must be signed in to change notification settings - Fork 0
/
King.java
99 lines (84 loc) · 3.3 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
import java.awt.Graphics2D;
import java.awt.Point;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.util.ArrayList;
public class King extends Piece {
private final static String[] IMGS = {"images/whites/king.png", "images/blacks/king.png"};
private final static int PIECE_VALUE = 1000000;
private boolean hasMoved = false; //if the piece has moved since the start of the game
public King(Board board, Player player, int xTile, int yTile) {
super(board, player, xTile, yTile);
}
public void setPosition(Point tile) { //overrides piece method to include a hasMoved status
super.setPosition(tile);
hasMoved = true;
}
//returns all the possible locations the piece can move to.
public ArrayList<Point> validTiles() { //TODO: check if king is in check
ArrayList<Point> valids = new ArrayList<Point>();
Point p = new Point(getTile().x + 1, getTile().y);
if(isValid(p)) valids.add(p);
p = new Point(getTile().x + 1, getTile().y + 1);
if(isValid(p)) valids.add(p);
p = new Point(getTile().x, getTile().y + 1);
if(isValid(p)) valids.add(p);
p = new Point(getTile().x - 1, getTile().y + 1);
if(isValid(p)) valids.add(p);
p = new Point(getTile().x - 1, getTile().y);
if(isValid(p)) valids.add(p);
p = new Point(getTile().x - 1, getTile().y - 1);
if(isValid(p)) valids.add(p);
p = new Point(getTile().x, getTile().y - 1);
if(isValid(p)) valids.add(p);
p = new Point(getTile().x + 1, getTile().y - 1);
if(isValid(p)) valids.add(p);
valids.addAll(validCastles());
return valids;
}
public ArrayList<Point> validCastles() { //returns ArrayList of tiles to castle to
ArrayList<Point> valids = new ArrayList<Point>();
if(hasMoved == false) { //castling support
Piece rook = pieceAt(new Point(0, getTile().y));
if(rook instanceof Rook && ((Rook) rook).hasMoved() == false) {
if(isValid(new Point(getTile().x - 1, getTile().y)) &&
isValid(new Point(getTile().x - 2, getTile().y)) &&
isValid(new Point(getTile().x - 3, getTile().y))) {
valids.add(new Point(getTile().x - 2, getTile().y));
}
if(isValid(new Point(getTile().x + 1, getTile().y)) &&
isValid(new Point(getTile().x + 2, getTile().y))) {
valids.add(new Point(getTile().x + 2, getTile().y));
}
}
}
return valids;
}
private boolean isValid(Point p) {
return (onBoard(p) && pieceAt(p) == null);
}
public boolean isInCheck() { //returns true if the king is in check, false otherwise
for(Piece p : getOpponent().getAlives()) {
for(Point v : p.validTiles()) {
if(v.equals(getTile())) return true;
}
}
return false;
}
//set the image to the appropriate file import
public void setImg() {
try{
super.fullSizeImg = ImageIO.read(new File(IMGS[getTeam()]));
} catch(IOException e){
//TODO: is this really what i want in here?
e.printStackTrace();
}
}
public Piece copy() { //returns a copy of this piece
return new King(getBoard(), getPlayer(), getTile().x, getTile().y);
}
public int getValue() {
return PIECE_VALUE;
}
}