/** * A Board "paints" the screen background and then uses two arrays of pieces, * one for white and one for black. * * @author Dale Reed * @version October 24, 2008 */ import java.util.Scanner; import java.util.Random; // allows getting a random number public class Board { // instance variables static final int NUMBER_OF_ROWS = 3; static final int NUMBER_OF_COLUMNS = 3; 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 = 30; static final int STARTING_Y_VALUE = 40; static final int NUMBER_OF_PIECES = 9; // values to be used when explicitly setting board tile values. The first value becomes the // label for the upper-left corner, then labels are assigned row by row. static final int[] startPositionValues = {3,2,7, 5,8,4, 6,1,0}; Square[] pieces = new Square[ NUMBER_OF_PIECES]; Square theInvisibleSquare; // will point to the Square with index 9, which is invisible // create a random number generator Random randomNumberGenerator = new Random(1); /*** * Default constructor that "paints" board background and initializes arrays of pieces. *

* Constructor first allocates memory for each of the 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. *
* Display the title at the top of the window *
* Next create the large black background rectangle, of width,height size:
* (SQUARE_SIZE+OFFSET)*NUMBER_OF_PIECES + 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, ""); *

* */ //---------------------------------------------------------------------------------------- public Board() { // Create the top middle label Square title = new Square( 105, -10, SQUARE_SIZE, "white", true, "9 Tiles Puzzle"); // variables used for drawing int x; int y; // first make a large black background rectangle, that will end up being the border x = STARTING_X_VALUE; y = STARTING_Y_VALUE; Square background = new Square( x-OFFSET, y-OFFSET, (SQUARE_SIZE+OFFSET)*NUMBER_OF_ROWS + OFFSET, "black", true, ""); // Draw the playing pieces themselves for( int row=0; row