package edu.columbia.cs.cs1007.checkers;
import edu.columbia.cs.cs1007.checkers.Constants.PIECE_COLOR;
import edu.columbia.cs.cs1007.checkers.Constants.VERTICAL_DIRECTION;
/**
* Implementation of a checkers piece.
* @author Julia Stoyanovich (jds2109@columbia.edu)
* COMS 1007, Summer 2009.
*/
public class Piece {
private boolean _isKing;
private PIECE_COLOR _color;
/**
* Constructor.
* @param color
*/
public Piece(PIECE_COLOR color) {
_color = color;
_isKing = false;
}
/**
* Constructor that creates an "empty" piece.
*/
public Piece() {
this(PIECE_COLOR.NONE);
}
/**
* Constructor.
* @param that
*/
public Piece(Piece that) {
_isKing = that._isKing;
_color = that._color;
}
/**
* Check whether the piece is a king.
* @return true if king, false otherwise.
*/
public boolean isKing() {
return _isKing;
}
/**
* Assign king status to a piece.
*/
public void makeKing() {
_isKing = true;
}
/**
* Accessor.
* @return color of the piece
*/
public PIECE_COLOR getColor() {
return _color;
}
/**
* Ask the piece in which vertical direction(s) it would like to move.
* Non-king pieces can only move in one direction, determined by their
* color: white pieces always move towards increasing rows, black pieces
* move towards decreasing rows.
* King pieces can move in both directions.
* @return vertical direction(s) of desired move
*/
// where does this piece want to move?
public VERTICAL_DIRECTION getDesiredVerticalDirection() {
VERTICAL_DIRECTION direction;
if (_isKing) {
// wants to move in all directions
direction = VERTICAL_DIRECTION.BOTH;
} else if (_color == PIECE_COLOR.WHITE) {
// wants to move towards increasing row numbers
direction = VERTICAL_DIRECTION.FORTH;
} else {
// wants to move towards decreasing row numbers
direction = VERTICAL_DIRECTION.BACK;
}
return direction;
}
/**
* Generate a string representation of the piece
* @return string
*/
public String toString() {
String res = "";
if (_color == PIECE_COLOR.BLACK) {
res += Constants.BLACK_SIGN;
} else if (_color == PIECE_COLOR.WHITE) {
res += Constants.WHITE_SIGN;
} else {
res = " ";
}
return res;
}
/**
* Check whether this is an empty piece.
* @return true if empty, false otherwise
*/
public boolean isEmpty() {
return (_color == PIECE_COLOR.NONE);
}
}
|