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 * The superclass of all classes representing pieces in wild chess variants. 27 */ 28 29 public abstract class Piece{ 30 31 32 /** 33 * A constant representing a white piece. 34 */ 35 36 public static final int WHITE = 1; 37 38 39 40 /** 41 * A constant representing a black piece. 42 */ 43 44 public static final int BLACK = -1; 45 46 47 48 49 /** 50 * An integer representing the piece. Negative values are black pieces and 51 * positive are white pieces. The absolute value of this variable is the 52 * type of the piece. Thus, the value of this integer is color*type; 53 */ 54 55 protected final int val; 56 57 58 59 60 /** 61 * Creates a Piece of the given color and type. 62 * 63 * @param color The color of the piece, either Piece.WHITE or Piece.BLACK 64 * @param type The type of the piece, 0 is not allowed. 65 */ 66 Piece(int color, int type)67 protected Piece(int color, int type){ 68 switch(color){ 69 case WHITE: 70 case BLACK: 71 break; 72 default: 73 throw new IllegalArgumentException("Unknown color constant: "+color); 74 } 75 if (type == 0) 76 throw new IllegalArgumentException("Piece type may not be 0"); 77 78 this.val = color*type; 79 } 80 81 82 83 84 85 /** 86 * Returns true if the given Piece has opposite color from 87 * this piece. 88 */ 89 isOppositeColor(Piece piece)90 public boolean isOppositeColor(Piece piece){ 91 return this.getColor() != piece.getColor(); 92 } 93 94 95 96 97 /** 98 * Returns trie of the given Piece has the same color as this Piece. 99 */ 100 isSameColor(Piece piece)101 public boolean isSameColor(Piece piece){ 102 return this.getColor() == piece.getColor(); 103 } 104 105 106 107 108 109 /** 110 * Returns the <code>Player</code> this piece belongs to. 111 */ 112 getPlayer()113 public Player getPlayer(){ 114 if (isWhite()) 115 return Player.WHITE_PLAYER; 116 else 117 return Player.BLACK_PLAYER; 118 } 119 120 121 122 123 124 /** 125 * Returns the color of this Piece, either {@link #BLACK} or {@link #WHITE}. 126 */ 127 getColor()128 public int getColor(){ 129 return val<0 ? BLACK : WHITE; 130 } 131 132 133 134 135 136 /** 137 * Returns true if this Piece is white, returns false otherwise. 138 */ 139 isWhite()140 public boolean isWhite(){ 141 return (val>0); 142 } 143 144 145 146 147 148 /** 149 * Returns true if this Piece is black, returns false otherwise. 150 */ 151 isBlack()152 public boolean isBlack(){ 153 return (val<0); 154 } 155 156 157 158 159 160 /** 161 * Returns a short (notational) string representing this chess piece ("N" 162 * for a knight for example). Black and white pieces should be represented 163 * by the same string. To differentiate between black and white pieces, use 164 * the <code>toShortColorString</code> method. 165 * 166 * @return a short string representing this piece. 167 */ 168 toShortString()169 public abstract String toShortString(); 170 171 172 173 174 /** 175 * Returns the same as toShortString() only lowercase for black pieces 176 * and uppercase for white ones. 177 */ 178 toShortColorString()179 public String toShortColorString(){ 180 if (isWhite()) 181 return toShortString().toUpperCase(); 182 else 183 return toShortString().toLowerCase(); 184 } 185 186 187 188 189 190 /** 191 * Returns a string representing the color of this piece, "White" for a 192 * white piece and "Black" for a black piece. Returns "Colorless" for the empty 193 * piece. 194 */ 195 getColorName()196 public String getColorName(){ 197 if (isWhite()) 198 return "White"; 199 else if (isBlack()) 200 return "Black"; 201 202 throw new Error("This may never happen"); 203 } 204 205 206 207 208 209 /** 210 * Returns a string representing the type of this piece ("Knight" for a 211 * knight for example). 212 */ 213 getTypeName()214 public abstract String getTypeName(); 215 216 217 218 219 /** 220 * Returns a string representation of this chess piece. Returns "Empty" for 221 * the empty piece. 222 */ 223 toString()224 public String toString(){ 225 return getColorName()+' '+getTypeName(); 226 } 227 228 229 230 231 /** 232 * Returns the hashcode of this Piece. 233 */ 234 hashCode()235 public int hashCode(){ 236 return val; 237 } 238 239 240 241 /** 242 * Returns true if the given Piece is the same as this one. 243 * This returns true if and only if the type (class) of this piece is the same 244 * as the type of the given piece and their "piece" values are the same. 245 * Subclasses implementing singleton pieces should override this and compare by 246 * reference. 247 */ 248 equals(Object o)249 public boolean equals(Object o){ 250 if (o == null) 251 return false; 252 253 if (o.getClass() != this.getClass()) 254 return false; 255 256 return val==((Piece)o).val; 257 } 258 259 260 } 261