/** * A Board "paints" the screen background and then uses 2 arrays of 8 pieces each to keep track of moves. * * @author Dale Reed * @version October 24, 2008 */ public class Board { // instance variables static final int NUMBER_OF_ROWS = 4; static final int NUMBER_OF_COLUMNS = 5; static final int SQUARE_SIZE = 60; /*** OFFSET is the size of extra space around each playing piece */ static final int OFFSET = 5; // OFFSET used in boundaries around playing squares // set starting values for rectangular playing surface static final int STARTING_X_VALUE = 25; static final int STARTING_Y_VALUE = SQUARE_SIZE; /*** count how many pieces white still has to be placed on the board */ private int whitePiecesNumber = 8; /*** count how many pieces black still has to be placed on the board */ private int blackPiecesNumber = 8; /*** uses the label to display the number remaining to play for white at top of board */ private MyRectangle whitePiecesDisplay; /*** uses the label to display the number remaining to play for black at bottom of board */ private MyRectangle blackPiecesDisplay; // display remaining pieces for black on screen /*** array of 8 white playing pieces */ private Piece[] whitePieces; /*** array of 8 black playing pieces */ private Piece[] blackPieces; /*** * Default constructor that "paints" board background and initializes arrays of pieces. *

* Constructor first allocates memory for each of the 8 pieces in the white and black * pieces arrays, setting the color and labels for each of them. They should all still * not be visible, and it is fine for them to all be in the same position (say 0,0), * since they are invisible. *
* Next create the large black background rectangle, of width,height size:
* (SQUARE_SIZE+OFFSET)*5 + OFFSET, (SQUARE_SIZE+OFFSET)*4 + OFFSET *

* Following create each of the background squares. If you know the row and column for * the square, you can use the following formula to place it correctly on the screen:
* Square theSquare = new Square( x + (SQUARE_SIZE + OFFSET) * columns, y + (SQUARE_SIZE + OFFSET) * row, SQUARE_SIZE, "lightGray", true, ""); *

* Lastly create and display the rectangles at the top and bottom that display the number * of pieces remaining to play for white and black. * */ public Board() { // allocate memory for the playing pieces whitePieces = new Piece[ 8]; blackPieces = new Piece[ 8]; // setup the playing pieces for( int i=0; i * " Remaining to play for"
* and then add to that the string "white" or "black", and then add to that the new value. * Use this string to change the label either at the top or bottom of the playing window. * * @param theColor the color ("black" or "white") of the piece label to be updated * @param newValue the new number value to be displayed at the end of the label */ public void updatePiecesDisplay(String theColor, int newValue) { String theText = " Remaining to play for " + theColor + ": " + newValue; if( theColor.equals("white")) { whitePiecesDisplay.setLabel( theText); } else { blackPiecesDisplay.setLabel( theText); } } /** * Get a new piece (if there still are any remaining) and place it at the indicated position. * Note that positions are displayed on the board as 1..20. Given this value, we can * calculate the row and colum as follows:
* row = (thePosition-1) / NUMBER_OF_COLUMNS;
* column = (thePosition-1) % NUMBER_OF_COLUMNS;
* (Remember that NUMBER_OF_COLUMNS is 5.) For example, an index of 8 would give row 1 * and column 2, which is correct from the point of view of the top row being row 0 * and the left column being column 0. *

* Using these row and column values, we can calculate the x,y position for the piece * to be placed using the formula:
* x = STARTING_X_VALUE + (SQUARE_SIZE + OFFSET) * column;
* y = SQUARE_SIZE + (SQUARE_SIZE + OFFSET) * row;
* * @param theColor color of piece ("white" or "black") to be placed * @param thePosition the position (1..20) where it should be placed */ public void placePiece(String theColor, int thePosition) { // find the x,y values corresponding to thePosition. First extract the row and column numbers from thePosition int row = (thePosition-1) / NUMBER_OF_COLUMNS; int column = (thePosition-1) % NUMBER_OF_COLUMNS; // Now use the row and column values in the formula used to calculate position int x = STARTING_X_VALUE + (SQUARE_SIZE + OFFSET) * column; int y = SQUARE_SIZE + (SQUARE_SIZE + OFFSET) * row; // see whose move it is, and ensure there are still pieces to be placed if( theColor.equals("black")) { // make the appropriate next piece placed in the requested position and visible blackPieces[ 8-blackPiecesNumber].setCircleValues(x, y, true); // record the new index of this piece blackPieces[ 8-blackPiecesNumber].setBoardPosition( thePosition); // decrement the count of black pieces and update that display on the playing board blackPiecesNumber--; updatePiecesDisplay( "black", blackPiecesNumber); } else { // color must be "white" // make the appropriate next piece placed in the requested position and visible whitePieces[ 8-whitePiecesNumber].setCircleValues(x, y, true); // record the new index of this piece whitePieces[ 8-whitePiecesNumber].setBoardPosition( thePosition); // decrement the count of white pieces and update that display on the playing board whitePiecesNumber--; updatePiecesDisplay( "white", whitePiecesNumber); } }//end placePiece(... }// end class Board()