-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKing.java
More file actions
118 lines (97 loc) · 3.99 KB
/
Copy pathKing.java
File metadata and controls
118 lines (97 loc) · 3.99 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
/**
* King
*
* @author Faiza Salami, 7941056
* <p>
* REMARKS: A king class representing a king piece on a chess board
*/
public class King extends Piece {
public King(char piece) {
super(piece);
}
/**
* checks if the move being made is valid for a player's king
*
* @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) {
Piece newPiece = gameBoard.getPiece(move.getNewRow(), move.getNewCol());
boolean output = false;
if ((move.getRow() == move.getNewRow()) && Math.abs(move.getNewCol() - move.getCol()) == 1) {
output = (newPiece == null || newPiece.isAIPiece());
} else if ((move.getCol() == move.getNewCol()) && Math.abs(move.getNewRow() - move.getRow()) == 1) {
output = (newPiece == null || newPiece.isAIPiece());
} else if (Math.abs(move.getNewCol() - move.getCol()) == Math.abs(move.getNewRow() - move.getRow())) {
output = (newPiece == null || newPiece.isAIPiece());
}
if (output) {
gameBoard.getPiece(move.getRow(), move.getCol()).setCaptured(newPiece);
}
return output;
}
/**
* generate a move for the AI king, easy mode. this method just moves the king by one square
*
* @param move the move containing where the king currently is
* @param gameBoard the current game board
* @return boolean-returns the move containing what location the king is moving to or null if there's no available
* space for the king 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 king's vicinity to be captured
*
* @param move the move containing the king's current location
* @param gameBoard the current game board
* @return Move-the move containing what location the king is moving to or null if the king cant capture any player
*/
public Move capturePlayerPiece(Move move, Board gameBoard) {
int[][] possibleMoves = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {-1, 1}, {-1, -1}, {1, 1}, {1, -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;
}
/**
* the name of the piece
*
* @return returns the piece name
*/
public String name() {
return "King";
}
}