-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRook.java
More file actions
67 lines (51 loc) · 2.63 KB
/
Copy pathRook.java
File metadata and controls
67 lines (51 loc) · 2.63 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
59
60
61
62
63
64
65
66
67
import java.util.ArrayList;
public class Rook extends Piece {
//Implemented getPossibleMoves function
@Override
public ArrayList<String> getPossibleMoves(String originalcoord, boolean isWhitePlaying) {
possibleMoves.clear();
//Extracts row and column values from coordinates of piece position
int row = Character.getNumericValue(originalcoord.charAt(1));
int col = Character.getNumericValue(originalcoord.charAt(0));
boolean enemyPiecePresent = false;
int testRow = row;
int testCol = col;
//Iterates through 4 possible lines of movement of bishop piece - diagonally upwards to the right and left and diagonally downwards to right and left
//Value of i determines which line of movement is being scanned
for(int i = 0; i<4; i++) {
//Iterates through line of movement until piece is detected
while (!enemyPiecePresent) {
//Different direction vectors for line of movement
if (i == 0) {
testRow += 1;
} else if (i == 1) {
testRow -= 1;
} else if (i == 2) {
testCol += 1;
} else {
testCol -= 1;
}
//Checks if row and column of square being checked is within GridPane, as coordinates returned are in GridPane format
if (testRow > 0 && testRow < 9 && testCol > -1 && testCol < 8) {
//Adds coordinates of square as possible move (provided addIfValid base validation is passed)
addIfValid(testRow, testCol, isWhitePlaying);
//If square is not empty and contains a piece
if (!(logic_board.logicBoard[testRow - 1][testCol].equals(""))) {
//Set true to break out of while loop and onto next iteration of for loop to scan next line of movement
enemyPiecePresent = true;
}
}
//If row and column not within GridPane, sets enemyPiecePresent true to break out of while loop and move to next for loop iteration to scan next line of movement
else {
enemyPiecePresent = true;
}
}
//Resets column and row values to original column and row values to scan next line of movement, and resets boolean
testCol = col;
testRow = row;
enemyPiecePresent = false;
}
//Returns List of moves (coordinates of squares piece can move to)
return possibleMoves;
}
}