-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnight.java
More file actions
25 lines (21 loc) · 802 Bytes
/
Knight.java
File metadata and controls
25 lines (21 loc) · 802 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
public class Knight extends Piece {
public Knight(String color, int row, int col) {
super(color, row, col);
}
@Override
public boolean isValidMove(int destRow, int destCol, Piece[][] board) {
int rowDiff = Math.abs(destRow - getRow());
int colDiff = Math.abs(destCol - getCol());
// Knight moves in an "L" shape: 2 squares in one direction, 1 square perpendicular
return ((rowDiff == 2 && colDiff == 1) || (rowDiff == 1 && colDiff == 2)) &&
!isAllyPiece(destRow, destCol, board);
}
@Override
public String getImagePath() {
return getClass().getResource(getColor() + "Knight.png").toExternalForm();
}
@Override
public String getType() {
return "Knight";
}
}