-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathPiece.java
More file actions
187 lines (157 loc) · 5.2 KB
/
Piece.java
File metadata and controls
187 lines (157 loc) · 5.2 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import javax.imageio.ImageIO;
public abstract class Piece {
private final int color;
private Square currentSquare;
private BufferedImage img;
public Piece(int color, Square initSq, String img_file) {
this.color = color;
this.currentSquare = initSq;
try {
if (this.img == null) {
this.img = ImageIO.read(getClass().getResource(img_file));
}
} catch (IOException e) {
System.out.println("File not found: " + e.getMessage());
}
}
public boolean move(Square fin) {
Piece occup = fin.getOccupyingPiece();
if (occup != null) {
if (occup.getColor() == this.color) return false;
else fin.capture(this);
}
currentSquare.removePiece();
this.currentSquare = fin;
currentSquare.put(this);
return true;
}
public Square getPosition() {
return currentSquare;
}
public void setPosition(Square sq) {
this.currentSquare = sq;
}
public int getColor() {
return color;
}
public Image getImage() {
return img;
}
public void draw(Graphics g) {
int x = currentSquare.getX();
int y = currentSquare.getY();
g.drawImage(this.img, x, y, null);
}
public int[] getLinearOccupations(Square[][] board, int x, int y) {
int lastYabove = 0;
int lastXright = 7;
int lastYbelow = 7;
int lastXleft = 0;
for (int i = 0; i < y; i++) {
if (board[i][x].isOccupied()) {
if (board[i][x].getOccupyingPiece().getColor() != this.color) {
lastYabove = i;
} else lastYabove = i + 1;
}
}
for (int i = 7; i > y; i--) {
if (board[i][x].isOccupied()) {
if (board[i][x].getOccupyingPiece().getColor() != this.color) {
lastYbelow = i;
} else lastYbelow = i - 1;
}
}
for (int i = 0; i < x; i++) {
if (board[y][i].isOccupied()) {
if (board[y][i].getOccupyingPiece().getColor() != this.color) {
lastXleft = i;
} else lastXleft = i + 1;
}
}
for (int i = 7; i > x; i--) {
if (board[y][i].isOccupied()) {
if (board[y][i].getOccupyingPiece().getColor() != this.color) {
lastXright = i;
} else lastXright = i - 1;
}
}
int[] occups = {lastYabove, lastYbelow, lastXleft, lastXright};
return occups;
}
public List<Square> getDiagonalOccupations(Square[][] board, int x, int y) {
LinkedList<Square> diagOccup = new LinkedList<Square>();
int xNW = x - 1;
int xSW = x - 1;
int xNE = x + 1;
int xSE = x + 1;
int yNW = y - 1;
int ySW = y + 1;
int yNE = y - 1;
int ySE = y + 1;
while (xNW >= 0 && yNW >= 0) {
if (board[yNW][xNW].isOccupied()) {
if (board[yNW][xNW].getOccupyingPiece().getColor() == this.color) {
break;
} else {
diagOccup.add(board[yNW][xNW]);
break;
}
} else {
diagOccup.add(board[yNW][xNW]);
yNW--;
xNW--;
}
}
while (xSW >= 0 && ySW < 8) {
if (board[ySW][xSW].isOccupied()) {
if (board[ySW][xSW].getOccupyingPiece().getColor() == this.color) {
break;
} else {
diagOccup.add(board[ySW][xSW]);
break;
}
} else {
diagOccup.add(board[ySW][xSW]);
ySW++;
xSW--;
}
}
while (xSE < 8 && ySE < 8) {
if (board[ySE][xSE].isOccupied()) {
if (board[ySE][xSE].getOccupyingPiece().getColor() == this.color) {
break;
} else {
diagOccup.add(board[ySE][xSE]);
break;
}
} else {
diagOccup.add(board[ySE][xSE]);
ySE++;
xSE++;
}
}
while (xNE < 8 && yNE >= 0) {
if (board[yNE][xNE].isOccupied()) {
if (board[yNE][xNE].getOccupyingPiece().getColor() == this.color) {
break;
} else {
diagOccup.add(board[yNE][xNE]);
break;
}
} else {
diagOccup.add(board[yNE][xNE]);
yNE--;
xNE++;
}
}
return diagOccup;
}
// No implementation, to be implemented by each subclass
public abstract List<Square> getLegalMoves(Board b);
}