-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueen.java
More file actions
53 lines (45 loc) · 2.01 KB
/
Queen.java
File metadata and controls
53 lines (45 loc) · 2.01 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
53
public class Queen extends Piece {
public Queen(int row, int col, String color) {
super(row, col, color, "Queen"); // sets the position and color of the queen
}
public boolean isValidMove(int[] end, Piece[][] board) {
int x = end[0];
int y = end[1];
int delta_row = Math.abs(x - super.getRow()); // gets the difference between the destination position and the current
int delta_col = Math.abs(y - super.getColumn());
if (super.getRow() == x && super.getColumn() == y) {
return false;
}// checks for the best case scenario where the destination is the same as the current
boolean line = (super.getRow() == x) || (super.getColumn() == y); // sees if the destination is in the same row or column as the current in order to count it as a valid line
boolean diagonal = false; // now checks to see if there is a possible diagonal move
// if the change in y and change in x are the same, that means that the diagonal is possible since
if (delta_row == delta_col) {
diagonal = true;
}
if (!line && !diagonal) { // if the final destination is not a line or a diagonal, then the move is not valid
return false;
}
int rowMovement = 1;
if (x == super.getRow()) { // basically we are going to simulate moving on the board
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());
}
}