-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPawn.java
More file actions
129 lines (106 loc) · 4.48 KB
/
Copy pathPawn.java
File metadata and controls
129 lines (106 loc) · 4.48 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
/**
* Pawn
*
* @author Faiza Salami, 7941056
* <p>
* REMARKS: A pawn class representing a pawn piece on a chess board
*/
public class Pawn extends Piece {
public Pawn(char piece) {
super(piece);
}
/**
* checks if the move being made is valid for a player's pawn
*
* @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;
Piece newPiece = gameBoard.getPiece(move.getNewRow(), move.getNewCol());
if (move.getRow() == move.getNewRow() + 1) {
if (newPiece == null && move.getCol() == move.getNewCol()) {
output = true;
} else if (newPiece != null && newPiece.isAIPiece() && (move.getNewCol() == move.getCol() + 1 || move.getNewCol() == move.getCol() - 1)) {
output = true;
}
}
if (output) {
gameBoard.getPiece(move.getRow(), move.getCol()).setCaptured(newPiece);
}
return output;
}
/**
* generate a move for the AI pawn, easy mode. this method just moves the pawn by one square
*
* @param move the move containing where the pawn currently is
* @param gameBoard the current game board
* @return boolean-returns the move containing what location the pawn is moving to or null if there's no available
* space for the pawn to move to
*/
public Move generateMove(Move move, Board gameBoard) {
//list of way the pawn can go diagonal
int[][] diagonal = {{1, -1}, {1, 1}};
int newRow;
int newCol;
for (int i = 0; i < diagonal.length; i++) {
newRow = move.getRow() + diagonal[i][0]; //get the new row the pawn is moving to
newCol = move.getCol() + diagonal[i][1]; //get the new col the pawn is moving to
//check if its within bounds of the board
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
if (gameBoard.getPiece(newRow, newCol) != null && gameBoard.getPiece(newRow, newCol).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();
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
if (gameBoard.getPiece(newRow, newCol) == null) {
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 pawn's vicinity to be captured
*
* @param move the move containing the pawn's current location
* @param gameBoard the current game board
* @return Move-the move containing what location the pawn is moving to or null if the pawn cant capture any player
*/
public Move capturePlayerPiece(Move move, Board gameBoard) {
int[][] diagonal = {{1, -1}, {1, 1}}; //the only possible moves are the ones that can capture a humans piece diagonally
int newRow;
int newCol;
for (int i = 0; i < diagonal.length; i++) {
newRow = move.getRow() + diagonal[i][0]; //get the new row the pawn is moving to
newCol = move.getCol() + diagonal[i][1]; //get the new col the pawn is moving to
//check if its within bounds of the board
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
if (gameBoard.getPiece(newRow, newCol) != null && gameBoard.getPiece(newRow, newCol).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 "Pawn";
}
}