-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathKnight.java
More file actions
37 lines (28 loc) · 1009 Bytes
/
Knight.java
File metadata and controls
37 lines (28 loc) · 1009 Bytes
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
import java.util.LinkedList;
import java.util.List;
public class Knight extends Piece {
public Knight(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 = 2; i > -3; i--) {
for (int k = 2; k > -3; k--) {
if(Math.abs(i) == 2 ^ Math.abs(k) == 2) {
if (k != 0 && i != 0) {
try {
legalMoves.add(board[y + k][x + i]);
} catch (ArrayIndexOutOfBoundsException e) {
continue;
}
}
}
}
}
return legalMoves;
}
}