-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueen.java
More file actions
117 lines (109 loc) · 5.59 KB
/
Copy pathQueen.java
File metadata and controls
117 lines (109 loc) · 5.59 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
public class Queen extends ChessPiece {
public Queen(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) {
//check that we moved like bishop and cords is correct
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 if (checkPos(line) && checkPos(column) && checkPos(toLine) && checkPos(toColumn)) {
// if we moved like rook and cords is correct
if (column == toColumn) {
// from line to line
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) {
//from column to column
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 "Q";
}
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;
}
}