-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPawn.java
More file actions
53 lines (44 loc) · 1.79 KB
/
Copy pathPawn.java
File metadata and controls
53 lines (44 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
public class Pawn extends ChessPiece {
public Pawn(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) && chessBoard.board[line][column] != null) { // check that position in board
if (column == toColumn) { // check that we don't want to eat
int dir;
int start;
if (color.equals("White")) { // for white piece
dir = 1;
start = 1;
} else { // for black piece
dir = -1;
start = 6;
}
if (line + dir == toLine) { //check direction
return chessBoard.board[toLine][toColumn] == null;
}
if (line == start && line + 2 * dir == toLine) {
return chessBoard.board[toLine][toColumn] == null && chessBoard.board[line + dir][column] == null; // check that positions is null
}
} else { // want to eat piece
if ((column - toColumn == 1 || column - toColumn == -1) && (line - toLine == 1 || line - toLine == -1) && // check that piece another color
chessBoard.board[toLine][toColumn] != null) {
return !chessBoard.board[toLine][toColumn].getColor().equals(color);
} else return false;
}
}
return false;
}
@Override
public String getSymbol() {
return "P";
}
public boolean checkPos(int pos) {
return pos >= 0 && pos <= 7;
}
}