-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBishop.java
More file actions
52 lines (43 loc) · 1.46 KB
/
Bishop.java
File metadata and controls
52 lines (43 loc) · 1.46 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
47
48
49
50
51
52
public class Bishop extends Piece {
public Bishop(int row, int col, String color) {
super(row, col, color, "Bishop");
}
public boolean isValidMove(int[] end, Piece[][] board) {
int x = end[0];
int y = end[1];
if (super.getRow() == x && super.getColumn() == y) {
return false;
}
int delta_row = Math.abs(x - super.getRow());
int delta_col = Math.abs(y - super.getColumn());
boolean diagonal = false;
if (delta_row == delta_col) {
diagonal = true;
}
if (!diagonal) {
return false;
}
int rowMovement = 1;
if (x == super.getRow()) {
rowMovement = 0;
} else if (x < super.getRow()) {
rowMovement = -1;
}
int colMovement = 1;
if (y == super.getColumn()) {
colMovement = 0;
} else if (y < super.getColumn()) {
colMovement = -1;
}
int currentRow = super.getRow() + rowMovement;
int currentCol = super.getColumn() + colMovement;
while (currentRow != x || currentCol != y) {
if (board[currentRow][currentCol] != null) {
return false;
}
currentRow += rowMovement;
currentCol += colMovement;
}
return (board[x][y] == null || board[x][y].getColor() != getColor());
}
}