/* * ChessPiece.java * * Created on October 28, 2002, 3:39 PM */ package chessThreaded; /** * * @author redmond */ public class ChessPiece { private char color = 'E'; // color of a piece W,B or Empty (E) private String piece = "EMPTY"; // any of the normal pieces plus empty /** Creates a new instance of ChessPiece */ public ChessPiece() { // just take defaults } /** * Creates a new instance of ChessPiece * if any problems, make it empty */ public ChessPiece(char col, String pc) { if (ChessPiece.validColor(col)) { color = Character.toUpperCase(col); } else { // trouble color = 'E'; } if (ChessPiece.validPieceType(pc)) { piece = pc.toUpperCase(); } // else trouble else { piece = "EMPTY"; } } /** * determine if two chess pieces are equal valued */ public boolean equals(Object obj) { ChessPiece toCompare = (ChessPiece) obj; if ((color == toCompare.getColor()) && (piece.equalsIgnoreCase(toCompare.getPiece()))) { return true; } else { return false; } } /** * make ready for presentation * an empty piece is shown as blank */ public String toString() { // handle empty position if (empty() ) { return " "; } else { // show color and first letter of piece name String res = "" + color; res += piece.charAt(0); // need to distinguish between king and knight if (piece.equals("KNIGHT")) { res += "t"; } else { res += " "; } return res; } } ///////////////////////////////////////////////////////////////// // Inspectors ////////////////////////////////////////////////////////////////// public char getColor() { return color; } public String getPiece() { return piece; } /** * report an estimated value of a piece - very rough - never beat Kasparov * with this */ public int getValue() { int res = 0; if (piece.equalsIgnoreCase("PAWN")) { res = 1; } if (piece.equalsIgnoreCase("ROOK")) { res = 5; } if (piece.equalsIgnoreCase("KNIGHT")) { res = 3; } if (piece.equalsIgnoreCase("BISHOP")) { res = 3; } if (piece.equalsIgnoreCase("QUEEN")) { res = 10; } if (piece.equalsIgnoreCase("KING")) { res = 100; } // purely for debugging - get this out of here for real runs //System.out.println(" Thread: " + Thread.currentThread().getName() + // " piece: " + this + " rating: " + res); return res; } /** * report whether a piece is really an empty space */ public boolean empty() { // executive decision - if either color or piece indicates empty - treat as empty // really ought to throw an exception if they don't agree if ((color == 'E') || (piece.equalsIgnoreCase("empty"))) { return true; } else { return false; } } /** * static method to determine if a potential piece type is a valid piece type */ public static boolean validPieceType(String pc) { if (pc.equalsIgnoreCase("pawn")) { return true; } if (pc.equalsIgnoreCase("rook")) { return true; } if (pc.equalsIgnoreCase("knight")) { return true; } if (pc.equalsIgnoreCase("bishop")) { return true; } if (pc.equalsIgnoreCase("queen")) { return true; } if (pc.equalsIgnoreCase("king")) { return true; } if (pc.equalsIgnoreCase("empty")) { return true; } // must be invalid return false; } /** * static method to determine if a potential piece color is a valid piece color */ public static boolean validColor(char col) { if (col == 'b') { return true; } if (col == 'B') { return true; } if (col == 'w') { return true; } if (col == 'W') { return true; } if (col == 'E') { return true; } // must be invalid return false; } //////////////////////////////////////////////////////////////////////// // Mutators /////////////////////////////////////////////////////////////////////// /** * change value for a pieces color - indicate failure if bad value passed */ public boolean setColor (char col) { if (ChessPiece.validColor(col)) { color = Character.toUpperCase(col); return true; } else { return false; } } /** * change value for a pieces type - indicate failure if bad value passed */ public boolean setPiece (String pc) { if (ChessPiece.validPieceType(pc)) { piece = pc.toUpperCase(); return true; } else { return false; } } /** * Used for testing pieces class - kinda sparse test right now * @param args the command line arguments */ public static void main(String[] args) { char allColors [] = { 'b', 'B', 'w', 'W', 'E'}; String allPieces [] = { "pawn", "rook", "knight", "bishop", "queen", "king", "empty"}; int numPoss = allColors.length * allPieces.length; ChessPiece all [] = new ChessPiece [numPoss]; int cnt = 0; for (int col = 0; col < allColors.length; col++) { for (int pc = 0; pc < allPieces.length; pc++) { char currCol = allColors[col]; String currPc = allPieces[pc]; all[cnt] = new ChessPiece(currCol,currPc); cnt++; } } // display the pieces for (int pc = 0; pc < numPoss; pc++ ) { System.out.println("Piece " + pc + ": " + all[pc]); } } }