|
1 /** |
|
2 * $Id: Board.java 148 2008-04-25 22:05:48Z mbroeker $ |
|
3 * $URL: http://localhost/svn/eclipse/Schachspiel/trunk/org/homelinux/largo/games/board/Board.java $ |
|
4 */ |
|
5 |
|
6 package org.homelinux.largo.games.board; |
|
7 |
|
8 import java.awt.Canvas; |
|
9 import java.awt.Color; |
|
10 import java.awt.Dimension; |
|
11 import java.awt.Graphics; |
|
12 import java.awt.Image; |
|
13 import java.util.Vector; |
|
14 |
|
15 public class Board extends Canvas { |
|
16 static final long serialVersionUID = 140208; |
|
17 |
|
18 protected boolean UNDO_DEBUG = false; |
|
19 |
|
20 protected Piece board_pieces[]; |
|
21 private Piece pieces[]; |
|
22 |
|
23 protected Vector<History> stack; |
|
24 protected int moveNr; |
|
25 |
|
26 protected static final int EMPTY = 0; |
|
27 |
|
28 Color color_black; |
|
29 Color color_white; |
|
30 |
|
31 int iHeight; |
|
32 int iWidth; |
|
33 int xPos; |
|
34 int yPos; |
|
35 |
|
36 boolean black; |
|
37 boolean blacksTurn; |
|
38 |
|
39 MouseListener listener = null; |
|
40 |
|
41 /** |
|
42 * The init method initializes an empty board with a history. |
|
43 */ |
|
44 public void init() { |
|
45 int i; |
|
46 pieces = new Piece[64]; |
|
47 |
|
48 for (i = 0; i < 64; i++) { |
|
49 pieces[i] = new Piece(null, null, EMPTY, 0); |
|
50 } |
|
51 |
|
52 xPos = 0; |
|
53 yPos = 0; |
|
54 |
|
55 black = false; |
|
56 blacksTurn = false; |
|
57 board_pieces = getPieces(pieces); |
|
58 stack = new Vector<History>(); |
|
59 moveNr = 0; |
|
60 } |
|
61 |
|
62 public Board (int w, int h) { |
|
63 iWidth = w; |
|
64 iHeight = h; |
|
65 color_white = new Color (255, 255, 255); |
|
66 color_black = new Color (50, 100, 200); |
|
67 setPreferredSize (new Dimension(8 * iWidth, 8 * iHeight)); |
|
68 |
|
69 init(); |
|
70 listener = new MouseListener(iWidth, iHeight); |
|
71 addMouseListener(listener); |
|
72 } |
|
73 |
|
74 /** |
|
75 * getPiece returns a Piece-Object from the Board Cache. It might not be visible on the current board. |
|
76 */ |
|
77 public Piece getPiece(int t) { |
|
78 return pieces[t]; |
|
79 } |
|
80 |
|
81 /** |
|
82 * setPiece sets a Piece-Object into the Board Cache and will not be visible. |
|
83 */ |
|
84 protected void setPiece(int t, Piece p) { |
|
85 pieces[t] = p; |
|
86 } |
|
87 |
|
88 protected Piece[] getPieces() { |
|
89 return getPieces(pieces); |
|
90 } |
|
91 |
|
92 private Piece[] getPieces(Piece[] which_pieces) { |
|
93 Piece[] p = new Piece[64]; |
|
94 int i; |
|
95 |
|
96 for ( i=0;i<64;i++) |
|
97 p[i] = new Piece(which_pieces[i]); |
|
98 |
|
99 return p; |
|
100 } |
|
101 |
|
102 protected void setPieces(Piece[] p) { |
|
103 pieces = p; |
|
104 } |
|
105 |
|
106 /** |
|
107 * needed in paint(Graphics g) |
|
108 */ |
|
109 private Image getBoardImage (int i) { |
|
110 return board_pieces[i].image; |
|
111 } |
|
112 |
|
113 /** |
|
114 * getMouseListener returns a valid MouseListener for this Board. |
|
115 */ |
|
116 public MouseListener getMouseListener() { |
|
117 return listener; |
|
118 } |
|
119 |
|
120 /** |
|
121 * currentMove returns the current stack.size(). |
|
122 */ |
|
123 public int currentMove() { |
|
124 return stack.size(); |
|
125 } |
|
126 |
|
127 /** |
|
128 * returns the color of piece[i]. |
|
129 */ |
|
130 public String getColor(int i) { |
|
131 return pieces[i].color; |
|
132 } |
|
133 |
|
134 /** |
|
135 * returns the current Value of a particular piece. |
|
136 */ |
|
137 public int getValue(int i) { |
|
138 return pieces[i].value; |
|
139 } |
|
140 |
|
141 /** |
|
142 * Defined Integer Constants: EMTPY = 0 |
|
143 */ |
|
144 public int getType(int i) { |
|
145 return pieces[i].type; |
|
146 } |
|
147 |
|
148 public boolean isEmpty (int i) { |
|
149 if (pieces[i].type == EMPTY) |
|
150 return true; |
|
151 return false; |
|
152 } |
|
153 |
|
154 boolean BoardisEmpty (int i) { |
|
155 if (board_pieces[i].type == EMPTY) |
|
156 return true; |
|
157 return false; |
|
158 } |
|
159 |
|
160 /** |
|
161 * blacksTurn = true -> Black moves |
|
162 * blacksTurn = false -> White moves |
|
163 */ |
|
164 public boolean isBlacksTurn() { |
|
165 return blacksTurn; |
|
166 } |
|
167 |
|
168 public void setBlacksTurn(boolean b) { |
|
169 blacksTurn = b; |
|
170 } |
|
171 |
|
172 /** |
|
173 * returns true, when piece[i] is white |
|
174 */ |
|
175 public boolean isWhite(int i) { |
|
176 if ( isEmpty(i) ) |
|
177 return false; |
|
178 |
|
179 if ( pieces[i].color.equals ("white") ) |
|
180 return true; |
|
181 return false; |
|
182 } |
|
183 |
|
184 /** |
|
185 * returns true, when piece[i] is black |
|
186 */ |
|
187 public boolean isBlack(int i) { |
|
188 if ( isEmpty(i) ) |
|
189 return false; |
|
190 |
|
191 if ( pieces[i].color.equals ("black") ) |
|
192 return true; |
|
193 return false; |
|
194 } |
|
195 |
|
196 /** |
|
197 * returns true, when both pieces have opposite colors |
|
198 */ |
|
199 public boolean isEnemy(int t, int o) { |
|
200 if ( isEmpty(t) ) |
|
201 return false; |
|
202 |
|
203 if (!pieces[t].color.equals (pieces[o].color) ) |
|
204 return true; |
|
205 return false; |
|
206 } |
|
207 |
|
208 /** |
|
209 * returns true, when both pieces have the same color |
|
210 */ |
|
211 public boolean isSamePiece(int t, int o) { |
|
212 if ( isEmpty(t) ) |
|
213 return false; |
|
214 |
|
215 if (pieces[t].color.equals (pieces[o].color) ) |
|
216 return true; |
|
217 return false; |
|
218 } |
|
219 |
|
220 /** |
|
221 * This function checks for the color of the piece at piece[i] and returns true, |
|
222 * if the color matches the current Turn. |
|
223 */ |
|
224 public boolean validTurn(int i) { |
|
225 if ( isEmpty(i) ) |
|
226 return false; |
|
227 |
|
228 if ( getColor(i).equals("white") && isBlacksTurn() ) |
|
229 return false; |
|
230 if ( getColor(i).equals("black") && !isBlacksTurn() ) |
|
231 return false; |
|
232 return true; |
|
233 } |
|
234 |
|
235 /** |
|
236 * simulates a "valid" move or returns false. |
|
237 * all changes are relative to pieces and will not be painted. |
|
238 * This method sets internal variables for the rochade() method, which will be overidden. |
|
239 */ |
|
240 public boolean simulateMove(int t, int o) { |
|
241 if ( !validMove(t, o) ) { |
|
242 return false; |
|
243 } |
|
244 |
|
245 push(pieces[t], t, pieces[o], o); |
|
246 |
|
247 pieces[t] = new Piece(pieces[o]); |
|
248 pieces[o] = new Piece(null, null, EMPTY, 0); |
|
249 |
|
250 return true; |
|
251 } |
|
252 |
|
253 /** |
|
254 * Moves a Piece on the Board at Point p |
|
255 */ |
|
256 public boolean move (Point p) { |
|
257 int t; |
|
258 int o; |
|
259 |
|
260 t = (p.endy * 8) + p.endx; |
|
261 o = (p.starty * 8) + p.startx; |
|
262 |
|
263 if ( t < 0 || t > 63 ) |
|
264 return false; |
|
265 if ( o < 0 || o > 63 ) |
|
266 return false; |
|
267 |
|
268 if (validTurn(o)) |
|
269 return doMove (t, o); |
|
270 |
|
271 System.out.println("It's not your turn!"); |
|
272 return false; |
|
273 } |
|
274 |
|
275 /** |
|
276 * Moves one half-move backward. |
|
277 */ |
|
278 public void backwards() { |
|
279 History h1 = null; |
|
280 History h2 = null; |
|
281 |
|
282 if ( moveNr < 2) |
|
283 return; |
|
284 |
|
285 moveNr -= 2; |
|
286 |
|
287 h1 = stack.elementAt(moveNr); |
|
288 h2 = stack.elementAt(moveNr+1); |
|
289 |
|
290 board_pieces[h1.pos] = h1.piece; |
|
291 board_pieces[h2.pos] = h2.piece; |
|
292 |
|
293 repaint(); |
|
294 } |
|
295 |
|
296 /** |
|
297 * Moves one half-move forward. |
|
298 */ |
|
299 public void forward() { |
|
300 History h1 = null; |
|
301 History h2 = null; |
|
302 |
|
303 if ( moveNr > stack.size()-2 ) |
|
304 return; |
|
305 |
|
306 h1 = stack.elementAt(moveNr); |
|
307 h2 = stack.elementAt(moveNr+1); |
|
308 |
|
309 board_pieces[h1.pos] = h2.piece; |
|
310 board_pieces[h2.pos] = new Piece(null, null, EMPTY, 0); |
|
311 |
|
312 moveNr += 2; |
|
313 repaint(); |
|
314 } |
|
315 |
|
316 /** |
|
317 * pushes 2 pieces onto the stack. |
|
318 */ |
|
319 public void push(Piece p1, int t, Piece p2, int o) { |
|
320 stack.add(new History(p1, t, isBlacksTurn())); |
|
321 stack.add(new History(p2, o, isBlacksTurn())); |
|
322 } |
|
323 |
|
324 /** |
|
325 * pop: undo the push operation. |
|
326 */ |
|
327 public void undo() { |
|
328 History h1 = null; |
|
329 History h2 = null; |
|
330 int size = stack.size(); |
|
331 |
|
332 if ( size < 2 ) |
|
333 return; |
|
334 |
|
335 h1 = stack.elementAt(size-1); |
|
336 h2 = stack.elementAt(size-2); |
|
337 |
|
338 pieces[h1.pos] = h1.piece; |
|
339 pieces[h2.pos] = h2.piece; |
|
340 |
|
341 stack.setSize(size-2); |
|
342 |
|
343 /* Reset the current player */ |
|
344 setBlacksTurn(h2.turn); |
|
345 } |
|
346 |
|
347 /** |
|
348 * This method must be implemented in your "GameBoard" |
|
349 * Return value: TRUE: the move IS possible |
|
350 * Return value: FALSE: the move IS NOT possible |
|
351 */ |
|
352 public boolean validMove(int t, int o) { |
|
353 System.out.println("public boolean validMove(int t, int o): Implement me"); |
|
354 return false; |
|
355 } |
|
356 |
|
357 /** |
|
358 * This method must be implemented in your "GameBoard" |
|
359 * Return value: TRUE: the move was performed |
|
360 * Return value: FALSE: the move was not performed |
|
361 */ |
|
362 public boolean doMove(int t, int o) { |
|
363 System.out.println("public boolean doMove(int t, int o): Implement me"); |
|
364 return false; |
|
365 } |
|
366 |
|
367 public void update(Graphics g){ |
|
368 paint(g); |
|
369 } |
|
370 |
|
371 public void paint (Graphics g) { |
|
372 for (int i = 0; i < 8; i++) { |
|
373 for (int j = 0; j < 8; j++) { |
|
374 if (black == true) { |
|
375 g.setColor (color_black); |
|
376 g.fillRect (xPos, yPos, iWidth, iHeight); |
|
377 black = false; |
|
378 } else { |
|
379 g.setColor (color_white); |
|
380 g.fillRect (xPos, yPos, iWidth, iHeight); |
|
381 black = true; |
|
382 } |
|
383 if ( !BoardisEmpty ((i * 8) + j) ) |
|
384 g.drawImage (getBoardImage ((i * 8) + j), xPos + 5, yPos, this); |
|
385 xPos += iWidth; |
|
386 } |
|
387 xPos = 0; |
|
388 yPos += iHeight; |
|
389 black = !black; |
|
390 } |
|
391 xPos = 0; |
|
392 yPos = 0; |
|
393 } |
|
394 } |