/** * Handles creating a random answer and storing it for the game * * @author Tyson Joehler and Aric Walker * @version 2/2/2005 */ public class Answer { /** * Answer constructor * * @param howManyPegs the number of pegs the user chooses to have in each row * @param howManyColors the number of colors the user chooses to have in each row */ Answer (int howManyPegs, int howManyColors) { colors = howManyColors; answer = new int [ howManyPegs ]; flags = new boolean [ howManyPegs ]; } /** * Creates a random answer array * pre- no answer array * post- array with random numbers between the specified values (color) */ public void createAnswer () { for (int i = 0; i < answer.length; i++) { answer[i] = (int) (colors * Math.random()) + 1; } } /** * Resets the flags array * pre- answer is tagged with values saying if it's been used or not * post- the flags array is set to all false again (reset) */ public void reset () { for (int i = 0; i < flags.length; i++) { flags[i] = false; } } /** * Returns the answer # stored in the array at the given position * * @param position the peg at the certain position * @return The number that was stored in the requested position */ public int getPeg (int position) { return answer[position]; } /** * Method used in handling if the answer has been tried already * * @param position the peg at the certain position * @return Boolean- if the peg has been used or not */ public boolean getPegUsed (int position) { return flags[position]; } /** * Sets the peg at position to 'used' * * @param position the specified position */ public void setPegUsed (int position) { flags[position] = true; } /** * returns the length of the array * @return The length of the array */ public int getLength() { return answer.length; } /** * Prints out the answer- called only for testing */ public void print () { for (int i = 0; i < answer.length; i++) { System.out.print ( answer[i] + "(" + flags[i] + ")" + " "); } System.out.println(); } private int colors; private int [] answer; private boolean [] flags; }