-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBishop.java
More file actions
46 lines (38 loc) · 1.38 KB
/
Bishop.java
File metadata and controls
46 lines (38 loc) · 1.38 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
39
40
41
42
43
44
45
46
public class Bishop extends Piece {
public Bishop(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());
// Bishop moves diagonally
if (rowDiff == colDiff && !isAllyPiece(destRow, destCol, board)) {
return isPathClearDiagonal(destRow, destCol, board);
}
return false;
}
// Checking if the diagonal path is clear
private boolean isPathClearDiagonal(int destRow, int destCol, Piece[][] board) {
int rowStep = Integer.compare(destRow, getRow());
int colStep = Integer.compare(destCol, getCol());
int currentRow = getRow() + rowStep;
int currentCol = getCol() + colStep;
while (currentRow != destRow && currentCol != destCol) {
if (board[currentRow][currentCol] != null) {
return false;
}
currentRow += rowStep;
currentCol += colStep;
}
return true;
}
@Override
public String getImagePath() {
return getClass().getResource(getColor() + "Bishop.png").toExternalForm();
}
@Override
public String getType() {
return "Bishop";
}
}