-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNight.java
More file actions
38 lines (31 loc) · 1.37 KB
/
Night.java
File metadata and controls
38 lines (31 loc) · 1.37 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
29
30
31
32
33
34
35
36
37
38
public class Night extends Piece {
public Night(String position, boolean isWhite) {
super(position, isWhite);
}
@Override
public String isValidMove(String newPosition, Piece[][] board) {
// Convert positions like "A2" to board indices
int currentRow = Character.getNumericValue(position.charAt(1)) - 1;
int currentCol = Character.toUpperCase(position.charAt(0)) - 'A';
int newRow = Character.getNumericValue(newPosition.charAt(1)) - 1;
int newCol = Character.toUpperCase(newPosition.charAt(0)) - 'A';
// Ensure within board limits
if (newRow < 0 || newRow > 7 || newCol < 0 || newCol > 7) {
return "Out Of Bounds";
}
// Prevent moving to the same position
if (newPosition.equals(position)) {
return "Can't move to the same position";
}
// Check if the move is an L-shape
int rowDiff = Math.abs(newRow - currentRow);
int colDiff = Math.abs(newCol - currentCol);
if ((rowDiff == 2 && colDiff == 1) || (rowDiff == 1 && colDiff == 2)) {
// Allow move if destination is empty or contains an opponent's piece
if (board[newRow][newCol] == null || board[newRow][newCol].isWhite() != isWhite) {
return "Valid Move";
}
}
return "Invalid Move";
}
}