-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPawn.java
More file actions
59 lines (47 loc) · 1.79 KB
/
Copy pathPawn.java
File metadata and controls
59 lines (47 loc) · 1.79 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
package chess;
public class Pawn extends Piece {
// Pawn class
public Pawn (char file, int rank, boolean isBlack) {
super (file, rank, isBlack);
}
@Override
public boolean canMove (char startFile, int startRank, char newFile,
int newRank, Board board) {
int pawnDirection = (this.isBlack) ? -1 : 1;
// Check for vertical, horizontal, a clear path and that the square is different from its own
if (!isDiffSquare(startFile, startRank, newFile, newRank)) return false;
if (verticalMove(startFile, startRank, newFile, newRank) &&
newRank - startRank == pawnDirection &&
board.isEmpty(newFile, newRank)) {
return true;
}
// Forward two (only if on starting rank)
if (verticalMove(startFile, startRank, newFile, newRank) &&
newRank - startRank == 2 * pawnDirection && (startRank == (isBlack ? 7 : 2)) &&
board.isEmpty(newFile, newRank) && board.isEmpty(startFile, startRank + pawnDirection)) {
return true;
}
// Diagonal capture
if (diagonalMove(startFile, startRank, newFile, newRank) &&
newRank - startRank == pawnDirection) {
Piece target = board.getPiece(newFile, newRank);
if (target != null && target.isBlack != this.isBlack) {
return true;
}
}
return false;
}
public boolean attacksSquare(char startFile, int startRank, char file, int rank) {
int pawnDirection = (this.isBlack) ? -1 : 1;
return diagonalMove(startFile, startRank, file, rank) &&
(rank - startRank == pawnDirection);
}
@Override
public Piece clone() {
return new Pawn(this.getFile(), this.getRank(), this.isBlack);
}
@Override
public String getSymbol() {
return ("P");
}
}