package edu.columbia.cs.cs1007.checkers;
import edu.columbia.cs.cs1007.checkers.Constants.SQUARE_COLOR;
/**
* This class implements a square of the checkers board.
* It contains both the color of the square, and any pieces
* that may have been placed onto that square.
*
* @author Julia Stoyanovich (jds2109@columbia.edu)
* COMS 1007, Summer 2009.
*/
public class Square {
private SQUARE_COLOR _color; // what color is the square
private Piece _contents; // is there a piece on the square, if so, what color is it
/**
* Constructor that creates a square with an "empty" piece.
* @param color
*/
public Square(SQUARE_COLOR color) {
_color = color;
_contents = new Piece();
}
/**
* Constructor.
* @param color
* @param contents
*/
public Square(SQUARE_COLOR color, Piece contents) {
_color = color;
_contents = new Piece(contents);
}
/**
* Accessor.
* @return square color
*/
public SQUARE_COLOR getColor() {
return _color;
}
/**
* Accessor.
* @return piece that is positioned onto the square.
*/
public Piece getContents() {
return _contents;
}
/**
* Place a piece onto the square.
* @param contents
*/
public void setContents(Piece contents) {
_contents = new Piece(contents);
}
/**
* Replace the contents of the square by an "empty" piece.
*/
public void removeContents() {
// an empty piece
_contents = new Piece();
}
/**
* Check whether there is a non-empty piece on this square.
* @return true if there is a piece there, false othewise
*/
public boolean isOccupied() {
return (!_contents.isEmpty());
}
/**
* Generate a string representation of the square.
* @return string
*/
public String toString() {
String res;
if (_color == SQUARE_COLOR.GREEN) {
res = "[" + _contents.toString() + "]";
} else {
res = " " + _contents.toString() + " ";
}
return res;
}
}
|