import java.awt.Color;

/**
 * A simple guessing game involving colors.
 * When created, a ColorGame picks a random color specified by
 * three ints representing the red, green, and blue components of
 * the color.  The isMatch() method checks to see if a given
 * guess (3 components) matches the chosen color.  The goalColor
 * and the most recent guessColor can be accessed from outside this 
 * class to create a GUI for this game.  The range field (indicating
 * how large an int is used to specify a color component) is also
 * available for use in the GUI.
 */
class ColorGame {
	int range;						// Allowed range for color components
	Color goalColor;				// Current goal color
	Color guessColor;				// Most recent guess color
	
	/**
	 * Create a new ColorGame.
	 * @param range color components are between 0 and range-1 (inclusive)
	 */
	public ColorGame (int range) {
		if (256 % range != 0) throw new IllegalArgumentException(
			"Number of colors must divide 256.");
		this.range = range;
		goalColor = color(	(int) (Math.random() * range),
							(int) (Math.random() * range),
							(int) (Math.random() * range));
		guessColor = color(0,0,0);
		}
	
	/**
	 * Check if the given guess matches the goal color.
	 * Each component of the guess is a number between 0
	 * and range-1 (inclusive).
	 * @param r the red component
	 * @param g the green component
	 * @param b the blue component
	 */
	public boolean isMatch (int r, int g, int b) {
		guessColor = color(r,g,b);
		return guessColor.equals(goalColor);
		}
	
	/**
	 * Return the color determined by the given red, green, and
	 * blue values.  Each color value is a number between 0 and
	 * range-1 (inclusive).
	 * @param r the red component
	 * @param g the green component
	 * @param b the blue component
	 * @return the color determined by the red, green, and blue values
	 */
	private Color color (int r, int g, int b) {
		int factor = 256/range;
		return new Color(r*factor,g*factor,b*factor);
		}
	}
