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 * This is the superclass of move implementations for various wild chess variants. 27 */ 28 29 public abstract class Move{ 30 31 32 /** 33 * The square on which the moving piece was standing before the move. 34 */ 35 36 protected final Square startingSquare; 37 38 39 40 /** 41 * The square to which the moving piece moved. 42 */ 43 44 protected final Square endingSquare; 45 46 47 48 /** 49 * The player making the move. 50 */ 51 52 protected final Player player; 53 54 55 56 /** 57 * The string representation of this move. This may be null. 58 */ 59 60 private final String stringRepresentation; 61 62 63 64 65 /** 66 * Creates a new Move from the given starting Square to the given ending 67 * Square, with the given moving Player. 68 * <P>Both the starting and ending squares may be null, as they sometimes are, 69 * for example in Kriegspiel. 70 * <P>The given stringRepresentation will be the string returned from the 71 * toString() method. It may be <code>null</code>, in which case a string 72 * constructed from the move data will be returned by toString(). 73 */ 74 Move(Square startingSquare, Square endingSquare, Player player, String stringRepresentation)75 public Move(Square startingSquare, Square endingSquare, Player player, 76 String stringRepresentation){ 77 78 if (player == null) 79 throw new IllegalArgumentException("The moving player may not be null"); 80 81 this.startingSquare = startingSquare; 82 this.endingSquare = endingSquare; 83 this.player = player; 84 this.stringRepresentation = stringRepresentation; 85 } 86 87 88 89 90 /** 91 * Returns the initial square of the moving piece or <code>null</code> if 92 * unknown. 93 */ 94 getStartingSquare()95 public Square getStartingSquare(){ 96 return startingSquare; 97 } 98 99 100 101 /** 102 * Returns the square to which the moving piece moved or <code>null</code> if 103 * unknown 104 */ 105 getEndingSquare()106 public Square getEndingSquare(){ 107 return endingSquare; 108 } 109 110 111 112 /** 113 * Returns the Player making the move. 114 */ 115 getPlayer()116 public Player getPlayer(){ 117 return player; 118 } 119 120 121 122 /** 123 * Returns the string representation of the move passed in the constructor. 124 * This may be null. 125 */ 126 getStringRepresentation()127 public final String getStringRepresentation(){ 128 return stringRepresentation; 129 } 130 131 132 133 /** 134 * Returns a string representation of the move based on the move data. 135 */ 136 getMoveString()137 public String getMoveString(){ 138 Square startingSquare = getStartingSquare(); 139 Square endingSquare = getEndingSquare(); 140 141 String startingString = startingSquare == null ? "??" : startingSquare.toString(); 142 String endingString = endingSquare == null ? "??" : endingSquare.toString(); 143 144 return startingString + endingString; 145 } 146 147 148 149 /** 150 * Breaks the given move string, represented in Smith Warren format into 151 * starting square, ending square and promotion target, and using 152 * WildVariant.parsePiece(String) and 153 * WildVariant.createMove(Position, Square, Square, Piece) creates a Move 154 * object. 155 */ 156 parseWarrenSmith(String moveSmith, Position position, String moveString)157 public static Move parseWarrenSmith(String moveSmith, Position position, String moveString){ 158 WildVariant variant = position.getVariant(); 159 160 char lastChar = moveSmith.charAt(moveSmith.length() - 1); 161 if (lastChar == 'c') // Short castling 162 return variant.createShortCastling(position); 163 else if (lastChar == 'C') // Long castling 164 return variant.createLongCastling(position); 165 166 Square startSquare = Square.parseSquare(moveSmith.substring(0, 2)); 167 Square endSquare = Square.parseSquare(moveSmith.substring(2, 4)); 168 169 Piece promotionTarget = null; 170 if ("NBRQK".indexOf(lastChar) != -1){ 171 // The 'K' can happen in Giveaway, where you can promote to a king 172 String promotionTargetString = String.valueOf(moveSmith.charAt(moveSmith.length() - 1)); 173 if (position.getCurrentPlayer().isBlack()) 174 promotionTargetString = promotionTargetString.toLowerCase(); 175 promotionTarget = variant.parsePiece(promotionTargetString); 176 } 177 178 return variant.createMove(position, startSquare, endSquare, promotionTarget, moveString); 179 } 180 181 182 183 184 /** 185 * Returns the string representation of this Move. If the value returned by 186 * {@link #getStringRepresentation()} is not null, that is returned. Otherwise 187 * the value of getMoveString() is returned. 188 */ 189 toString()190 public String toString(){ 191 if (getStringRepresentation()==null) 192 return getMoveString(); 193 else 194 return getStringRepresentation(); 195 } 196 197 198 199 } 200