-
Notifications
You must be signed in to change notification settings - Fork 0
/
King.java
40 lines (32 loc) · 1.03 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
package com.company;
import com.company.Board;
import java.util.LinkedList;
import java.util.List;
public class King extends Piece {
public King(Square currentSquare){
super(currentSquare);
}
@Override
public String toString(){
return "King";
}
@Override
public LinkedList<Square> getPossibleMoves(Board b){
LinkedList<Square> possibleMoves = new LinkedList<Square>();
Square[][] board = b.getSquareArray(); // get the board array
int x = this.getPosition().getXNum(); // get x position of king
int y = this.getPosition().getYNum(); // get y position of king
for (int i = -1; i <= 1; i++){
for (int j = -1; j <= 1; j++){
if(!(i == 0 && j == 0)) {
try {
possibleMoves.add(board[x + i][y + j]);
} catch (ArrayIndexOutOfBoundsException e){
continue;
}
}
}
}
return possibleMoves;
}
}