-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRook.java
More file actions
50 lines (48 loc) · 1.68 KB
/
Copy pathRook.java
File metadata and controls
50 lines (48 loc) · 1.68 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
/**
* Rook.java
*
* @author Deep Patel
*
* REMARKS: This is a class which stores information about the rook
* piece and how it moves, which is in a straight line horizontally or
* vertically
*/
public class Rook extends Piece{
//Constructor
public Rook(boolean myPiece) {
super(myPiece);
}
/**
* This method is used to check if the move made by the rook 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) {
if (move.getFromRow() == move.getToRow()) { // Moving horizontally
int minCol = Math.min(move.getFromCol(), move.getToCol());
int maxCol = Math.max(move.getFromCol(), move.getToCol());
for (int i = minCol + 1; i < maxCol; i++) {
if (board.getSymbol(move.getFromRow() - 1, i - 1) != ' ') {
return false;
}
}
return true;
} else if (move.getFromCol() == move.getToCol()) { // Moving vertically
int minRow = Math.min(move.getFromRow(), move.getToRow());
int maxRow = Math.max(move.getFromRow(), move.getToRow());
for (int i = minRow + 1; i < maxRow; i++) {
if (board.getSymbol(i-1,move.getFromCol()-1) != ' ') {
return false;
}
}
return true;
} else {
return false; // Rook can only move horizontally or vertically
}
}
}