-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKing.java
More file actions
50 lines (41 loc) · 1.52 KB
/
Copy pathKing.java
File metadata and controls
50 lines (41 loc) · 1.52 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
public class King extends ChessPiece {
public King(String color) {
super(color);
}
@Override
public String getColor() {
return color;
}
@Override
public boolean canMoveToPosition(ChessBoard chessBoard, int line, int column, int toLine, int toColumn) {
if (checkPos(line) && checkPos(column) && checkPos(toLine) && checkPos(toColumn)) {
if (Math.abs(line - toLine) > 1 || Math.abs(column - toColumn) > 1) return false;
if (isUnderAttack(chessBoard, toLine, toColumn)) return false;
if (chessBoard.board[toLine][toColumn] != null) {
return !chessBoard.board[toLine][toColumn].getColor().equals(color);
}
return true;
} else return false;
}
@Override
public String getSymbol() {
return "K";
}
public boolean isUnderAttack(ChessBoard chessBoard, int line, int column) {
if (checkPos(line) && checkPos(column)) {
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
if (chessBoard.board[i][j] != null) {
if (!chessBoard.board[i][j].getColor().equals(color) && chessBoard.board[i][j].canMoveToPosition(chessBoard, i, j, line, column)) {
return true;
}
}
}
}
return false;
} else return false;
}
public boolean checkPos(int pos) {
return pos >= 0 && pos <= 7;
}
}