-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKing.java
35 lines (24 loc) · 1.1 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
public class King extends Piece {
public King(int row, int col, String color) {
super(row, col, color, "King");
}
public boolean isValidMove(int[] end, Piece[][] board){
String color = super.getColor();
int[] start = super.getCoords();
int rowDiff = Math.abs(start[0] - end[0]);
int colDiff = Math.abs(start[1] - end[1]);
boolean isOneSquareMove;
//checks if the king is moving only one square
//rowDiff and colDiff can be 1 or less but if they are both 0 then the king isn't moving any squares which isn't allowed
if (rowDiff <= 1 && colDiff <=1 && (rowDiff != 0 || colDiff != 0)){
isOneSquareMove = true;
} else {
isOneSquareMove = false;
}
if (!isOneSquareMove){ // kings can only move one square
return false;
}
// if the destination is empty or the opposite color it is valid
return (board[end[0]][end[1]] == null || board[end[0]][end[1]].getColor() != getColor());
}
}