/** * Define the fields and methods for a single board playing piece. * * @author reed * @date Oct 24, 2008 * * */ public class Piece { // Fields for the class. // Comments of the form: /**....*/ are used for javadoc, which // automatically generates documentation. /** * the circle to be displayed for this piece */ Circle theCircle; /** * the board position (0 to 19) for this piece. If not displayed it should be set to -1. */ int boardPosition; // constants /** Size of each Square */ static final int SQUARE_SIZE = 60; /** offsets to use in boundaries around playing squares */ static final int OFFSET = 5; /** define number of columns, used for calculating new board position index when making a move */ static final int NUMBER_OF_COLUMNS = 5; /** * Default Constructor, chains off to fully qualified constructor */ public Piece() { // chain off to fully qualified constructor this( 0, 0, 0, "red", false, "", -1); } /** * Fully qualified Constructor that creates a circle for this playing piece and sets * boardPosition which is -1 if circle is not currently displayed. * * @param x x position for the Circle when visible, to be passed to Circle * @param y y position for ththe Circle when visible, to be passed to Circle * @param size size of the Circle * @param color color of the Circle * @param visibility whether or not the Circle will be visible * @param label text to be displayed in the circle * @param boardPosition the index value where this circle is displayed, -1 if not displayed * */ public Piece(int x, int y, int size, String color, boolean visibility, String label, int boardPosition) { // make the circle slightly smaller and offset, so background position labels can be seen theCircle = new Circle( x+OFFSET, y+OFFSET, size-(OFFSET*2), color, visibility, label); this.boardPosition = boardPosition; //default value } /** * Set the x,y position of the circle by passing values on to the Circle class setPosition() method. * * @param x new x position for the circle * @param y new y position for the circle */ void setPosition( int x, int y) { theCircle.setPosition(x, y); } /** * Set the size of the circle by passing size on to the Circle class changeSize() method. * * @param newSize new size of the circle */ void setSize( int newSize) { theCircle.changeSize( newSize-(OFFSET*2)); } /** * Get the color of the piece * * @return the color of the piece */ public String getColor() { return theCircle.getColor(); } /** * Set the color of the circle by passing color on to the Circle class changeColor() method. * * @param newColor new color of the circle */ void setColor( String newColor) { theCircle.changeColor( newColor); } /** * Set the visibility of the circle. Do this by calling either the Circle class * makeVisible() method or the Circle class makeInvisible() method. * * @param theVisibility whether or not the circle should be visible */ void setVisibility( boolean theVisibility) { if( theVisibility==true) { theCircle.makeVisible(); } else { theCircle.makeInvisible(); } } /** * Set the label displayed in the circle. * * @param newLabel the new label */ void setLabel( String newLabel) { theCircle.setLabel( newLabel); } /** * Set the board index position value for this piece. * * @param thePosition */ void setBoardPosition( int thePosition) { this.boardPosition = thePosition; } /** * Get the board index position value for this piece. * * @return thePosition the position (board index) of this piece */ int getBoardPosition( ) { return boardPosition; } /** * Set multiple values at one time for the circle, setting x,y, and visibility. * * @param x x position of the circle * @param y y position of the circle * @param visibility the visibility of the circle. */ void setCircleValues( int x, int y, boolean visibility) { // offset position a bit, since we make the circles slightly smaller than the // enclosing squares theCircle.setPosition(x+OFFSET,y+OFFSET); setVisibility( visibility); } /** * Change multiple values for an existing circle. * * @param x x position for the Circle when visible, to be passed to Circle * @param y y position for ththe Circle when visible, to be passed to Circle * @param newSize new size of the Circle * @param newColor new color of the Circle * @param visibility whether or not the Circle will be visible * @param newLabel new text to be displayed in the circle */ void setCircleValues( int x, int y, int newSize, String newColor, boolean visibility, String newLabel) { setPosition(x+OFFSET,y+OFFSET); setSize( newSize-(OFFSET*2)); setColor( newColor); setVisibility( visibility); setLabel( newLabel); } /** * Move the circle in the indicated single-character direction and update its index accordingly. * * @param theDirection the direction to move ('u','d','l','r') */ void moveTheCircle(char theDirection) { switch( theDirection) { case 'u': theCircle.slowMoveVertical( -(SQUARE_SIZE + OFFSET)); boardPosition = boardPosition - NUMBER_OF_COLUMNS; break; case 'd': theCircle.slowMoveVertical( SQUARE_SIZE + OFFSET); boardPosition = boardPosition + NUMBER_OF_COLUMNS; break; case 'l': theCircle.slowMoveHorizontal( -(SQUARE_SIZE + OFFSET)); boardPosition = boardPosition - 1; break; case 'r': theCircle.slowMoveHorizontal( SQUARE_SIZE + OFFSET); boardPosition = boardPosition + 1; break; default: System.out.println("Illegal direction value of: " + theDirection + " in method moveTheCircle in class Piece. Exiting..."); System.exit( -1); break; } }//end moveTheCircle(char) /** * Move the circle in the indicated direction a certain number of times. This can be used * to make a jump move, by moving the piece two squares and removing the piece it "jumps" over. * This is accomplished by calling method moveTheCircle() multiple times. * * @param theDirection Direction to move, which is passed on to method moveTheCircle() * @param numberOfTimes How many times a single-square move should be made. */ void moveTheCircle(char theDirection, int numberOfTimes) { for( int i=0; i