-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRook.java
More file actions
72 lines (60 loc) · 2.96 KB
/
Copy pathRook.java
File metadata and controls
72 lines (60 loc) · 2.96 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
public class Rook extends ChessPiece {
public Rook(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 (column == toColumn) {
for (int i = getMin(line, toLine); i < getMax(line, toLine); i++) {
if (chessBoard.board[i][column] != null) {
if (chessBoard.board[i][column] == this && i == getMax(line, toLine)) return false;
else if (chessBoard.board[i][column].getColor().equals(this.color) && i == toLine)
return false;
else if (!chessBoard.board[i][column].getColor().equals(this.color) && i == toLine)
return true;
else if (i != toLine && i != line) return false;
}
}
if (chessBoard.board[toLine][column] != null) {
if (chessBoard.board[toLine][column].getColor().equals(this.color) && chessBoard.board[toLine][column] != this)
return false;
else return !chessBoard.board[toLine][column].getColor().equals(this.color) && chessBoard.board[toLine][column] != this;
} else return true;
} else if (line == toLine) {
for (int i = getMin(toColumn, column); i < getMax(column, toColumn); i++) {
if (chessBoard.board[line][i] != null) {
if (chessBoard.board[line][i] == this && i == getMax(column, toColumn)) return false;
else if (chessBoard.board[line][i].getColor().equals(this.color) && i == toColumn)
return false;
else if (!chessBoard.board[line][i].getColor().equals(this.color) && i == toColumn)
return true;
else if (i != toLine && i != column) return false;
}
}
if (chessBoard.board[toLine][toColumn] != null) {
if (chessBoard.board[toLine][toColumn].getColor().equals(this.color) && chessBoard.board[toLine][toColumn] != this)
return false;
else return !chessBoard.board[toLine][toColumn].getColor().equals(this.color) && chessBoard.board[toLine][toColumn] != this;
} else return true;
} else return false;
} else return false;
}
@Override
public String getSymbol() {
return "R";
}
public int getMax(int a, int b) {
return Math.max(a, b);
}
public int getMin(int a, int b) {
return Math.min(a, b);
}
public boolean checkPos(int pos) {
return pos >= 0 && pos <= 7;
}
}