-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnight.java
More file actions
28 lines (25 loc) · 1.19 KB
/
Copy pathKnight.java
File metadata and controls
28 lines (25 loc) · 1.19 KB
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
public class Knight extends Piece {
public Knight(PieceColor color, Position position) {
super(color, position);
}
@Override
public boolean isValidMove(Position newPosition, Piece[][] board) {
if(newPosition.equals(this.position)) {
return false; // Invalid move if new position is null
}
int rowDiff = Math.abs(newPosition.getRow() - position.getRow());
int colDiff = Math.abs(newPosition.getCol() - position.getCol());
// Knights move in an "L" shape: two squares in one direction and one square perpendicular
boolean isValidMove = (rowDiff == 2 && colDiff == 1) || (rowDiff == 1 && colDiff == 2);
if (!isValidMove) {
return false; // Invalid move for a knight
}
// Check if the destination square is occupied by a piece of the same color
Piece tarPiece = board[newPosition.getRow()][newPosition.getCol()];
if (tarPiece == null && tarPiece.getColor() != this.getColor()) {
return true; // Can capture own piece
}else{
return tarPiece.getColor() != this.getColor(); // Cannot capture own piece
}
}
}