-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBishop.java
More file actions
187 lines (152 loc) · 6.51 KB
/
Copy pathBishop.java
File metadata and controls
187 lines (152 loc) · 6.51 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
180
181
182
183
184
185
186
187
/**
* Bishop
*
* @author Faiza Salami, 7941056
* <p>
* REMARKS: A bishop class representing a bishop piece on a chess board
*/
public class Bishop extends Piece {
public Bishop(char piece) {
super(piece);
}
/**
* checks if the move being made is valid for a player's bishop
*
* @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; //if there is any piece in between its starting point to its location
Piece newPiece = gameBoard.getPiece(move.getNewRow(), move.getNewCol());
if (Math.abs(move.getNewCol() - move.getCol()) == Math.abs(move.getNewRow() - move.getRow())) {
for (int i = 1; i <= Math.abs(move.getNewCol() - move.getCol()) - 1; i++) {
if (gameBoard.getPiece(Math.min(move.getRow(), move.getNewRow()) + i, Math.max(move.getCol(), move.getNewCol()) - i) != null) {
noPiecesBetween = false;
}
}
} else {
noPiecesBetween = false;
}
if (noPiecesBetween && (newPiece == null || newPiece.isAIPiece())) {
gameBoard.getPiece(move.getRow(), move.getCol()).setCaptured(newPiece);
output = true;
}
return output;
}
/**
* the name of the piece
*
* @return returns the piece name
*/
public String name() {
return "Bishop";
}
/**
* generate a move for the AI bishop, easy mode. this method just moves the bishop by one square
*
* @param move the move containing where the bishop currently is
* @param gameBoard the current game board
* @return boolean-returns the move containing what location the bishop is moving to or null if there's no available space for the
* bishop to move to
*/
public Move generateMove(Move move, Board gameBoard) {
int[][] possibleMoves = {{-1, 1}, {-1, -1}, {1, 1}, {1, -1}};
int newRow;
int newCol;
Piece newPiece;
//if any of the possible move is a valid move, that will be the AI's move
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 bishop's vicinity to be captured
*
* @param move the move containing the bishop's current location
* @param gameBoard the current game board
* @return Move-the move containing what location the bishop is moving to or null if the bishop cant capture any player
*/
public Move capturePlayerPiece(Move move, Board gameBoard) {
int newRow = move.getRow() + 1;
int newCol = move.getCol() + 1;
Piece newPiece = null;
//keep moving the bishop until it meets another piece
while (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8 && gameBoard.getPiece(newRow, newCol) == null) {
newPiece = gameBoard.getPiece(newRow, newCol);
newRow++;
newCol++;
}
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
newPiece = gameBoard.getPiece(newRow, newCol);
}
//if the new piece the bishop meets is a players piece, capture that player's piece
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() - 1;
while (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8 && gameBoard.getPiece(newRow, newCol) == null) {
newPiece = gameBoard.getPiece(newRow, newCol);
newRow++;
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() + 1;
while (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8 && gameBoard.getPiece(newRow, newCol) == null) {
newPiece = gameBoard.getPiece(newRow, newCol);
newRow--;
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() - 1;
while (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8 && gameBoard.getPiece(newRow, newCol) == null) {
newPiece = gameBoard.getPiece(newRow, newCol);
newRow--;
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;
}
return null;
}
}