/** * 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 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(); //------------------------------------ for lab 6!!!!!!!!!!!!!!!!!!!-------------------------------- int[] intArray; // for task 1 and 2 Square[] sArray; // for task 3 //---------------------------------------------------------------------------------------------- // 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); /********************** for task 1 and 2 *******************/ intArray = new int[10]; for(int i=0; i<10; i++) intArray[i]=(int) (Math.random()*100+1); /************************************************************/ /********************** for task 3 *************************/ sArray = new Square[7]; for(int i=0; i<7; i++){ int randSize = (int) (Math.random()*60+1); sArray[i] = new Square( xPosition-50*OFFSET+i*10*OFFSET, yPosition+10*OFFSET, randSize, getRandomColor(), true, ""); } }//end Board() constructor void displayIntArray(){ //write your code for task 1 to display the numbers in the intArray } void selectionSort(){ //for task 2: /* psudo code for selection sort: 1. Find the minimum value in the list 2. Swap it with the value in the first position 3. Repeat the steps above for the remainder of the list (starting at the second position and advancing each time) */ //write your code to sort the elements in intArray and display the sorted values } void sortSquare(){ //for task 3: //sort the Squares by increasing size, then redisplay } //---------------------------------------------------------------------------------------------- // 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) { }//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()