10 import org.homelinux.largo.games.board.Board; |
10 import org.homelinux.largo.games.board.Board; |
11 import org.homelinux.largo.games.board.Piece; |
11 import org.homelinux.largo.games.board.Piece; |
12 import org.homelinux.largo.utils.ImgComponent; |
12 import org.homelinux.largo.utils.ImgComponent; |
13 |
13 |
14 public class ChessBoard extends Board { |
14 public class ChessBoard extends Board { |
15 static final long serialVersionUID = 1L; |
15 static final long serialVersionUID = 1L; |
16 |
16 |
17 static final int ROOK = 1; |
17 static final int ROOK = 1; |
18 static final int KNIGHT = 2; |
18 static final int KNIGHT = 2; |
19 static final int BISHOP = 3; |
19 static final int BISHOP = 3; |
20 static final int QUEEN = 4; |
20 static final int QUEEN = 4; |
21 static final int KING = 5; |
21 static final int KING = 5; |
22 static final int PAWN = 6; |
22 static final int PAWN = 6; |
23 |
23 |
24 final Piece white_queen = new Piece(new ImgComponent("images/white_queen.png").getImage(), "white", QUEEN, 90); |
24 final Piece white_queen = new Piece(new ImgComponent("images/white_queen.png").getImage(), "white", QUEEN, 90); |
25 final Piece black_queen = new Piece(new ImgComponent("images/black_queen.png").getImage(), "black", QUEEN, 90); |
25 final Piece black_queen = new Piece(new ImgComponent("images/black_queen.png").getImage(), "black", QUEEN, 90); |
26 |
26 |
27 boolean white_rochades_small; |
27 boolean white_rochades_small; |
28 boolean white_rochades_big; |
28 boolean white_rochades_big; |
29 boolean black_rochades_small; |
29 boolean black_rochades_small; |
30 boolean black_rochades_big; |
30 boolean black_rochades_big; |
31 |
31 |
32 void initBoard() { |
32 void initBoard() { |
33 add(new ImgComponent("images/black_rook.png").getImage(), 0, "black", ROOK); |
33 add(new ImgComponent("images/black_rook.png").getImage(), 0, "black", ROOK); |
34 add(new ImgComponent("images/black_knight.png").getImage(), 1, "black", KNIGHT); |
34 add(new ImgComponent("images/black_knight.png").getImage(), 1, "black", KNIGHT); |
35 add(new ImgComponent("images/black_bishop.png").getImage(), 2, "black", BISHOP); |
35 add(new ImgComponent("images/black_bishop.png").getImage(), 2, "black", BISHOP); |
36 add(new ImgComponent("images/black_queen.png").getImage(), 3, "black", QUEEN); |
36 add(new ImgComponent("images/black_queen.png").getImage(), 3, "black", QUEEN); |
37 add(new ImgComponent("images/black_king.png").getImage(), 4, "black", KING); |
37 add(new ImgComponent("images/black_king.png").getImage(), 4, "black", KING); |
38 add(new ImgComponent("images/black_bishop.png").getImage(), 5, "black", BISHOP); |
38 add(new ImgComponent("images/black_bishop.png").getImage(), 5, "black", BISHOP); |
39 add(new ImgComponent("images/black_knight.png").getImage(), 6, "black", KNIGHT); |
39 add(new ImgComponent("images/black_knight.png").getImage(), 6, "black", KNIGHT); |
40 add(new ImgComponent("images/black_rook.png").getImage(), 7, "black", ROOK); |
40 add(new ImgComponent("images/black_rook.png").getImage(), 7, "black", ROOK); |
41 |
41 |
42 for (int i = 0; i < 8; i++) |
42 for (int i = 0; i < 8; i++) |
43 add(new ImgComponent("images/black_pawn.png").getImage(), 8 + i, "black", PAWN); |
43 add(new ImgComponent("images/black_pawn.png").getImage(), 8 + i, "black", PAWN); |
44 |
44 |
45 add(new ImgComponent("images/white_rook.png").getImage(), 56, "white", ROOK); |
45 add(new ImgComponent("images/white_rook.png").getImage(), 56, "white", ROOK); |
46 add(new ImgComponent("images/white_knight.png").getImage(), 57, "white", KNIGHT); |
46 add(new ImgComponent("images/white_knight.png").getImage(), 57, "white", KNIGHT); |
47 add(new ImgComponent("images/white_bishop.png").getImage(), 58, "white", BISHOP); |
47 add(new ImgComponent("images/white_bishop.png").getImage(), 58, "white", BISHOP); |
48 add(new ImgComponent("images/white_queen.png").getImage(), 59, "white", QUEEN); |
48 add(new ImgComponent("images/white_queen.png").getImage(), 59, "white", QUEEN); |
49 add(new ImgComponent("images/white_king.png").getImage(), 60, "white", KING); |
49 add(new ImgComponent("images/white_king.png").getImage(), 60, "white", KING); |
50 add(new ImgComponent("images/white_bishop.png").getImage(), 61, "white", BISHOP); |
50 add(new ImgComponent("images/white_bishop.png").getImage(), 61, "white", BISHOP); |
51 add(new ImgComponent("images/white_knight.png").getImage(), 62, "white", KNIGHT); |
51 add(new ImgComponent("images/white_knight.png").getImage(), 62, "white", KNIGHT); |
52 add(new ImgComponent("images/white_rook.png").getImage(), 63, "white", ROOK); |
52 add(new ImgComponent("images/white_rook.png").getImage(), 63, "white", ROOK); |
53 |
53 |
54 for (int i = 0; i < 8; i++) |
54 for (int i = 0; i < 8; i++) |
55 add(new ImgComponent("images/white_pawn.png").getImage(), 55 - i, "white", PAWN); |
55 add(new ImgComponent("images/white_pawn.png").getImage(), 55 - i, "white", PAWN); |
56 } |
56 } |
57 |
57 |
58 /** |
58 /** |
59 * The init method initializes a complete chess board with figures and a |
59 * The init method initializes a complete chess board with figures and a |
60 * history. |
60 * history. |
61 */ |
61 */ |
62 public void init() { |
62 public void init() { |
63 super.init(); |
63 super.init(); |
64 initBoard(); |
64 initBoard(); |
65 board_pieces = getPieces(); |
65 board_pieces = getPieces(); |
66 repaint(); |
66 repaint(); |
67 } |
67 } |
68 |
68 |
69 public ChessBoard(int w, int h) { |
69 public ChessBoard(int w, int h) { |
70 super(w, h); |
70 super(w, h); |
71 } |
71 } |
72 |
72 |
73 void add(Image img, int i, String c, int t) { |
73 void add(Image img, int i, String c, int t) { |
74 int v = 0; |
74 int v = 0; |
75 |
75 |
76 switch (t) { |
76 switch (t) { |
77 case ROOK: |
77 case ROOK: |
78 v = 80; |
78 v = 80; |
79 break; |
79 break; |
80 case KNIGHT: |
80 case KNIGHT: |
81 v = 30; |
81 v = 30; |
82 break; |
82 break; |
83 case BISHOP: |
83 case BISHOP: |
84 v = 30; |
84 v = 30; |
85 break; |
85 break; |
86 case QUEEN: |
86 case QUEEN: |
87 v = 90; |
87 v = 90; |
88 break; |
88 break; |
89 case KING: |
89 case KING: |
90 v = 120; |
90 v = 120; |
91 break; |
91 break; |
92 case PAWN: |
92 case PAWN: |
93 v = 10; |
93 v = 10; |
94 break; |
94 break; |
95 } |
95 } |
96 |
96 |
97 setPiece(i, new Piece(img, c, t, v)); |
97 setPiece(i, new Piece(img, c, t, v)); |
98 } |
98 } |
99 |
99 |
100 /** |
100 /** |
101 * returns true, when the white king is at pieces[i] |
101 * returns true, when the white king is at pieces[i] |
102 */ |
102 */ |
103 public boolean isWhiteKing(int i) { |
103 public boolean isWhiteKing(int i) { |
104 if (isWhite(i) && getType(i) == KING) |
104 if (isWhite(i) && getType(i) == KING) |
105 return true; |
105 return true; |
106 return false; |
106 return false; |
107 } |
107 } |
108 |
108 |
109 /** |
109 /** |
110 * returns true, when the black king is at pieces[i] |
110 * returns true, when the black king is at pieces[i] |
111 */ |
111 */ |
112 public boolean isBlackKing(int i) { |
112 public boolean isBlackKing(int i) { |
113 if (isBlack(i) && getType(i) == KING) |
113 if (isBlack(i) && getType(i) == KING) |
114 return true; |
114 return true; |
115 return false; |
115 return false; |
116 } |
116 } |
117 |
117 |
118 /** |
118 /** |
119 * Returns the score of the current position Every field controlled by |
119 * Returns the score of the current position Every field controlled by |
120 * [color] gives one point A King, controlled by [color], gives 10 extra |
120 * [color] gives one point A King, controlled by [color], gives 10 extra |
121 * points |
121 * points |
122 */ |
122 */ |
123 public int controls(String color) { |
123 public int controls(String color) { |
124 int t, o; |
124 int t, o; |
125 int value = 0; |
125 int value = 0; |
126 |
126 |
127 for (o = 0; o < 64; o++) { |
127 for (o = 0; o < 64; o++) { |
128 if (color.equals(getColor(o))) { |
128 if (color.equals(getColor(o))) { |
129 for (t = 0; t < 64; t++) { |
129 for (t = 0; t < 64; t++) { |
130 if (validMove(t, o)) { |
130 if (validMove(t, o)) { |
131 if (getType(t) == KING) |
131 if (getType(t) == KING) |
132 value += 10; |
132 value += 10; |
133 value++; |
133 value++; |
134 } |
134 } |
135 } |
135 } |
136 } |
136 } |
137 } |
137 } |
138 return value; |
138 return value; |
139 } |
139 } |
140 |
140 |
141 /** |
141 /** |
142 * Checks, wether this move is possible or not. |
142 * Checks, wether this move is possible or not. |
143 */ |
143 */ |
144 public boolean validMove(int t, int o) { |
144 public boolean validMove(int t, int o) { |
145 int steps; |
145 int steps; |
146 int rows; |
146 int rows; |
147 int dx; |
147 int dx; |
148 |
148 |
149 /* Must be here */ |
149 /* Must be here */ |
150 white_rochades_small = false; |
150 white_rochades_small = false; |
151 white_rochades_big = false; |
151 white_rochades_big = false; |
152 black_rochades_small = false; |
152 black_rochades_small = false; |
153 black_rochades_big = false; |
153 black_rochades_big = false; |
154 |
154 |
155 if (t == o) |
155 if (t == o) |
156 return false; |
156 return false; |
157 |
157 |
158 if (isSamePiece(t, o)) |
158 if (isSamePiece(t, o)) |
159 return false; |
159 return false; |
160 |
160 |
161 if (getType(o) == EMPTY) |
161 if (getType(o) == EMPTY) |
162 return false; |
162 return false; |
163 |
163 |
164 /* |
164 /* |
165 * 00 01 02 03 04 05 06 07 |
165 * 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 |
166 * 08 09 10 11 12 13 14 15 |
166 * 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
167 * 16 17 18 19 20 21 22 23 |
167 * 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
168 * 24 25 26 27 28 29 30 31 |
168 */ |
169 * 32 33 34 35 36 37 38 39 |
169 |
170 * 40 41 42 43 44 45 46 47 |
170 steps = Math.abs(t - o); |
171 * 48 49 50 51 52 53 54 55 |
171 rows = Math.abs(t / 8 - o / 8); |
172 * 56 57 58 59 60 61 62 63 |
172 |
173 */ |
173 switch (getType(o)) { |
174 |
174 case ROOK: |
175 steps = Math.abs(t - o); |
175 if (steps < 8 && rows == 0) { |
176 rows = Math.abs(t / 8 - o / 8); |
176 if (t > o) |
177 |
177 for (dx = o + 1; dx < t; dx++) { |
178 switch (getType(o)) { |
178 if (getColor(dx) != null) { |
179 case ROOK: |
179 return false; |
180 if (steps < 8 && rows == 0) { |
180 } |
181 if (t > o) |
181 } |
182 for (dx = o + 1; dx < t; dx++) { |
182 else |
183 if (getColor(dx) != null) { |
183 for (dx = o - 1; dx > t; dx--) { |
184 return false; |
184 if (getColor(dx) != null) { |
185 } |
185 return false; |
186 } |
186 } |
187 else |
187 } |
188 for (dx = o - 1; dx > t; dx--) { |
188 return true; |
189 if (getColor(dx) != null) { |
189 } |
190 return false; |
190 if (steps % 8 == 0) { |
191 } |
191 if (t > o) { |
192 } |
192 for (dx = o + 8; dx < t; dx += 8) { |
193 return true; |
193 if (getColor(dx) != null) |
194 } |
194 return false; |
195 if (steps % 8 == 0) { |
195 } |
196 if (t > o) { |
196 } else { |
197 for (dx = o + 8; dx < t; dx += 8) { |
197 for (dx = o - 8; dx > t; dx -= 8) { |
198 if (getColor(dx) != null) |
198 if (getColor(dx) != null) |
199 return false; |
199 return false; |
200 } |
200 } |
201 } else { |
201 } |
202 for (dx = o - 8; dx > t; dx -= 8) { |
202 if (steps == 8 && rows == 1) |
203 if (getColor(dx) != null) |
203 return true; |
204 return false; |
204 if (steps == 16 && rows == 2) |
205 } |
205 return true; |
206 } |
206 if (steps == 24 && rows == 3) |
207 if (steps == 8 && rows == 1) |
207 return true; |
208 return true; |
208 if (steps == 32 && rows == 4) |
209 if (steps == 16 && rows == 2) |
209 return true; |
210 return true; |
210 if (steps == 40 && rows == 5) |
211 if (steps == 24 && rows == 3) |
211 return true; |
212 return true; |
212 if (steps == 48 && rows == 6) |
213 if (steps == 32 && rows == 4) |
213 return true; |
214 return true; |
214 if (steps == 56 && rows == 7) |
215 if (steps == 40 && rows == 5) |
215 return true; |
216 return true; |
216 } |
217 if (steps == 48 && rows == 6) |
217 break; |
218 return true; |
218 case KNIGHT: |
219 if (steps == 56 && rows == 7) |
219 /* works fine */ |
220 return true; |
220 if (steps == 6 && rows == 1) |
221 } |
221 return true; |
222 break; |
222 if (steps == 10 && rows == 1) |
223 case KNIGHT: |
223 return true; |
224 /* works fine */ |
224 if (steps == 15 && rows == 2) |
225 if (steps == 6 && rows == 1) |
225 return true; |
226 return true; |
226 if (steps == 17 && rows == 2) |
227 if (steps == 10 && rows == 1) |
227 return true; |
228 return true; |
228 break; |
229 if (steps == 15 && rows == 2) |
229 case BISHOP: |
230 return true; |
230 if (steps % 7 == 0) { |
231 if (steps == 17 && rows == 2) |
231 if (t > o) { |
232 return true; |
232 for (dx = o + 7; dx < t; dx += 7) { |
233 break; |
233 if (getColor(dx) != null) |
234 case BISHOP: |
234 return false; |
235 if (steps % 7 == 0) { |
235 } |
236 if (t > o) { |
236 } else { |
237 for (dx = o + 7; dx < t; dx += 7) { |
237 for (dx = o - 7; dx > t; dx -= 7) { |
238 if (getColor(dx) != null) |
238 if (getColor(dx) != null) |
239 return false; |
239 return false; |
240 } |
240 } |
241 } else { |
241 } |
242 for (dx = o - 7; dx > t; dx -= 7) { |
242 if (steps == 7 && rows == 1) |
243 if (getColor(dx) != null) |
243 return true; |
244 return false; |
244 if (steps == 14 && rows == 2) |
245 } |
245 return true; |
246 } |
246 if (steps == 21 && rows == 3) |
247 if (steps == 7 && rows == 1) |
247 return true; |
248 return true; |
248 if (steps == 28 && rows == 4) |
249 if (steps == 14 && rows == 2) |
249 return true; |
250 return true; |
250 if (steps == 35 && rows == 5) |
251 if (steps == 21 && rows == 3) |
251 return true; |
252 return true; |
252 if (steps == 42 && rows == 6) |
253 if (steps == 28 && rows == 4) |
253 return true; |
254 return true; |
254 if (steps == 49 && rows == 7) |
255 if (steps == 35 && rows == 5) |
255 return true; |
256 return true; |
256 } |
257 if (steps == 42 && rows == 6) |
257 if (steps % 9 == 0) { |
258 return true; |
258 if (t > o) { |
259 if (steps == 49 && rows == 7) |
259 for (dx = o + 9; dx < t; dx += 9) { |
260 return true; |
260 if (getColor(dx) != null) |
261 } |
261 return false; |
262 if (steps % 9 == 0) { |
262 } |
263 if (t > o) { |
263 } else { |
264 for (dx = o + 9; dx < t; dx += 9) { |
264 for (dx = o - 9; dx > t; dx -= 9) { |
265 if (getColor(dx) != null) |
265 if (getColor(dx) != null) |
266 return false; |
266 return false; |
267 } |
267 } |
268 } else { |
268 } |
269 for (dx = o - 9; dx > t; dx -= 9) { |
269 if (steps == 9 && rows == 1) |
270 if (getColor(dx) != null) |
270 return true; |
271 return false; |
271 if (steps == 18 && rows == 2) |
272 } |
272 return true; |
273 } |
273 if (steps == 27 && rows == 3) |
274 if (steps == 9 && rows == 1) |
274 return true; |
275 return true; |
275 if (steps == 36 && rows == 4) |
276 if (steps == 18 && rows == 2) |
276 return true; |
277 return true; |
277 if (steps == 45 && rows == 5) |
278 if (steps == 27 && rows == 3) |
278 return true; |
279 return true; |
279 if (steps == 54 && rows == 6) |
280 if (steps == 36 && rows == 4) |
280 return true; |
281 return true; |
281 if (steps == 63 && rows == 7) |
282 if (steps == 45 && rows == 5) |
282 return true; |
283 return true; |
283 } |
284 if (steps == 54 && rows == 6) |
284 break; |
285 return true; |
285 |
286 if (steps == 63 && rows == 7) |
286 case QUEEN: |
287 return true; |
287 if (steps < 8 && rows == 0) { |
288 } |
288 if (t > o) |
289 break; |
289 for (dx = o + 1; dx < t; dx++) { |
290 |
290 if (getColor(dx) != null) |
291 case QUEEN: |
291 return false; |
292 if (steps < 8 && rows == 0) { |
292 } |
293 if (t > o) |
293 else |
294 for (dx = o + 1; dx < t; dx++) { |
294 for (dx = o - 1; dx > t; dx--) { |
295 if (getColor(dx) != null) |
295 if (getColor(dx) != null) |
296 return false; |
296 return false; |
297 } |
297 } |
298 else |
298 return true; |
299 for (dx = o - 1; dx > t; dx--) { |
299 } |
300 if (getColor(dx) != null) |
300 |
301 return false; |
301 if (steps % 8 == 0) { |
302 } |
302 if (t > o) { |
303 return true; |
303 for (dx = o + 8; dx < t; dx += 8) { |
304 } |
304 if (getColor(dx) != null) |
305 |
305 return false; |
306 if (steps % 8 == 0) { |
306 } |
307 if (t > o) { |
307 } else { |
308 for (dx = o + 8; dx < t; dx += 8) { |
308 for (dx = o - 8; dx > t; dx -= 8) { |
309 if (getColor(dx) != null) |
309 if (getColor(dx) != null) |
310 return false; |
310 return false; |
311 } |
311 } |
312 } else { |
312 } |
313 for (dx = o - 8; dx > t; dx -= 8) { |
313 |
314 if (getColor(dx) != null) |
314 if (steps == 8 && rows == 1) |
315 return false; |
315 return true; |
316 } |
316 if (steps == 16 && rows == 2) |
317 } |
317 return true; |
318 |
318 if (steps == 24 && rows == 3) |
319 if (steps == 8 && rows == 1) |
319 return true; |
320 return true; |
320 if (steps == 32 && rows == 4) |
321 if (steps == 16 && rows == 2) |
321 return true; |
322 return true; |
322 if (steps == 40 && rows == 5) |
323 if (steps == 24 && rows == 3) |
323 return true; |
324 return true; |
324 if (steps == 48 && rows == 6) |
325 if (steps == 32 && rows == 4) |
325 return true; |
326 return true; |
326 if (steps == 56 && rows == 7) |
327 if (steps == 40 && rows == 5) |
327 return true; |
328 return true; |
328 } |
329 if (steps == 48 && rows == 6) |
329 if (steps % 7 == 0) { |
330 return true; |
330 if (t > o) { |
331 if (steps == 56 && rows == 7) |
331 for (dx = o + 7; dx < t; dx += 7) { |
332 return true; |
332 if (getColor(dx) != null) |
333 } |
333 return false; |
334 if (steps % 7 == 0) { |
334 } |
335 if (t > o) { |
335 } else { |
336 for (dx = o + 7; dx < t; dx += 7) { |
336 for (dx = o - 7; dx > t; dx -= 7) { |
337 if (getColor(dx) != null) |
337 if (getColor(dx) != null) |
338 return false; |
338 return false; |
339 } |
339 } |
340 } else { |
340 } |
341 for (dx = o - 7; dx > t; dx -= 7) { |
341 if (steps == 7 && rows == 1) |
342 if (getColor(dx) != null) |
342 return true; |
343 return false; |
343 if (steps == 14 && rows == 2) |
344 } |
344 return true; |
345 } |
345 if (steps == 21 && rows == 3) |
346 if (steps == 7 && rows == 1) |
346 return true; |
347 return true; |
347 if (steps == 28 && rows == 4) |
348 if (steps == 14 && rows == 2) |
348 return true; |
349 return true; |
349 if (steps == 35 && rows == 5) |
350 if (steps == 21 && rows == 3) |
350 return true; |
351 return true; |
351 if (steps == 42 && rows == 6) |
352 if (steps == 28 && rows == 4) |
352 return true; |
353 return true; |
353 if (steps == 49 && rows == 7) |
354 if (steps == 35 && rows == 5) |
354 return true; |
355 return true; |
355 } |
356 if (steps == 42 && rows == 6) |
356 |
357 return true; |
357 if (steps % 9 == 0) { |
358 if (steps == 49 && rows == 7) |
358 if (t > o) { |
359 return true; |
359 for (dx = o + 9; dx < t; dx += 9) { |
360 } |
360 if (getColor(dx) != null) |
361 |
361 return false; |
362 if (steps % 9 == 0) { |
362 } |
363 if (t > o) { |
363 } else { |
364 for (dx = o + 9; dx < t; dx += 9) { |
364 for (dx = o - 9; dx > t; dx -= 9) { |
365 if (getColor(dx) != null) |
365 if (getColor(dx) != null) |
366 return false; |
366 return false; |
367 } |
367 } |
368 } else { |
368 } |
369 for (dx = o - 9; dx > t; dx -= 9) { |
369 if (steps == 9 && rows == 1) |
370 if (getColor(dx) != null) |
370 return true; |
371 return false; |
371 if (steps == 18 && rows == 2) |
372 } |
372 return true; |
373 } |
373 if (steps == 27 && rows == 3) |
374 if (steps == 9 && rows == 1) |
374 return true; |
375 return true; |
375 if (steps == 36 && rows == 4) |
376 if (steps == 18 && rows == 2) |
376 return true; |
377 return true; |
377 if (steps == 45 && rows == 5) |
378 if (steps == 27 && rows == 3) |
378 return true; |
379 return true; |
379 if (steps == 54 && rows == 6) |
380 if (steps == 36 && rows == 4) |
380 return true; |
381 return true; |
381 if (steps == 63 && rows == 7) |
382 if (steps == 45 && rows == 5) |
382 return true; |
383 return true; |
383 } |
384 if (steps == 54 && rows == 6) |
384 break; |
385 return true; |
385 |
386 if (steps == 63 && rows == 7) |
386 case KING: |
387 return true; |
387 if ((steps == 1) && (rows == 0)) |
388 } |
388 return true; |
389 break; |
389 |
390 |
390 if ((steps == 8) && (rows == 1)) |
391 case KING: |
391 return true; |
392 if ((steps == 1) && (rows == 0)) |
392 |
393 return true; |
393 if ((steps == 7) && (rows == 1)) |
394 |
394 return true; |
395 if ((steps == 8) && (rows == 1)) |
395 |
396 return true; |
396 if ((steps == 9) && (rows == 1)) |
397 |
397 return true; |
398 if ((steps == 7) && (rows == 1)) |
398 |
399 return true; |
399 if ((steps == 2) && (rows == 0)) { |
400 |
400 if (isWhiteKing(o)) { /* White: Rochade */ |
401 if ((steps == 9) && (rows == 1)) |
401 if (isWhiteKing(60) && isEmpty(61) && isEmpty(62) && (t == 62)) { |
402 return true; |
402 if ((getType(63) == ROOK) && isWhite(63)) { |
403 |
403 white_rochades_small = true; |
404 if ((steps == 2) && (rows == 0)) { |
404 return true; |
405 if (isWhiteKing(o)) { /* White: Rochade */ |
405 } |
406 if (isWhiteKing(60) && isEmpty(61) && isEmpty(62) && (t == 62)) { |
406 } |
407 if ((getType(63) == ROOK) && isWhite(63)) { |
407 |
408 white_rochades_small = true; |
408 if (isWhiteKing(60) && isEmpty(59) && isEmpty(58) && isEmpty(57) && (t == 58)) { |
409 return true; |
409 if ((getType(56) == ROOK) && isWhite(56)) { |
410 } |
410 white_rochades_big = true; |
411 } |
411 return true; |
412 |
412 } |
413 if (isWhiteKing(60) && isEmpty(59) && isEmpty(58) && isEmpty(57) && (t == 58)) { |
413 } |
414 if ((getType(56) == ROOK) && isWhite(56)) { |
414 } |
415 white_rochades_big = true; |
415 |
416 return true; |
416 if (isBlackKing(o)) { /* Black: Rochade */ |
417 } |
417 if (isBlackKing(4) && isEmpty(5) && isEmpty(6) && (t == 6)) { |
418 } |
418 if ((getType(7) == ROOK) && isBlack(7)) { |
419 } |
419 black_rochades_small = true; |
420 |
420 return true; |
421 if (isBlackKing(o)) { /* Black: Rochade */ |
421 } |
422 if (isBlackKing(4) && isEmpty(5) && isEmpty(6) && (t == 6)) { |
422 } |
423 if ((getType(7) == ROOK) && isBlack(7)) { |
423 |
424 black_rochades_small = true; |
424 if (isBlackKing(4) && isEmpty(3) && isEmpty(2) && isEmpty(1) && (t == 2)) { |
425 return true; |
425 if ((getType(0) == ROOK) && isBlack(0)) { |
426 } |
426 black_rochades_big = true; |
427 } |
427 return true; |
428 |
428 } |
429 if (isBlackKing(4) && isEmpty(3) && isEmpty(2) && isEmpty(1) && (t == 2)) { |
429 } |
430 if ((getType(0) == ROOK) && isBlack(0)) { |
430 } |
431 black_rochades_big = true; |
431 } |
432 return true; |
432 break; |
433 } |
433 |
434 } |
434 case PAWN: |
435 } |
435 if (getColor(o).equals("white")) { |
436 } |
436 if (steps == 7 && rows == 1 && isEnemy(t, o) && t < o) |
437 break; |
437 return true; |
438 |
438 |
439 case PAWN: |
439 if (steps == 9 && rows == 1 && isEnemy(t, o) && t < o) |
440 if (getColor(o).equals("white")) { |
440 return true; |
441 if (steps == 7 && rows == 1 && isEnemy(t, o) && t < o) |
441 } else { |
442 return true; |
442 if (steps == 7 && rows == 1 && isEnemy(t, o) && t > o) |
443 |
443 return true; |
444 if (steps == 9 && rows == 1 && isEnemy(t, o) && t < o) |
444 |
445 return true; |
445 if (steps == 9 && rows == 1 && isEnemy(t, o) && t > o) |
446 } else { |
446 return true; |
447 if (steps == 7 && rows == 1 && isEnemy(t, o) && t > o) |
447 } |
448 return true; |
448 |
449 |
449 if (steps % 8 == 0) { |
450 if (steps == 9 && rows == 1 && isEnemy(t, o) && t > o) |
450 if (t > o) { |
451 return true; |
451 for (dx = o + 8; dx <= t; dx += 8) { |
452 } |
452 if (getColor(dx) != null) |
453 |
453 return false; |
454 if (steps % 8 == 0) { |
454 } |
455 if (t > o) { |
455 } else { |
456 for (dx = o + 8; dx <= t; dx += 8) { |
456 for (dx = o - 8; dx >= t; dx -= 8) { |
457 if (getColor(dx) != null) |
457 if (getColor(dx) != null) |
458 return false; |
458 return false; |
459 } |
459 } |
460 } else { |
460 } |
461 for (dx = o - 8; dx >= t; dx -= 8) { |
461 |
462 if (getColor(dx) != null) |
462 if (steps == 8 && rows == 1) { |
463 return false; |
463 if (getColor(o).equals("white") && t < o) |
464 } |
464 return true; |
465 } |
465 if (getColor(o).equals("black") && t > o) |
466 |
466 return true; |
467 if (steps == 8 && rows == 1) { |
467 } |
468 if (getColor(o).equals("white") && t < o) |
468 |
469 return true; |
469 if (steps == 16 && rows == 2 && (o >= 8 && o <= 15)) { |
470 if (getColor(o).equals("black") && t > o) |
470 if (getColor(o).equals("white") && t < o) |
471 return true; |
471 return true; |
472 } |
472 if (getColor(o).equals("black") && t > o) |
473 |
473 return true; |
474 if (steps == 16 && rows == 2 && (o >= 8 && o <= 15)) { |
474 } |
475 if (getColor(o).equals("white") && t < o) |
475 |
476 return true; |
476 if (steps == 16 && rows == 2 && (o >= 48 && o <= 55)) { |
477 if (getColor(o).equals("black") && t > o) |
477 if (getColor(o).equals("white") && t < o) |
478 return true; |
478 return true; |
479 } |
479 if (getColor(o).equals("black") && t > o) |
480 |
480 return true; |
481 if (steps == 16 && rows == 2 && (o >= 48 && o <= 55)) { |
481 } |
482 if (getColor(o).equals("white") && t < o) |
482 } |
483 return true; |
483 break; |
484 if (getColor(o).equals("black") && t > o) |
484 } |
485 return true; |
485 return false; |
486 } |
486 } |
487 } |
487 |
488 break; |
488 /** |
489 } |
489 * This method must be called immediately after isValidMove(t, o) |
490 return false; |
490 */ |
491 } |
491 boolean rochade() { |
492 |
492 if (white_rochades_small) { |
493 /** |
493 setPiece(61, getPiece(63)); |
494 * This method must be called immediately after isValidMove(t, o) |
494 setPiece(63, new Piece(null, null, EMPTY, 0)); |
495 */ |
495 return true; |
496 boolean rochade() { |
496 } |
497 if (white_rochades_small) { |
497 |
498 setPiece(61, getPiece(63)); |
498 if (white_rochades_big) { |
499 setPiece(63, new Piece(null, null, EMPTY, 0)); |
499 setPiece(59, getPiece(56)); |
500 return true; |
500 setPiece(56, new Piece(null, null, EMPTY, 0)); |
501 } |
501 return true; |
502 |
502 } |
503 if (white_rochades_big) { |
503 |
504 setPiece(59, getPiece(56)); |
504 if (black_rochades_small) { |
505 setPiece(56, new Piece(null, null, EMPTY, 0)); |
505 setPiece(5, getPiece(7)); |
506 return true; |
506 setPiece(7, new Piece(null, null, EMPTY, 0)); |
507 } |
507 return true; |
508 |
508 } |
509 if (black_rochades_small) { |
509 |
510 setPiece(5, getPiece(7)); |
510 if (black_rochades_big) { |
511 setPiece(7, new Piece(null, null, EMPTY, 0)); |
511 setPiece(3, getPiece(0)); |
512 return true; |
512 setPiece(0, new Piece(null, null, EMPTY, 0)); |
513 } |
513 return true; |
514 |
514 } |
515 if (black_rochades_big) { |
515 |
516 setPiece(3, getPiece(0)); |
516 return false; |
517 setPiece(0, new Piece(null, null, EMPTY, 0)); |
517 } |
518 return true; |
518 |
519 } |
519 /** |
520 |
520 * checks, whether color is in check or not. |
521 return false; |
521 */ |
522 } |
522 public boolean isCheck(String color) { |
523 |
523 int i; |
524 /** |
524 int wking; |
525 * checks, whether color is in check or not. |
525 int bking; |
526 */ |
526 |
527 public boolean isCheck(String color) { |
527 wking = bking = -1; |
528 int i; |
528 |
529 int wking; |
529 for (i = 0; i < 64; i++) { |
530 int bking; |
530 if (isWhiteKing(i)) { |
531 |
531 wking = i; |
532 wking = bking = -1; |
532 } |
533 |
533 if (isBlackKing(i)) { |
534 for (i = 0; i < 64; i++) { |
534 bking = i; |
535 if (isWhiteKing(i)) { |
535 } |
536 wking = i; |
536 } |
537 } |
537 |
538 if (isBlackKing(i)) { |
538 // returns 64 false positives |
539 bking = i; |
539 if (wking == -1 || bking == -1) |
540 } |
540 return true; |
541 } |
541 |
542 |
542 if (color.equals("white")) { |
543 // returns 64 false positives |
543 for (i = 0; i < 64; i++) |
544 if (wking == -1 || bking == -1) |
544 if (validMove(wking, i)) { |
545 return true; |
545 return true; |
546 |
546 } |
547 if (color.equals("white")) { |
547 |
548 for (i = 0; i < 64; i++) |
548 return false; |
549 if (validMove(wking, i)) { |
549 } |
550 return true; |
550 |
551 } |
551 if (color.equals("black")) { |
552 |
552 for (i = 0; i < 64; i++) |
553 return false; |
553 if (validMove(bking, i)) { |
554 } |
554 return true; |
555 |
555 } |
556 if (color.equals("black")) { |
556 |
557 for (i = 0; i < 64; i++) |
557 return false; |
558 if (validMove(bking, i)) { |
558 } |
559 return true; |
559 return false; |
560 } |
560 } |
561 |
561 |
562 return false; |
562 /** |
563 } |
563 * simulates a "valid" move or returns false. all changes are relative to |
564 return false; |
564 * pieces and will not be painted. This method sets internal variables for |
565 } |
565 * the rochade() method, which will be overwritten. |
566 |
566 */ |
567 /** |
567 public boolean simulateMove(int t, int o) { |
568 * simulates a "valid" move or returns false. all changes are relative to |
568 if (!validMove(t, o)) { |
569 * pieces and will not be painted. This method sets internal variables for |
569 return false; |
570 * the rochade() method, which will be overwritten. |
570 } |
571 */ |
571 |
572 public boolean simulateMove(int t, int o) { |
572 push(getPiece(t), t, getPiece(o), o); |
573 if (!validMove(t, o)) { |
573 |
574 return false; |
574 if (getType(o) == PAWN) { |
575 } |
575 if (isBlack(o) && t >= 56) |
576 |
576 setPiece(o, new Piece(black_queen)); |
577 push(getPiece(t), t, getPiece(o), o); |
577 |
578 |
578 if (isWhite(o) && t <= 7) |
579 if (getType(o) == PAWN) { |
579 setPiece(o, new Piece(white_queen)); |
580 if (isBlack(o) && t >= 56) |
580 } |
581 setPiece(o, new Piece(black_queen)); |
581 |
582 |
582 setPiece(t, new Piece(getPiece(o))); |
583 if (isWhite(o) && t <= 7) |
583 setPiece(o, new Piece(null, null, EMPTY, 0)); |
584 setPiece(o, new Piece(white_queen)); |
584 |
585 } |
585 if (isCheck(getColor(t))) { |
586 |
586 /* restore current position */ |
587 setPiece(t, new Piece(getPiece(o))); |
587 if (UNDO_DEBUG) |
588 setPiece(o, new Piece(null, null, EMPTY, 0)); |
588 print("UNDO", t, o, 0); |
589 |
589 undo(); |
590 if (isCheck(getColor(t))) { |
590 return false; |
591 /* restore current position */ |
591 } |
592 if (UNDO_DEBUG) |
592 |
593 print("UNDO", t, o, 0); |
593 return true; |
594 undo(); |
594 } |
595 return false; |
595 |
596 } |
596 /** |
597 |
597 * performs a "valid" move or returns false. all changes are relative to |
598 return true; |
598 * board_pieces and will be painted. |
599 } |
599 */ |
600 |
600 public boolean doMove(int t, int o) { |
601 /** |
601 if (!validMove(t, o)) { |
602 * performs a "valid" move or returns false. all changes are relative to |
602 return false; |
603 * board_pieces and will be painted. |
603 } |
604 */ |
604 |
605 public boolean doMove(int t, int o) { |
605 push(getPiece(t), t, getPiece(o), o); |
606 if (!validMove(t, o)) { |
606 |
607 return false; |
607 if (getType(o) == PAWN) { |
608 } |
608 if (isBlack(o) && t >= 56) |
609 |
609 setPiece(o, new Piece(black_queen)); |
610 push(getPiece(t), t, getPiece(o), o); |
610 |
611 |
611 if (isWhite(o) && t <= 7) |
612 if (getType(o) == PAWN) { |
612 setPiece(o, new Piece(white_queen)); |
613 if (isBlack(o) && t >= 56) |
613 } |
614 setPiece(o, new Piece(black_queen)); |
614 |
615 |
615 setPiece(t, new Piece(getPiece(o))); |
616 if (isWhite(o) && t <= 7) |
616 setPiece(o, new Piece(null, null, EMPTY, 0)); |
617 setPiece(o, new Piece(white_queen)); |
617 |
618 } |
618 if (rochade()) |
619 |
619 System.out.println("Rochade"); |
620 setPiece(t, new Piece(getPiece(o))); |
620 |
621 setPiece(o, new Piece(null, null, EMPTY, 0)); |
621 if (isCheck(getColor(t))) { |
622 |
622 // restore current positions |
623 if (rochade()) |
623 if (UNDO_DEBUG) |
624 System.out.println("Rochade"); |
624 print("UNDO", t, o, 0); |
625 |
625 undo(); |
626 if (isCheck(getColor(t))) { |
626 return false; |
627 // restore current positions |
627 } |
628 if (UNDO_DEBUG) |
628 |
629 print("UNDO", t, o, 0); |
629 board_pieces = getPieces(); |
630 undo(); |
630 repaint(); |
631 return false; |
631 |
632 } |
632 moveNr = stack.size(); |
633 |
633 |
634 board_pieces = getPieces(); |
634 return true; |
635 repaint(); |
635 } |
636 |
636 |
637 moveNr = stack.size(); |
637 /** |
638 |
638 * converts the internal representation into normal chess notation and |
639 return true; |
639 * writes it to stdout |
640 } |
640 */ |
641 |
641 public void print(String s, int t, int o, int value) { |
642 /** |
642 int row1, col1, row2, col2; |
643 * converts the internal representation into normal chess notation and |
643 |
644 * writes it to stdout |
644 row1 = o / 8; |
645 */ |
645 col1 = o - (8 * row1); |
646 public void print(String s, int t, int o, int value) { |
646 |
647 int row1, col1, row2, col2; |
647 row2 = t / 8; |
648 |
648 col2 = t - (8 * row2); |
649 row1 = o / 8; |
649 |
650 col1 = o - (8 * row1); |
650 System.out.printf("%s: %C%d-%c%d = %3d%n", s, ('A') + col1, 8 - row1, ('A') + col2, 8 - row2, value); |
651 |
651 } |
652 row2 = t / 8; |
|
653 col2 = t - (8 * row2); |
|
654 |
|
655 System.out.printf("%s: %C%d-%c%d = %3d%n", s, ('A') + col1, 8 - row1, ('A') + col2, 8 - row2, value); |
|
656 } |
|
657 } |
652 } |