-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHorse.java
More file actions
46 lines (40 loc) · 1.86 KB
/
Copy pathHorse.java
File metadata and controls
46 lines (40 loc) · 1.86 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
public class Horse extends ChessPiece {
public Horse(String color) {
super(color);
}
@Override
public String getColor() {
return this.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 (line != toLine && column != toColumn && (chessBoard.board[toLine][toColumn] == null || // check that horse
!chessBoard.board[toLine][toColumn].color.equals(this.color)) && // can't move out
chessBoard.board[line][column] != null) { // position is empty
if (!chessBoard.board[line][column].equals(this)) {
return false;
}
// all positions for horse
int[][] positions = new int[][]{
{line - 2, column - 1}, {line - 2, column + 1},
{line + 2, column - 1}, {line + 2, column + 1},
{line - 1, column - 2}, {line - 1, column + 2},
{line + 1, column - 2}, {line + 1, column + 2}};
for (int i = 0; i < positions.length; i++) {
if (positions[i][0] == toLine && positions[i][1] == toColumn)
return true; // check that toLine and toColumn
} // in positions
}
} else return false;
return false;
}
@Override
public String getSymbol() {
return "H";
}
public boolean checkPos(int pos) { // check that our position is correct
if (pos >= 0 && pos <= 7) return true;
else return false;
}
}