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