-
Notifications
You must be signed in to change notification settings - Fork 9
/
King.java
112 lines (93 loc) Β· 3.33 KB
/
King.java
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
package chess.pieces;
import boardgame.Board;
import boardgame.Position;
import chess.ChessMatch;
import chess.ChessPiece;
import chess.Color;
public class King extends ChessPiece {
private ChessMatch chessMatch;
public King(Board board, Color color, ChessMatch chessMatch) {
super(board, color);
this.chessMatch = chessMatch;
}
private boolean testRookCastling(Position position) {
ChessPiece p = (ChessPiece) getBoard().piece(position);
return p != null && p instanceof Rook && p.getColor() == getColor() && p.getMoveCount() == 0;
}
@Override
public String toString() {
return "K";
}
private boolean canMove(Position position) {
ChessPiece p = (ChessPiece) getBoard().piece(position);
return p == null || p.getColor() != getColor();
}
@Override
public boolean[][] possibleMoves() {
boolean[][] mat = new boolean[getBoard().getRows()][getBoard().getColumns()];
Position p = new Position(0, 0);
// above
p.setValue(position.getRow() - 1, position.getColumn());
if (getBoard().positionExists(p) && canMove(p)) {
mat[p.getRow()][p.getColumn()] = true;
}
// below
p.setValue(position.getRow() + 1, position.getColumn());
if (getBoard().positionExists(p) && canMove(p)) {
mat[p.getRow()][p.getColumn()] = true;
}
// left
p.setValue(position.getRow(), position.getColumn() - 1);
if (getBoard().positionExists(p) && canMove(p)) {
mat[p.getRow()][p.getColumn()] = true;
}
// right
p.setValue(position.getRow(), position.getColumn() + 1);
if (getBoard().positionExists(p) && canMove(p)) {
mat[p.getRow()][p.getColumn()] = true;
}
// nw
p.setValue(position.getRow() - 1, position.getColumn() - 1);
if (getBoard().positionExists(p) && canMove(p)) {
mat[p.getRow()][p.getColumn()] = true;
}
// ne
p.setValue(position.getRow() - 1, position.getColumn() + 1);
if (getBoard().positionExists(p) && canMove(p)) {
mat[p.getRow()][p.getColumn()] = true;
}
// sw
p.setValue(position.getRow() + 1, position.getColumn() - 1);
if (getBoard().positionExists(p) && canMove(p)) {
mat[p.getRow()][p.getColumn()] = true;
}
// se
p.setValue(position.getRow() + 1, position.getColumn() + 1);
if (getBoard().positionExists(p) && canMove(p)) {
mat[p.getRow()][p.getColumn()] = true;
}
// Specialmove castling
if (getMoveCount() == 0 && !chessMatch.getCheck()) {
// #specialmove castling kingside rook
Position postT1 = new Position(position.getRow(), position.getColumn() + 3);
if (testRookCastling(postT1)) {
Position p1 = new Position(position.getRow(), position.getColumn() + 1);
Position p2 = new Position(position.getRow(), position.getColumn() + 2);
if (getBoard().piece(p1) == null && getBoard().piece(p2) == null) {
mat[position.getRow()][position.getColumn() + 2] = true;
}
}
// #specialmove castling kingside rook
Position postT2 = new Position(position.getRow(), position.getColumn() - 4);
if (testRookCastling(postT2)) {
Position p1 = new Position(position.getRow(), position.getColumn() + 1);
Position p2 = new Position(position.getRow(), position.getColumn() + 2);
Position p3 = new Position(position.getRow(), position.getColumn() + 3);
if (getBoard().piece(p1) == null && getBoard().piece(p2) == null && getBoard().piece(p3) == null) {
mat[position.getRow()][position.getColumn() + 2] = true;
}
}
}
return mat;
}
}