1 /**
2  * The chess framework library.
3  * More information is available at http://www.jinchess.com/.
4  * Copyright (C) 2002 Alexander Maryanovsky.
5  * All rights reserved.
6  *
7  * The chess framework library is free software; you can redistribute
8  * it and/or modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * The chess framework library is distributed in the hope that it will
13  * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with the chess framework library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 
22 package free.chess;
23 
24 
25 /**
26  * Represents a "soldier" in the game of chess - a chess piece.
27  * This class does not allow to create instances of itself, instead it
28  * defines constants that represent each chess piece. This means that two
29  * Pieces are the same piece only if they reference the same object.
30  * An empty square is represented by null.
31  */
32 
33 public class ChessPiece extends Piece{
34 
35 
36 
37   /**
38    * A constant representing a pawn.
39    */
40 
41   protected static final int PAWN = 1;
42 
43 
44 
45   /**
46    * A constant representing a knight.
47    */
48 
49   protected static final int KNIGHT = 2;
50 
51 
52 
53 
54   /**
55    * A constant representing a bishop.
56    */
57 
58   protected static final int BISHOP = 3;
59 
60 
61 
62   /**
63    * A constant representing a rook.
64    */
65 
66   protected static final int ROOK = 4;
67 
68 
69 
70   /**
71    * A constant representing a queen.
72    */
73 
74   protected static final int QUEEN = 5;
75 
76 
77 
78   /**
79    * A constant representing a king.
80    */
81 
82   protected static final int KING = 6;
83 
84 
85 
86 
87   // Final objects representing each piece.
88 
89   public static final ChessPiece WHITE_PAWN   = new ChessPiece(WHITE,PAWN);
90   public static final ChessPiece WHITE_KNIGHT = new ChessPiece(WHITE,KNIGHT);
91   public static final ChessPiece WHITE_BISHOP = new ChessPiece(WHITE,BISHOP);
92   public static final ChessPiece WHITE_ROOK   = new ChessPiece(WHITE,ROOK);
93   public static final ChessPiece WHITE_QUEEN  = new ChessPiece(WHITE,QUEEN);
94   public static final ChessPiece WHITE_KING   = new ChessPiece(WHITE,KING);
95 
96   public static final ChessPiece BLACK_PAWN   = new ChessPiece(BLACK,PAWN);
97   public static final ChessPiece BLACK_KNIGHT = new ChessPiece(BLACK,KNIGHT);
98   public static final ChessPiece BLACK_BISHOP = new ChessPiece(BLACK,BISHOP);
99   public static final ChessPiece BLACK_ROOK   = new ChessPiece(BLACK,ROOK);
100   public static final ChessPiece BLACK_QUEEN  = new ChessPiece(BLACK,QUEEN);
101   public static final ChessPiece BLACK_KING   = new ChessPiece(BLACK,KING);
102 
103 
104 
105 
106 
107   /**
108    * Creates a ChessPiece of the given color and type.
109    * Possible values for both type and color are defined in this class.
110    *
111    * @param color The color of the piece, either Piece.WHITE or Piece.BLACK
112    * @param type The type of the piece (Piece.KNIGHT, Piece.BISHOP etc.)
113    */
114 
ChessPiece(int color, int type)115   private ChessPiece(int color, int type){
116     super(color, type);
117   }
118 
119 
120 
121 
122   /**
123    * Returns the ChessPiece corresponding to the given string. The string is expected
124    * to be in the format returned by the {@link #toShortString()} method.
125    */
126 
fromShortString(String piece)127   public static ChessPiece fromShortString(String piece){
128     if (piece.length()!=1)
129       throw new IllegalArgumentException("The given string must be exactly one character long.");
130 
131     char c = piece.charAt(0);
132 
133     switch(c){
134      case '-': return null;
135      case 'P': return ChessPiece.WHITE_PAWN;
136      case 'N': return ChessPiece.WHITE_KNIGHT;
137      case 'B': return ChessPiece.WHITE_BISHOP;
138      case 'R': return ChessPiece.WHITE_ROOK;
139      case 'Q': return ChessPiece.WHITE_QUEEN;
140      case 'K': return ChessPiece.WHITE_KING;
141      case 'p': return ChessPiece.BLACK_PAWN;
142      case 'n': return ChessPiece.BLACK_KNIGHT;
143      case 'b': return ChessPiece.BLACK_BISHOP;
144      case 'r': return ChessPiece.BLACK_ROOK;
145      case 'q': return ChessPiece.BLACK_QUEEN;
146      case 'k': return ChessPiece.BLACK_KING;
147     }
148 
149     throw new IllegalArgumentException("Unknown piece: "+piece);
150   }
151 
152 
153 
154 
155   /**
156    * Returns <code>true</code> if this chess piece is of the same color as the
157    * given piece.
158    */
159 
isSameColorAs(ChessPiece otherPiece)160   public final boolean isSameColorAs(ChessPiece otherPiece){
161     return this.val*otherPiece.val>0;
162   }
163 
164 
165 
166 
167   /**
168    * Returns <code>true</code> if this chess piece is of the same type (rook,
169    * queen, etc.) as the given piece.
170    */
171 
isSameTypeAs(ChessPiece otherPiece)172   public final boolean isSameTypeAs(ChessPiece otherPiece){
173     return Math.abs(this.val)==Math.abs(otherPiece.val);
174   }
175 
176 
177 
178 
179   /**
180    * Returns true if this Piece is a pawn, returns false otherwise.
181    */
182 
isPawn()183   public final boolean isPawn(){
184     return (this==WHITE_PAWN)||(this==BLACK_PAWN);
185   }
186 
187 
188 
189 
190 
191 
192   /**
193    * Returns true if this Piece is a knight, returns false otherwise.
194    */
195 
isKnight()196   public final boolean isKnight(){
197     return (this==WHITE_KNIGHT)||(this==BLACK_KNIGHT);
198   }
199 
200 
201 
202 
203 
204 
205   /**
206    * Returns true if this Piece is a bishop, returns false otherwise.
207    */
208 
isBishop()209   public final boolean isBishop(){
210     return (this==WHITE_BISHOP)||(this==BLACK_BISHOP);
211   }
212 
213 
214 
215 
216 
217 
218   /**
219    * Returns true if this Piece is a rook, returns false otherwise.
220    */
221 
isRook()222   public final boolean isRook(){
223     return (this==WHITE_ROOK)||(this==BLACK_ROOK);
224   }
225 
226 
227 
228 
229 
230   /**
231    * Returns true if this Piece is a queen, returns false otherwise.
232    */
233 
isQueen()234   public final boolean isQueen(){
235     return (this==WHITE_QUEEN)||(this==BLACK_QUEEN);
236   }
237 
238 
239 
240 
241 
242 
243   /**
244    * Returns true if this Piece is a king, returns false otherwise.
245    */
246 
isKing()247   public final boolean isKing(){
248     return (this==WHITE_KING)||(this==BLACK_KING);
249   }
250 
251 
252 
253 
254 
255   /**
256    * Returns a short (notational) string representing this chess piece ("N"
257    * for a white knight for example, and "P" for a black pawn).
258    *
259    * @return a short string representing this piece.
260    *
261    * @see #fromShortString(String)
262    */
263 
toShortString()264   public String toShortString(){
265     if (isPawn())
266       return "P";
267     else if (isKnight())
268       return "N";
269     else if (isBishop())
270       return "B";
271     else if (isRook())
272       return "R";
273     else if (isQueen())
274       return "Q";
275     else if (isKing())
276       return "K";
277 
278     throw new Error("This may never happen");
279   }
280 
281 
282 
283 
284 
285   /**
286    * Returns a string representing the type of this piece ("Knight" for a
287    * knight for example).
288    */
289 
getTypeName()290   public String getTypeName(){
291     if (isPawn())
292       return "Pawn";
293     else if (isKnight())
294       return "Knight";
295     else if (isBishop())
296       return "Bishop";
297     else if (isRook())
298       return "Rook";
299     else if (isQueen())
300       return "Queen";
301     else if (isKing())
302       return "King";
303 
304     throw new Error("This may never happen");
305   }
306 
307 
308 
309 }
310