-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRook.java
More file actions
179 lines (151 loc) · 6.3 KB
/
Copy pathRook.java
File metadata and controls
179 lines (151 loc) · 6.3 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* Rook
*
* @author Faiza Salami, 7941056
* <p>
* REMARKS: A rook class representing a rook piece on a chess board
*/
public class Rook extends Piece {
public Rook(char piece) {
super(piece);
}
/**
* checks if the move being made is valid for a player's rook
*
* @param move the move that was made
* @param gameBoard the current game board
* @return boolean-returns true if the move is valid and false otherwise
*/
public boolean validMove(Move move, Board gameBoard) {
boolean output = false;
boolean noPiecesBetween = true;
Piece newPiece = gameBoard.getPiece(move.getNewRow(), move.getNewCol());
if (move.getRow() == move.getNewRow()) {
for (int i = 1; i <= Math.abs(move.getNewCol() - move.getCol()) - 1; i++) {
if (gameBoard.getPiece(move.getNewRow(), Math.min(move.getCol(), move.getNewCol()) + i) != null) {
noPiecesBetween = false;
}
}
} else if (move.getCol() == move.getNewCol()) {
for (int i = 1; i <= Math.abs(move.getNewRow() - move.getRow()) - 1; i++) {
if (gameBoard.getPiece(Math.min(move.getRow(), move.getNewRow()) + i, move.getNewCol()) != null) {
noPiecesBetween = false;
}
}
} else {
noPiecesBetween = false;
}
if (noPiecesBetween && (newPiece == null || newPiece.isAIPiece())) {
gameBoard.getPiece(move.getRow(), move.getCol()).setCaptured(newPiece);
output = true;
}
return output;
}
/**
* generate a move for the AI rook, easy mode. this method just moves the rook by one square
*
* @param move the move containing where the rook currently is
* @param gameBoard the current game board
* @return boolean-returns the move containing what location the rook is moving to or null if there's no available
* space for the rook to move to
*/
public Move generateMove(Move move, Board gameBoard) {
int[][] possibleMoves = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int newRow;
int newCol;
Piece newPiece;
for (int i = 0; i < possibleMoves.length; i++) {
newRow = move.getRow() + possibleMoves[i][0];
newCol = move.getCol() + possibleMoves[i][1];
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
newPiece = gameBoard.getPiece(newRow, newCol);
if (newPiece == null || newPiece.isPlayerPiece()) {
move.setNewRow(newRow);
move.setNewCol(newCol);
gameBoard.getPiece(move.getRow(), move.getCol()).setCaptured(gameBoard.getPiece(move.getNewRow(), move.getNewCol()));
return move;
}
}
}
return null;
}
/**
* checks if any of the player's piece is in the rook's vicinity to be captured
*
* @param move the move containing the rook's current location
* @param gameBoard the current game board
* @return Move-the move containing what location the rook is moving to or null if the rook cant capture any player
*/
public Move capturePlayerPiece(Move move, Board gameBoard) {
int newRow = move.getRow() + 1;
int newCol = move.getCol();
Piece newPiece = null;
while (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8 && gameBoard.getPiece(newRow, newCol) == null) {
newPiece = gameBoard.getPiece(newRow, newCol);
newRow++;
}
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
newPiece = gameBoard.getPiece(newRow, newCol);
}
if (newPiece != null && newPiece.isPlayerPiece()) {
move.setNewRow(newRow);
move.setNewCol(newCol);
gameBoard.getPiece(move.getRow(), move.getCol()).setCaptured(gameBoard.getPiece(move.getNewRow(), move.getNewCol()));
return move;
}
newRow = move.getRow();
newCol = move.getCol() + 1;
while (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8 && gameBoard.getPiece(newRow, newCol) == null) {
newPiece = gameBoard.getPiece(newRow, newCol);
newCol++;
}
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
newPiece = gameBoard.getPiece(newRow, newCol);
}
if (newPiece != null && newPiece.isPlayerPiece()) {
move.setNewRow(newRow);
move.setNewCol(newCol);
gameBoard.getPiece(move.getRow(), move.getCol()).setCaptured(gameBoard.getPiece(move.getNewRow(), move.getNewCol()));
return move;
}
newRow = move.getRow();
newCol = move.getCol() - 1;
while (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8 && gameBoard.getPiece(newRow, newCol) == null) {
newPiece = gameBoard.getPiece(newRow, newCol);
newCol--;
}
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
newPiece = gameBoard.getPiece(newRow, newCol);
}
if (newPiece != null && newPiece.isPlayerPiece()) {
move.setNewRow(newRow);
move.setNewCol(newCol);
gameBoard.getPiece(move.getRow(), move.getCol()).setCaptured(gameBoard.getPiece(move.getNewRow(), move.getNewCol()));
return move;
}
newRow = move.getRow() - 1;
newCol = move.getCol();
while (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8 && gameBoard.getPiece(newRow, newCol) == null) {
newPiece = gameBoard.getPiece(newRow, newCol);
newRow--;
}
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
newPiece = gameBoard.getPiece(newRow, newCol);
}
if (newPiece != null && newPiece.isPlayerPiece()) {
move.setNewRow(newRow);
move.setNewCol(newCol);
gameBoard.getPiece(move.getRow(), move.getCol()).setCaptured(gameBoard.getPiece(move.getNewRow(), move.getNewCol()));
return move;
}
return null;
}
/**
* the name of the piece
*
* @return returns the piece name
*/
public String name() {
return "Rook";
}
}