-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBishop.java
More file actions
78 lines (71 loc) · 3.15 KB
/
Copy pathBishop.java
File metadata and controls
78 lines (71 loc) · 3.15 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
73
74
75
76
77
78
public class Bishop extends ChessPiece {
public Bishop(String color) {
super(color);
}
@Override
public String getColor() {
return color;
}
@Override
public boolean canMoveToPosition(ChessBoard chessBoard, int line, int column, int toLine, int toColumn) {
// check that we can move to position and can't moved out from board or in not empty position
if (line != toLine && column != toColumn &&
getMax(line, toLine) - getMin(line, toLine) == getMax(column, toColumn) - getMin(column, toColumn) &&
checkPos(line) && checkPos(column) && checkPos(toLine) && checkPos(toColumn) &&
(chessBoard.board[toLine][toColumn] == null || !chessBoard.board[toLine][toColumn].color.equals(this.color)) &&
chessBoard.board[line][column] != null) {
if (!chessBoard.board[line][column].equals(this)) {
return false;
}
// from up-left to down-right
if ((column == getMin(column, toColumn) && line == getMax(line, toLine)) ||
(toColumn == getMin(column, toColumn) && toLine == getMax(line, toLine))) {
int fromL = getMax(line, toLine);
int fromC = getMin(column, toColumn);
int toL = getMin(line, toLine);
int toC = getMax(column, toColumn);
int[][] positions = new int[toC - fromC][1];
for (int i = 1; i < toC - fromC; i++) {
if (chessBoard.board[fromL - i][fromC + i] == null) {
positions[i - 1] = new int[]{fromL - i, fromC + i};
} else if (!chessBoard.board[fromL - i][fromC + i].color.equals(this.color) && fromL - i == toLine) {
positions[i - 1] = new int[]{fromL - i, fromC + i};
} else {
return false;
}
}
return true;
} else {
// from down-left to up-right
int fromL = getMin(line, toLine);
int fromC = getMin(column, toColumn);
int toL = getMax(line, toLine);
int toC = getMax(column, toColumn);
int[][] positions = new int[toC - fromC][1];
for (int i = 1; i < toC - fromC; i++) {
if (chessBoard.board[fromL + i][fromC + i] == null) {
positions[i - 1] = new int[]{fromL + i, fromC + i};
} else if (!chessBoard.board[fromL + i][fromC + i].color.equals(this.color) && fromL + i == toLine) {
positions[i - 1] = new int[]{fromL + i, fromC + i};
} else {
return false;
}
}
return true;
}
} else return false;
}
@Override
public String getSymbol() {
return "B";
}
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;
}
}