-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBishop.java
More file actions
58 lines (53 loc) · 1.71 KB
/
Copy pathBishop.java
File metadata and controls
58 lines (53 loc) · 1.71 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
54
55
56
57
58
/**
* Bishop.java
*
* @author Deep Patel
*
* REMARKS: This is a class which stores information about the bishop
* piece and how it moves, which is diagonally and cannot jump over
* pieces
*/
public class Bishop extends Piece {
//Constructor
public Bishop(boolean myPiece) {
super(myPiece);
}
/**
* This method is used to check if the move made by the bishop piece is a valid
* move for the piece
*
* Parameters:
* Board board - The method take in the current state of the board to validate the move
* Move move - The method takes in the move being made
*
* Returns a boolean to indicate whether the move was valid
*/
public boolean isValidMove(Board board, Move move) {
// Bishop must move diagonally
if (Math.abs(move.getToRow() - move.getFromRow()) != Math.abs(move.getToCol() - move.getFromCol())) {
return false;
}
//Row direction where bishop is moving
int rowDir;
int difference = move.getToRow() - move.getFromRow();
if (difference > 0) {
rowDir = 1;
} else {
rowDir = -1;
}
//Column direction where bishop is moving
int colDir;
int diff = move.getToCol() - move.getFromCol();
if (diff > 0) {
colDir = 1;
} else {
colDir = -1;
}
for (int i = 1; i < Math.abs(move.getToRow() - move.getFromRow()); i++) {
if (board.getSymbol(move.getFromRow() - 1 + i * rowDir, move.getFromCol() - 1 + i * colDir) != ' ') {
return false; // Path must be clear for bishop to move
}
}
return true;
}
}