All pastes #101606 Raw Edit

Miscellany

public text v1 · immutable
#101606 ·published 2006-07-27 00:34 UTC
rendered paste body
/*
 * LottoLogic.java
 *
 * Version:    
 *     $Id$
 *
 * Revisions:
 *     $Log$
 */

import java.awt.*;
import java.util.*;

/**
 * Logic for the Lotto game.
 *
 * @author     Paul Tymann
 * @author     *** YOUR NAME GOES HERE ***
 *
 */

public class LottoLogic {

    // Constants

    /**
     * The number of rows in the lotto game
     */

    private static final int NUM_ROWS = 7;

    /** 
     * The number of colums in the lotto game.
     */

    private static final int NUM_COLS = 7;

    /**
     * The total number of numbers than can be selected is given by 
     * NUM_ROWS * NUM_COLS
     */

    private static final int MAX_NUMBERS = NUM_ROWS * NUM_COLS;

    /**
     * The maximum number of numbers that may be selected by a user.
     */

    private static final int MAX_SELECT = 6;

    /**
     * The reference to the GUI interacting with the user
     */

    private LottoGUI gui;

    /**
     * Generator for the numbers
     */

    private BallBox box = new BallBox( MAX_NUMBERS );

    /**
     * Number of numbered buttons pressed by the user.  This enables
     * the logic to ensure that the user selects exactly MAX_SELECT
     * numbers prior to making a draw.
     */

    private int numButtonsPressed;

    /**
     * Construct a LottoLogic object connected to the specified
     * LottoGUI object.
     *
     * @param   gui    the GUI "connected" to the user.
     */

    public LottoLogic( LottoGUI gui ) {
	this.gui = gui;
    }

    // The callbacks

    /**
     * "Play" the Lotto game once.  Draws MAX_SELECT balls
     * from a BallBox and updates the GUI.
     */

    public void draw() {

	// *** INSERT YOUR CODE HERE ***
        int i=0;
        int counter=0; 
        if(numButtonsPressed==MAX_SELECT){

          clearTheBoard();
          while(i<6){

            int number=box.nextBall();
            if(gui.isSelected(number)){

              gui.setButtonColor(number,Color.yellow);
              counter+=1;

            }else gui.setButtonColor(number,Color.green);
  
            i+=1; 
         }
         
         gui.incDraws();
         gui.incTotals(counter); 
        }
     
    }

    /**
     * Clears the board and resets the statistics back to zero.
     * Note that this method does not "unselect" the numbers
     * the player has selected.
     */

    public void reset() {

	// *** INSERT YOUR CODE HERE ***
        clearTheBoard();
        gui.clearTotals(); 
    }

    /**
     * Updates the logic when a numbered button is pressed.
     *
     * @param    button     the number of the button pressed.
     */

    public void buttonPressed( int button ) {

	// *** INSERT YOUR CODE HERE ***
   
       if(gui.isSelected(button)){ 
         gui.setSelected(button,false);
         gui.setButtonColor(button,Color.lightGray);
         numButtonsPressed--;
       }
       else
       if(numButtonsPressed<MAX_SELECT){
         gui.setSelected(button,true);
         gui.setButtonColor(button,Color.blue);
         numButtonsPressed+=1; 
       }
    }

    /**
     * Prepare the logic for another draw.  Resets the unselected
     * buttons back to their original state.  The selected buttons
     * are changed back to the "selected" color.
     */

    public void clearTheBoard() {
	numButtonsPressed = 0;

	// Reset the ball box for another draw

	box.refill();

	// Step through each of the numbered buttons.  Color the
	// unselected buttons light gray and the selected buttons
	// blue.

	for ( int i = 0; i < MAX_NUMBERS; i++ ) {
	    if ( gui.isSelected( i ) ) {
		gui.setButtonColor( i, Color.blue );
		numButtonsPressed++;
	    }
	    else {
		gui.setButtonColor( i, Color.lightGray );
 	    }	
	}
    }

} // LottoLogic