-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKing.java
More file actions
20 lines (17 loc) · 776 Bytes
/
Copy pathKing.java
File metadata and controls
20 lines (17 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class King extends Piece {
public King(PieceColor color, Position position) {
super(color, position);
}
@Override
public boolean isValidMove(Position newPosition, Piece[][] board) {
int rowDiff = Math.abs(position.getRow() - newPosition.getRow());
int colDiff = Math.abs(position.getCol() - newPosition.getCol());
// The King moves one square in any direction.
boolean isOneSquareMove = rowDiff <= 1 && colDiff <= 1 && !(rowDiff == 0 && colDiff == 0);
if (!isOneSquareMove) {
return false; // Not a one-square move.
}
Piece dest = board[newPosition.getRow()][newPosition.getCol()];
return (dest == null || dest.getColor() != this.color);
}
}