1 /***
2 * Copyright (c) 2003 held jointly by the individual authors.
3 *
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; with out even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17 *
18 * > http://www.gnu.org/copyleft/lesser.html
19 * > http://www.opensource.org/licenses/lgpl-license.php
20 */
21 package examples.chess;
22
23 import java.awt.Image;
24 import java.awt.Toolkit;
25
26 /***
27 * @author mwilson
28 */
29 public abstract class Piece
30 {
31 public static final int WHITE = 0;
32 public static final int BLACK = 1;
33
34 private Image image;
35
36 private int color;
37
38 private char symbol;
39
40 /*** Creates a new instance of Piece */
41 private Piece(int color, char symbol)
42 {
43 this.color = color;
44 this.symbol = symbol;
45 }
46
47 public static Piece generate(final int color, final char type)
48 {
49 switch (type)
50 {
51 case Pawn.SYMBOL :
52 return new Pawn(color);
53 case King.SYMBOL :
54 return new King(color);
55 case Queen.SYMBOL :
56 return new Queen(color);
57 case Rook.SYMBOL :
58 return new Rook(color);
59 case Bishop.SYMBOL :
60 return new Bishop(color);
61 case Knight.SYMBOL :
62 return new Knight(color);
63 }
64
65 return null;
66 }
67
68 public int getColor()
69 {
70 return color;
71 }
72
73 public char getSymbol()
74 {
75 return symbol;
76 }
77
78 public Image getIcon()
79 {
80 return (image == null)
81 ? (image = Toolkit.getDefaultToolkit().getImage(Piece.class.getResource("/examples/chess/images/" + getColor() + getSymbol() + ".gif")))
82 : image;
83 }
84
85 public String toString()
86 {
87 return super.toString() + " " + getColor() + getSymbol();
88 }
89
90 public abstract void setCanMoveTo(Board board, int x, int y);
91
92 public static class Pawn extends Piece
93 {
94
95 public static final char SYMBOL = 'p';
96
97 public Pawn(int color)
98 {
99 super(color, SYMBOL);
100 }
101
102 public void setCanMoveTo(Board board, int x, int y)
103 {
104 Piece piece = board.getPiece(x, y);
105 int color = piece.getColor();
106 int step = (color == WHITE) ? 1 : -1;
107
108 if (board.getPiece(x, y + step) == null)
109 {
110 board.setCanMoveTo(x, y + step);
111 }
112
113 if (y == 1 || y == 6)
114 {
115 board.setCanMoveTo(x, y + (step * 2));
116 }
117
118 piece = board.getPiece(x - step, y + step);
119 if (piece != null && piece.getColor() != color)
120 board.setCanMoveTo(x - step, y + step);
121
122 piece = board.getPiece(x + step, y + step);
123 if (piece != null && piece.getColor() != color)
124 board.setCanMoveTo(x + step, y + step);
125 }
126 }
127
128 public static class Rook extends Piece
129 {
130
131 public static final char SYMBOL = 'r';
132
133 public Rook(int color)
134 {
135 super(color, SYMBOL);
136 }
137
138 public void setCanMoveTo(Board board, int x, int y)
139 {
140 Piece piece = board.getPiece(x, y);
141
142 int dx = 0;
143 int dy = 0;
144
145 for (dx = x + 1; board.setCanMoveTo(dx, y); dx++);
146 for (dx = x - 1; board.setCanMoveTo(dx, y); dx--);
147 for (dy = y + 1; board.setCanMoveTo(x, dy); dy++);
148 for (dy = y - 1; board.setCanMoveTo(x, dy); dy--);
149 }
150 }
151
152 public static class Knight extends Piece
153 {
154
155 public static final char SYMBOL = 'n';
156
157 public Knight(int color)
158 {
159 super(color, SYMBOL);
160 }
161
162 public void setCanMoveTo(Board board, int x, int y)
163 {
164 Piece piece = board.getPiece(x, y);
165
166 for (int dy = -2; dy < 3; dy += 4)
167 {
168 for (int dx = -1; dx < 2; dx += 2)
169 {
170 board.setCanMoveTo(x + dx, y + dy);
171 board.setCanMoveTo(x + dy, y + dx);
172 }
173 }
174 }
175 }
176
177 public static class Bishop extends Piece
178 {
179
180 public static final char SYMBOL = 'b';
181
182 public Bishop(int color)
183 {
184 super(color, SYMBOL);
185 }
186
187 public void setCanMoveTo(Board board, int x, int y)
188 {
189 Piece piece = board.getPiece(x, y);
190
191 int dx = 0;
192 int dy = 0;
193
194 for (dx = x + 1, dy = y + 1; board.setCanMoveTo(dx, dy); dx++, dy++);
195 for (dx = x - 1, dy = y + 1; board.setCanMoveTo(dx, dy); dx--, dy++);
196 for (dx = x + 1, dy = y - 1; board.setCanMoveTo(dx, dy); dx++, dy--);
197 for (dx = x - 1, dy = y - 1; board.setCanMoveTo(dx, dy); dx--, dy--);
198
199 }
200 }
201
202 public static class King extends Piece
203 {
204
205 public static final char SYMBOL = 'k';
206
207 public King(int color)
208 {
209 super(color, SYMBOL);
210 }
211
212 public void setCanMoveTo(Board board, int x, int y)
213 {
214 Piece piece = board.getPiece(x, y);
215
216 board.setCanMoveTo(x - 1, y - 1);
217 board.setCanMoveTo(x, y - 1);
218 board.setCanMoveTo(x + 1, y - 1);
219
220 board.setCanMoveTo(x - 1, y);
221 board.setCanMoveTo(x + 1, y);
222
223 board.setCanMoveTo(x - 1, y + 1);
224 board.setCanMoveTo(x, y + 1);
225 board.setCanMoveTo(x + 1, y + 1);
226
227 }
228 }
229
230 public static class Queen extends Piece
231 {
232
233 public static final char SYMBOL = 'q';
234
235 public Queen(int color)
236 {
237 super(color, SYMBOL);
238 }
239
240 public void setCanMoveTo(Board board, int x, int y)
241 {
242 Piece piece = board.getPiece(x, y);
243
244 int dx = 0;
245 int dy = 0;
246
247 for (dx = x + 1; board.setCanMoveTo(dx, y); dx++);
248 for (dx = x - 1; board.setCanMoveTo(dx, y); dx--);
249 for (dy = y + 1; board.setCanMoveTo(x, dy); dy++);
250 for (dy = y - 1; board.setCanMoveTo(x, dy); dy--);
251
252 for (dx = x + 1, dy = y + 1; board.setCanMoveTo(dx, dy); dx++, dy++);
253 for (dx = x - 1, dy = y + 1; board.setCanMoveTo(dx, dy); dx--, dy++);
254 for (dx = x + 1, dy = y - 1; board.setCanMoveTo(dx, dy); dx++, dy--);
255 for (dx = x - 1, dy = y - 1; board.setCanMoveTo(dx, dy); dx--, dy--);
256 }
257 }
258 }
This page was automatically generated by Maven