-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathKing.java
More file actions
40 lines (30 loc) · 1.11 KB
/
King.java
File metadata and controls
40 lines (30 loc) · 1.11 KB
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
import java.util.LinkedList;
import java.util.List;
public class King extends Piece {
public King(int color, Square initSq, String img_file) {
super(color, initSq, img_file);
}
@Override
public List<Square> getLegalMoves(Board b) {
LinkedList<Square> legalMoves = new LinkedList<Square>();
Square[][] board = b.getSquareArray();
int x = this.getPosition().getXNum();
int y = this.getPosition().getYNum();
for (int i = 1; i > -2; i--) {
for (int k = 1; k > -2; k--) {
if(!(i == 0 && k == 0)) {
try {
if(!board[y + k][x + i].isOccupied() ||
board[y + k][x + i].getOccupyingPiece().getColor()
!= this.getColor()) {
legalMoves.add(board[y + k][x + i]);
}
} catch (ArrayIndexOutOfBoundsException e) {
continue;
}
}
}
}
return legalMoves;
}
}