-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRook.java
More file actions
108 lines (87 loc) · 4.09 KB
/
Rook.java
File metadata and controls
108 lines (87 loc) · 4.09 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
public class Rook extends ChessPiece{
public Rook(String type, String color) {
super(type, color, 5);
}
public boolean isMoveValidWithoutChangingState(int startRow, int startCol, int endRow, int endCol, ChessPiece[][] board){
boolean isVerticalMove = startCol == endCol;
boolean isHorizontalMove = startRow == endRow;
if (!isVerticalMove && !isHorizontalMove) {
return false; // Move is neither vertical nor horizontal
}
// Determine the direction of the move and set up for iteration
int rowDirection = Integer.compare(endRow, startRow); // 0 if no change, -1 for up, 1 for down
int colDirection = Integer.compare(endCol, startCol); // 0 if no change, -1 for left, 1 for right
// Check for obstacles along the path
int currentRow = startRow + rowDirection;
int currentCol = startCol + colDirection;
while (currentRow != endRow || currentCol != endCol) {
if (board[currentRow][currentCol] != null) {
return false; // Path is blocked
}
currentRow += rowDirection;
currentCol += colDirection;
}
// At the destination square
ChessPiece destinationPiece = board[endRow][endCol];
if (destinationPiece != null) {
ChessPiece movingPiece = board[startRow][startCol];
if (destinationPiece.getColor().equals(movingPiece.getColor())) {
return false; // Cannot capture your own piece
} else {
// Capturing an opponent's piece
System.out.println("Rook: Enemy " + destinationPiece.getType(destinationPiece) + " captured");
return true;
}
}
return true; // The move is valid if it's clear or captures an opponent'
}
@Override
public boolean isValidMove(int startRow, int startCol, int endRow, int endCol, ChessPiece[][] board) {
String directionMove = "";
ChessPiece rook = board[startRow][startCol];
// Check for normal move
if (startCol == endCol) {
int direction = (startRow < endRow) ? 1 : -1; // Determines direction based on the target column
int tempInt = startRow + direction;
//System.out.println(tempInt);
for (int i = startRow + direction; i != endRow; i += direction) {
if (board[i][startCol] != null) {
return false; // There is a piece blocking the path
}
}
if (board[endRow][endCol] != null && !board[endRow][endCol].getColor().equals(rook.color)) {
System.out.println("Enemy " + board[endRow][endCol].getType(board[endRow][endCol]) + " captured");
return true; // Capturing vertically
}
//return false;
}
if (startRow == endRow) {
int direction = (startCol < endCol) ? 1 : -1; // Determines direction based on the target column
for (int i = startCol + direction; i != endCol; i += direction) {
if (board[startRow][i] != null) {
return false; // There is a piece blocking the path
}
}
if (board[endRow][endCol] != null){
if (!board[endRow][endCol].getColor().equals(rook.color)) {
System.out.println("Rook: Enemy " + board[endRow][endCol].getType(board[endRow][endCol]) + " captured");
return true;
}
else {
//System.out.println("cant eat your own color");
return false;
}
}
//return false; // Horizontal move is valid
}
else if (startRow != endRow && startCol != endCol){
return false;
}
else if (board[endRow][endCol] != null && board[endRow][endCol].getColor().equals(rook.color)){
return false;
}
// Check for capture move
return true; // If none of the above conditions are met, it's an invalid move
//return false;
}
}