/** * class Board * Implement the functionality for a puzzle where a pattern * of 9 color squares must be matched. * The program displays a grid of 25 squares on the screen, along * with a target pattern of 9 squares. The goal of the game * is to make the 9 central squares of the large grid match * the colors shown in the target set. * * Selecting the "Redo" button starts over. Selecting the * "Exit" button exits the game. * * Class: CS 102/107, Spring 2011 * Lab: Billie Joe Armstrong, Wed. 6:00 AM * System: Eclipse 3.4.2, jsdk 1.6, Windows XP * @author Dale Reed * @version February 7, 2011 * */ import java.awt.Color; import java.util.Scanner; // allows user input from the keyboard import java.util.Random; // allows getting a random number import java.util.Date; // allow creating a Date object to keep track of time public class Board { // Create the random number generator. "Seed" this with the value 1. // Removing the value from the parenthesis will give a different result every // time, which isn't what we want when we are debugging. Random randomNumberGenerator = new Random(1); // declare squares to be displayed Square randomizeButton; // randomize board Square exitButton; // users presses this to exit program Square gameMessagesLabel; // Used to display messages Square goalLabel; // Message shown above goal squares // declare some squares to be displayed on the screen Square s0,s1, s2,s3; // declare constants to use throughout program final int SQUARE_SIZE = 44; final int OFFSET = 6; int moveNumber = 0; int xPosition, yPosition; // Create a Date object to keep track of time Date startTime = new Date(); //---------------------------------------------------------------------------------------------- // Constructor to create the board Board() { xPosition = 40; yPosition = 40; // adjust the x,y starting position xPosition = xPosition - OFFSET/2 + 3; yPosition = xPosition - OFFSET/4 + 2; // jump to the right to set the x,y position to prepare to display the 3x3 goal configuration xPosition = xPosition + ((SQUARE_SIZE + OFFSET) * 6); yPosition = yPosition + ((SQUARE_SIZE + OFFSET) * 1); // create a set of random colors in the goal squares, ensuring adjacent squares do not have // the same color. s0 = new Square( xPosition-40*OFFSET, yPosition+10*OFFSET, SQUARE_SIZE, getRandomColor(), true, ""); }//end Board() constructor void redraw() { s0.setColor("white"); s0 = new Square( xPosition-40*OFFSET, yPosition+10*OFFSET, SQUARE_SIZE, getRandomColor(), true, ""); } //---------------------------------------------------------------------------------------------- // Generate a random number used to select one String getRandomColor() { String theColor = ""; // will store the color, to be returned // generate a random number, which will be used to select a color among 8 colors int randomNumber = randomNumberGenerator.nextInt( 8); switch (randomNumber) { case 0: theColor = "darkGray"; break; case 1: theColor = "red"; break; case 2: theColor = "blue"; break; case 3: theColor = "yellow"; break; case 4: theColor = "green"; break; case 5: theColor = "magenta"; break; case 6: theColor = "cyan"; break; case 7: theColor = "orange"; break; case 8: theColor = "pink"; break; default: System.out.println("Random number value for color out of range. Exiting..."); System.exit( -1); break; }//end switch return theColor; }//end getRandomColor() //---------------------------------------------------------------------------------------------- // Handle each mouse click void handleMouseClick(int mouseX, int mouseY) { }//end handleMouseClick //---------------------------------------------------------------------------------------------- // Handle each key press, in case you want to use this instead of mouse clicks void handleKeyPress( char c) { System.out.println("You pressed " + c); if(c=='w'){ yPosition-=OFFSET; }else if(c=='s'){ yPosition+=OFFSET; }else{ System.out.println("Choose w or s to move square up and down"); } redraw(); }//end handleKeyPress() //---------------------------------------------------------------------------------------------- // Handle the Redo button click by randomizing the board so that // each row and each column satisfies the desired parity //---------------------------------------------------------------------------------------------- // return the square corresponding to the index private Square getSquareNumber( int index) { Square returnValue = null; // will be the reference of the Square to be returned switch ( index) { case 0: returnValue = s0; break; case 1: returnValue = s1; break; case 2: returnValue = s2; break; case 3: returnValue = s3; break; default: // Sanity check, should never execute this code System.out.println("*** Error: invalid square number " + index + ". Exiting..."); System.exit( -1); break; }//end switch( index) return returnValue; }//end getSquareNumber() }// end class Board()