11 import org.homelinux.largo.games.board.History; |
11 import org.homelinux.largo.games.board.History; |
12 import org.homelinux.largo.games.board.Piece; |
12 import org.homelinux.largo.games.board.Piece; |
13 import org.homelinux.largo.utils.ImgComponent; |
13 import org.homelinux.largo.utils.ImgComponent; |
14 |
14 |
15 public class CheckersBoard extends Board { |
15 public class CheckersBoard extends Board { |
16 static final long serialVersionUID = 250408; |
16 static final long serialVersionUID = 250408; |
17 |
17 |
18 static final int PIECE = 1; |
18 static final int PIECE = 1; |
19 static final int QUEEN = 2; |
19 static final int QUEEN = 2; |
20 |
20 |
21 final Piece white_super_sun = |
21 final Piece white_super_sun = new Piece(new ImgComponent("images/white_super_sun.png").getImage(), "white", QUEEN, |
22 new Piece(new ImgComponent("images/white_super_sun.png").getImage(), "white", QUEEN, 50); |
22 50); |
23 |
23 |
24 final Piece black_super_sun = |
24 final Piece black_super_sun = new Piece(new ImgComponent("images/black_super_sun.png").getImage(), "black", QUEEN, |
25 new Piece(new ImgComponent("images/black_super_sun.png").getImage(), "black", QUEEN, 50); |
25 50); |
26 |
26 |
27 void initBoard () { |
27 void initBoard() { |
28 for (int i=1;i<8;i+=2) |
28 for (int i = 1; i < 8; i += 2) |
29 add (new ImgComponent ("images/black_sun.png").getImage (), i, "black", PIECE); |
29 add(new ImgComponent("images/black_sun.png").getImage(), i, "black", PIECE); |
30 for (int i=8;i<15;i+=2) |
30 for (int i = 8; i < 15; i += 2) |
31 add (new ImgComponent ("images/black_sun.png").getImage (), i, "black", PIECE); |
31 add(new ImgComponent("images/black_sun.png").getImage(), i, "black", PIECE); |
32 for (int i=17;i<=23;i+=2) |
32 for (int i = 17; i <= 23; i += 2) |
33 add (new ImgComponent ("images/black_sun.png").getImage (), i, "black", PIECE); |
33 add(new ImgComponent("images/black_sun.png").getImage(), i, "black", PIECE); |
34 |
34 |
35 for (int i=40;i<47;i+=2) |
35 for (int i = 40; i < 47; i += 2) |
36 add (new ImgComponent ("images/white_sun.png").getImage (), i, "white", PIECE); |
36 add(new ImgComponent("images/white_sun.png").getImage(), i, "white", PIECE); |
37 for (int i=49;i<=55;i+=2) |
37 for (int i = 49; i <= 55; i += 2) |
38 add (new ImgComponent ("images/white_sun.png").getImage (), i, "white", PIECE); |
38 add(new ImgComponent("images/white_sun.png").getImage(), i, "white", PIECE); |
39 for (int i=56;i<63;i+=2) |
39 for (int i = 56; i < 63; i += 2) |
40 add (new ImgComponent ("images/white_sun.png").getImage (), i, "white", PIECE); |
40 add(new ImgComponent("images/white_sun.png").getImage(), i, "white", PIECE); |
41 } |
41 } |
42 |
42 |
43 /** |
43 /** |
44 * The init method initializes a complete checkers board with figures and a history. |
44 * The init method initializes a complete checkers board with figures and a |
45 */ |
45 * history. |
46 public void init() { |
46 */ |
47 super.init(); |
47 public void init() { |
48 initBoard(); |
48 super.init(); |
49 board_pieces = getPieces(); |
49 initBoard(); |
50 repaint(); |
50 board_pieces = getPieces(); |
51 } |
51 repaint(); |
52 |
52 } |
53 public CheckersBoard (int w, int h) { |
53 |
54 super(w, h); |
54 public CheckersBoard(int w, int h) { |
55 } |
55 super(w, h); |
56 |
56 } |
57 void add (Image img, int i, String c, int t) { |
57 |
58 int v = 0; |
58 void add(Image img, int i, String c, int t) { |
59 |
59 int v = 0; |
60 switch(t) { |
60 |
61 case PIECE: |
61 switch (t) { |
62 v=10; |
62 case PIECE: |
63 } |
63 v = 10; |
64 |
64 } |
65 setPiece(i, new Piece(img, c, t, v)); |
65 |
66 } |
66 setPiece(i, new Piece(img, c, t, v)); |
67 |
67 } |
68 /** |
68 |
69 * Checks, wether this move is possible or not. |
69 /** |
70 */ |
70 * Checks, wether this move is possible or not. |
71 public boolean validMove (int t, int o) { |
71 */ |
72 int steps; |
72 public boolean validMove(int t, int o) { |
73 int rows; |
73 int steps; |
74 |
74 int rows; |
75 if ( t == o ) |
75 |
76 return false; |
76 if (t == o) |
77 |
77 return false; |
78 if (isSamePiece(t, o)) |
78 |
79 return false; |
79 if (isSamePiece(t, o)) |
80 |
80 return false; |
81 if (getType(o) == EMPTY) |
81 |
82 return false; |
82 if (getType(o) == EMPTY) |
83 |
83 return false; |
84 /* |
84 |
85 * 00 01 02 03 04 05 06 07 |
85 /* |
86 * 08 09 10 11 12 13 14 15 |
86 * 00 01 02 03 04 05 06 07 |
87 * 16 17 18 19 20 21 22 23 |
87 * 08 09 10 11 12 13 14 15 |
88 * 24 25 26 27 28 29 30 31 |
88 * 16 17 18 19 20 21 22 23 |
89 * 32 33 34 35 36 37 38 39 |
89 * 24 25 26 27 28 29 30 31 |
90 * 40 41 42 43 44 45 46 47 |
90 * 32 33 34 35 36 37 38 39 |
91 * 48 49 50 51 52 53 54 55 |
91 * 40 41 42 43 44 45 46 47 |
92 * 56 57 58 59 60 61 62 63 |
92 * 48 49 50 51 52 53 54 55 |
93 */ |
93 * 56 57 58 59 60 61 62 63 |
94 |
94 */ |
95 steps = Math.abs(t-o); |
95 |
96 rows = Math.abs(t/8 - o/8); |
96 steps = Math.abs(t - o); |
97 |
97 rows = Math.abs(t / 8 - o / 8); |
98 switch( getType(o) ) { |
98 |
99 case PIECE: |
99 switch (getType(o)) { |
100 if (steps % 7 == 0 ) { |
100 case PIECE: |
101 if (steps == 7 && rows == 1 && isEmpty(t)) { |
101 if (steps % 7 == 0) { |
102 if ( isBlack(o) && (t>o) ) |
102 if (steps == 7 && rows == 1 && isEmpty(t)) { |
103 return true; |
103 if (isBlack(o) && (t > o)) |
104 if ( isWhite(o) && (t<o) ) |
104 return true; |
105 return true; |
105 if (isWhite(o) && (t < o)) |
106 } |
106 return true; |
107 |
107 } |
108 if (steps == 14 && rows == 2 && isEmpty(t)) { |
108 |
109 if ( isBlack(o) && (t>o) && isEnemy(o+7, o) ) |
109 if (steps == 14 && rows == 2 && isEmpty(t)) { |
110 return true; |
110 if (isBlack(o) && (t > o) && isEnemy(o + 7, o)) |
111 if ( isWhite(o) && (t<o) && isEnemy(o-7, o) ) |
111 return true; |
112 return true; |
112 if (isWhite(o) && (t < o) && isEnemy(o - 7, o)) |
113 } |
113 return true; |
114 } |
114 } |
115 |
115 } |
116 if (steps % 9 == 0 ) { |
116 |
117 if (steps == 9 && rows == 1 && isEmpty(t)) { |
117 if (steps % 9 == 0) { |
118 if ( isBlack(o) && (t>o) ) |
118 if (steps == 9 && rows == 1 && isEmpty(t)) { |
119 return true; |
119 if (isBlack(o) && (t > o)) |
120 if ( isWhite(o) && (t<o) ) |
120 return true; |
121 return true; |
121 if (isWhite(o) && (t < o)) |
122 } |
122 return true; |
123 |
123 } |
124 if (steps == 18 && rows == 2 && isEmpty(t)) { |
124 |
125 if ( isBlack(o) && (t>o) && isEnemy(o+9, o) ) |
125 if (steps == 18 && rows == 2 && isEmpty(t)) { |
126 return true; |
126 if (isBlack(o) && (t > o) && isEnemy(o + 9, o)) |
127 if ( isWhite(o) && (t<o) && isEnemy(o-9, o) ) |
127 return true; |
128 return true; |
128 if (isWhite(o) && (t < o) && isEnemy(o - 9, o)) |
129 } |
129 return true; |
130 } |
130 } |
131 break; |
131 } |
132 case QUEEN: |
132 break; |
133 if (steps % 7 == 0 && isEmpty(t) ) { |
133 case QUEEN: |
134 if (steps == 7 && rows == 1) |
134 if (steps % 7 == 0 && isEmpty(t)) { |
135 return true; |
135 if (steps == 7 && rows == 1) |
136 if (steps == 14 && rows == 2) |
136 return true; |
137 return true; |
137 if (steps == 14 && rows == 2) |
138 if (steps == 21 && rows == 3) |
138 return true; |
139 return true; |
139 if (steps == 21 && rows == 3) |
140 if (steps == 28 && rows == 4) |
140 return true; |
141 return true; |
141 if (steps == 28 && rows == 4) |
142 if (steps == 35 && rows == 5) |
142 return true; |
143 return true; |
143 if (steps == 35 && rows == 5) |
144 if (steps == 42 && rows == 6) |
144 return true; |
145 return true; |
145 if (steps == 42 && rows == 6) |
146 if (steps == 49 && rows == 7) |
146 return true; |
147 return true; |
147 if (steps == 49 && rows == 7) |
148 } |
148 return true; |
149 |
149 } |
150 if (steps % 9 == 0 && isEmpty(t) ) { |
150 |
151 if (steps == 9 && rows == 1) |
151 if (steps % 9 == 0 && isEmpty(t)) { |
152 return true; |
152 if (steps == 9 && rows == 1) |
153 if (steps == 18 && rows == 2) |
153 return true; |
154 return true; |
154 if (steps == 18 && rows == 2) |
155 if (steps == 27 && rows == 3) |
155 return true; |
156 return true; |
156 if (steps == 27 && rows == 3) |
157 if (steps == 36 && rows == 4) |
157 return true; |
158 return true; |
158 if (steps == 36 && rows == 4) |
159 if (steps == 45 && rows == 5) |
159 return true; |
160 return true; |
160 if (steps == 45 && rows == 5) |
161 if (steps == 54 && rows == 6) |
161 return true; |
162 return true; |
162 if (steps == 54 && rows == 6) |
163 if (steps == 63 && rows == 7) |
163 return true; |
164 return true; |
164 if (steps == 63 && rows == 7) |
165 } |
165 return true; |
166 break; |
166 } |
167 } |
167 break; |
168 return false; |
168 } |
169 } |
169 return false; |
170 |
170 } |
171 /** |
171 |
172 * pop: undo the push operation. |
172 /** |
173 */ |
173 * pop: undo the push operation. |
174 public void undo() { |
174 */ |
175 History h1 = null; |
175 public void undo() { |
176 History h2 = null; |
176 History h1 = null; |
177 int size = stack.size(); |
177 History h2 = null; |
178 int pos; |
178 int size = stack.size(); |
179 |
179 int pos; |
180 if ( size < 2 ) |
180 |
181 return; |
181 if (size < 2) |
182 |
182 return; |
183 h1 = stack.elementAt(size-1); |
183 |
184 h2 = stack.elementAt(size-2); |
184 h1 = stack.elementAt(size - 1); |
185 |
185 h2 = stack.elementAt(size - 2); |
186 setPiece(h1.pos(), h1.piece()); |
186 |
187 setPiece(h2.pos(), h2.piece()); |
187 setPiece(h1.pos(), h1.piece()); |
188 |
188 setPiece(h2.pos(), h2.piece()); |
189 if ( getType(h2.pos()) != EMPTY ) { |
189 |
190 pos = 2*h2.pos()-h1.pos(); |
190 if (getType(h2.pos()) != EMPTY) { |
191 if ( pos >= 0 && pos <64) |
191 pos = 2 * h2.pos() - h1.pos(); |
192 setPiece(pos, new Piece(null, null, EMPTY, 0)); |
192 if (pos >= 0 && pos < 64) |
193 } |
193 setPiece(pos, new Piece(null, null, EMPTY, 0)); |
194 |
194 } |
195 stack.setSize(size-2); |
195 |
196 |
196 stack.setSize(size - 2); |
197 /* Reset the current player */ |
197 |
198 setBlacksTurn(h2.turn()); |
198 /* Reset the current player */ |
199 } |
199 setBlacksTurn(h2.turn()); |
200 |
200 } |
201 /** |
201 |
202 * simulates a "valid" move or returns false. |
202 /** |
203 * all changes are relative to pieces and will not be painted. |
203 * simulates a "valid" move or returns false. all changes are relative to |
204 */ |
204 * pieces and will not be painted. |
205 public boolean simulateMove(int t, int o) { |
205 */ |
206 int rows = Math.abs(t/8 - o/8); |
206 public boolean simulateMove(int t, int o) { |
207 int pos; |
207 int rows = Math.abs(t / 8 - o / 8); |
208 |
208 int pos; |
209 if ( !validMove(t, o) ) { |
209 |
210 return false; |
210 if (!validMove(t, o)) { |
211 } |
211 return false; |
212 |
212 } |
213 if ( rows == 2 && getType(o) == PIECE) { |
213 |
214 pos = (t-o)/2 + o; |
214 if (rows == 2 && getType(o) == PIECE) { |
215 push(getPiece(pos), pos, getPiece(o), o); |
215 pos = (t - o) / 2 + o; |
216 setPiece(pos, new Piece(null, null, EMPTY, 0)); |
216 push(getPiece(pos), pos, getPiece(o), o); |
217 } else |
217 setPiece(pos, new Piece(null, null, EMPTY, 0)); |
218 push(getPiece(t), t, getPiece(o), o); |
218 } else |
219 |
219 push(getPiece(t), t, getPiece(o), o); |
220 setPiece(t, new Piece(getPiece(o))); |
220 |
221 setPiece(o, new Piece(null, null, EMPTY, 0)); |
221 setPiece(t, new Piece(getPiece(o))); |
222 |
222 setPiece(o, new Piece(null, null, EMPTY, 0)); |
223 return true; |
223 |
224 } |
224 return true; |
225 |
225 } |
226 /** |
226 |
227 * performs a "valid" move or returns false. |
227 /** |
228 * all changes are relative to board_pieces and will be painted. |
228 * performs a "valid" move or returns false. all changes are relative to |
229 */ |
229 * board_pieces and will be painted. |
230 public boolean doMove(int t, int o) { |
230 */ |
231 int steps = Math.abs(t-o); |
231 public boolean doMove(int t, int o) { |
232 int rows = Math.abs(t/8-o/8); |
232 int steps = Math.abs(t - o); |
233 int pos; |
233 int rows = Math.abs(t / 8 - o / 8); |
234 |
234 int pos; |
235 if ( !validMove(t, o) ) { |
235 |
236 return false; |
236 if (!validMove(t, o)) { |
237 } |
237 return false; |
238 |
238 } |
239 if ( rows == 2 && getType(o) == PIECE) { |
239 |
240 pos = (t-o)/2 + o; |
240 if (rows == 2 && getType(o) == PIECE) { |
241 push(getPiece(pos), pos, getPiece(o), o); |
241 pos = (t - o) / 2 + o; |
242 setPiece(pos, new Piece(null, null, EMPTY, 0)); |
242 push(getPiece(pos), pos, getPiece(o), o); |
243 } else if ( rows >= 2 && getType(o) == QUEEN ) { |
243 setPiece(pos, new Piece(null, null, EMPTY, 0)); |
244 if (steps % 7 == 0 ) { |
244 } else if (rows >= 2 && getType(o) == QUEEN) { |
245 if ( t > o ) |
245 if (steps % 7 == 0) { |
246 for ( pos=o+7;pos <= t-7;pos+=7 ) { |
246 if (t > o) |
247 if ( isEnemy(pos, o) ) { |
247 for (pos = o + 7; pos <= t - 7; pos += 7) { |
248 push(getPiece(pos), pos, getPiece(o), o); |
248 if (isEnemy(pos, o)) { |
249 setPiece(pos, new Piece(null, null, EMPTY, 0)); |
249 push(getPiece(pos), pos, getPiece(o), o); |
250 } |
250 setPiece(pos, new Piece(null, null, EMPTY, 0)); |
251 } |
251 } |
252 else { |
252 } |
253 for ( pos=o-7;pos >= t+7;pos-=7 ) { |
253 else { |
254 if ( isEnemy(pos, o) ) { |
254 for (pos = o - 7; pos >= t + 7; pos -= 7) { |
255 push(getPiece(pos), pos, getPiece(o), o); |
255 if (isEnemy(pos, o)) { |
256 setPiece(pos, new Piece(null, null, EMPTY, 0)); |
256 push(getPiece(pos), pos, getPiece(o), o); |
257 } |
257 setPiece(pos, new Piece(null, null, EMPTY, 0)); |
258 } |
258 } |
259 } |
259 } |
260 } |
260 } |
261 |
261 } |
262 if (steps % 9 == 0 ) { |
262 |
263 if ( t > o ) |
263 if (steps % 9 == 0) { |
264 for ( pos=o+9;pos <= t-9;pos+=9 ) { |
264 if (t > o) |
265 if ( isEnemy(pos, o) ) { |
265 for (pos = o + 9; pos <= t - 9; pos += 9) { |
266 push(getPiece(pos), pos, getPiece(o), o); |
266 if (isEnemy(pos, o)) { |
267 setPiece(pos, new Piece(null, null, EMPTY, 0)); |
267 push(getPiece(pos), pos, getPiece(o), o); |
268 } |
268 setPiece(pos, new Piece(null, null, EMPTY, 0)); |
269 } |
269 } |
270 else { |
270 } |
271 for ( pos=o-9;pos >= t+9;pos-=9 ) { |
271 else { |
272 if ( isEnemy(pos, o) ) { |
272 for (pos = o - 9; pos >= t + 9; pos -= 9) { |
273 push(getPiece(pos), pos, getPiece(o), o); |
273 if (isEnemy(pos, o)) { |
274 setPiece(pos, new Piece(null, null, EMPTY, 0)); |
274 push(getPiece(pos), pos, getPiece(o), o); |
275 } |
275 setPiece(pos, new Piece(null, null, EMPTY, 0)); |
276 } |
276 } |
277 } |
277 } |
278 } |
278 } |
279 } |
279 } |
280 |
280 } |
281 if ( t <= 7 && getType(o) == PIECE) |
281 |
282 setPiece(t, new Piece(white_super_sun)); |
282 if (t <= 7 && getType(o) == PIECE) |
283 else if (t >=56 && getType(o) == PIECE) |
283 setPiece(t, new Piece(white_super_sun)); |
284 setPiece(t, new Piece(black_super_sun)); |
284 else if (t >= 56 && getType(o) == PIECE) |
285 else |
285 setPiece(t, new Piece(black_super_sun)); |
286 setPiece(t, new Piece(getPiece(o))); |
286 else |
287 |
287 setPiece(t, new Piece(getPiece(o))); |
288 setPiece(o, new Piece(null, null, EMPTY, 0)); |
288 |
289 |
289 setPiece(o, new Piece(null, null, EMPTY, 0)); |
290 board_pieces = getPieces(); |
290 |
291 repaint(); |
291 board_pieces = getPieces(); |
292 |
292 repaint(); |
293 moveNr = stack.size(); |
293 |
294 |
294 moveNr = stack.size(); |
295 return true; |
295 |
296 } |
296 return true; |
|
297 } |
297 } |
298 } |