-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKing.java
More file actions
86 lines (60 loc) · 2.34 KB
/
Copy pathKing.java
File metadata and controls
86 lines (60 loc) · 2.34 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
package chess;
public class King extends Piece {
// King class
public boolean kingHasMoved = false;
public King (char file, int rank, boolean isBlack) {
super (file, rank, isBlack);
}
@Override
public boolean canMove (char startFile, int startRank, char newFile,
int newRank, Board board) {
if (!this.kingHasMoved && startRank == newRank) {
// Check castling first, then classic moves
if (newFile == (char)(startFile + 2)) {
Piece rook = board.getPiece('h', startRank);
if (rook instanceof Rook && !((Rook) rook).checkIfMoved()
&& board.pathClear(startFile, startRank, (char)('h' - 1), startRank)
&& !Chess.isSquareUnderAttack((char)(startFile + 1), startRank, this.isBlack, board)
&& !Chess.isSquareUnderAttack(newFile, newRank, this.isBlack, board) &&
!Chess.isSquareUnderAttack(startFile, startRank, this.isBlack, board)){
return true;
}
}
if (newFile == (char)(startFile - 2)) {
Piece rook = board.getPiece('a', startRank);
if (rook instanceof Rook && !((Rook) rook).checkIfMoved()
&& board.pathClear(startFile, startRank, (char)('a' + 1), startRank)
&& !Chess.isSquareUnderAttack((char)(startFile - 1), startRank, this.isBlack, board)
&& !Chess.isSquareUnderAttack(newFile, newRank, this.isBlack, board) &&
!Chess.isSquareUnderAttack(startFile, startRank, this.isBlack, board)) {
return true;
}
}
}
if (!isDiffSquare(startFile, startRank, newFile, newRank)) return false;
if ((Math.abs(newFile - startFile) <= 1 && Math.abs(newRank - startRank) <= 1)) {
Piece newLoc = board.getPiece(newFile, newRank);
if (newLoc == null) {
return true;
}
if (newLoc.isBlack != this.isBlack) return true;
}
return false;
}
public void officiallyMoved() {
kingHasMoved = true;
}
public boolean checkIfMoved() {
return kingHasMoved;
}
@Override
public Piece clone() {
King cloned = new King(this.getFile(), this.getRank(), this.isBlack);
cloned.kingHasMoved = this.kingHasMoved;
return cloned;
}
@Override
public String getSymbol() {
return ("K");
}
}