/* * ChessPiece.java * * Created on February 24, 2005, 1:11 AM */ package chess; /** * * @author Mike */ public class ChessPiece { private Color color = Color.empty; // default to empty space private Piece piece = Piece.empty; // default to empty space /** Creates a new instance of ChessPiece. * just take defaults - an empty spot on the board */ public ChessPiece() { } /** Creates a new instance of ChessPiece. * use passed color and piece */ public ChessPiece (Color col, Piece pc) { color = col; piece = pc; } /** * report piece's Color using enum type */ public Color getColor () { return color; } /** * report piece's type using enum type */ public Piece getPiece () { return piece; } /** * report value of piece - based solely on its type */ public int getValue() { int res = 0; switch (piece ) { case pawn: res = 1; break; case knight: res = 3; break; case bishop: res = 3; break; case rook: res = 5; break; case queen: res = 10; break; case king: res = 100; break; case empty: res = 0; } return res; } /** * report value of piece - based solely on its type - but from the perspective of a particular color. * From the perspective of white - if the piece is black then the value is negative */ public int getValue(Color perspective) { int res = getValue(); if (color == perspective) { // rating for the person whose piece it is // result can be unchanged } else { res = res * -1; } return res; } /** * reports whether piece is an empty space. Policy decision - both color and piece * must be empty in order to qualify. Other situations should be handled with a isError method */ public boolean isEmpty() { if ((color == Color.empty) && (piece == Piece.empty)) { return true; } else { return false; } } /** * change pieces color. Probably should not be widely used. Piece Color should not change */ public void setColor (Color col) { color = col; } public boolean switchColor () { if (color == Color.white) { color = Color.black; } else if (color == Color.black) { color = Color.white; } else { // can't swap' return false; } // if got here it is because the swap was successful return true; } /** * change pieces type. Probably should not be widely used. Possible use - pawn reaches end row */ public void setPiece (Piece pc) { piece = pc; } /** * report whether two chess pieces have equivalent contents */ public boolean equals (Object obj) { int x; if (obj instanceof ChessPiece) { ChessPiece toCompare = (ChessPiece) obj; if (color.equals(toCompare.color) && piece.equals(toCompare.piece)) { return true; } else { return false; } } else { // if not same type cannot be equal return false; } } /** * report what the piece is in a nice string */ public String toString () { String res = color.toString() + " " + piece.toString(); return res; } /** * report what a piece is in a short string - for presenting a whole board */ public String toShortString () { String res = ""; switch (color) { case black: res = "B"; break; case white: res = "W"; break; case empty: res = " "; } switch (piece) { case pawn: res += "P "; break; case knight: res += "Kt"; break; case bishop: res += "B "; break; case rook: res += "R "; break; case queen: res += "Q "; break; case king: res += "K "; break; case empty: res += " "; } return res; } }