/** * Creates a random answer, returns the answer, and allows * access to all positions of the answer arry * * @author John Paxton augmented by Andrew Szatkowski * @version 1.0 */ public class Answer { /** * Constructs an instance of Answer class initializing * the instance fields * * @param number of pegs being used * number of colors being used */ Answer (int howManyPegs, int howManyColors) { colors = howManyColors; answer = new int [ howManyPegs ]; flags = new boolean [ howManyPegs ]; } /** * Creates random values between 1 and the number of colors * and places them in all positions of the answer array * * pre: the answer array has been initialized */ public void createAnswer () { for (int i = 0; i < answer.length; i++) { answer[i] = (int) (colors * Math.random()) + 1; } } /** * Returns the answer array * * @ return the answer array */ public int [] getAnswer() { return answer; } /** * Resets the flag array to false for * all array indexes */ public void reset () { for (int i = 0; i < flags.length; i++) { flags[i] = false; } } /** * Returns the value of a peg at the passed * in position * * @param the desired position of the array * @return the peg's value */ public int getPeg (int position) { return answer[position]; } /** * Returns the boolean of a peg at the passed * in position * * @param the desired position of the array * @return the boolean of the peg */ public boolean getPegUsed (int position) { return flags[position]; } /** * Sets the boolean value of a peg at the desired * position to reflect that it has been used * * @param the desired position of the array */ public void setPegUsed (int position) { flags[position] = true; } /** * Prints out the answer and flag arrays */ public void print () { for (int i = 0; i < answer.length; i++) { System.out.print ( answer[i] + "(" + flags[i] + ")" + " "); } System.out.println(); } // instance fields private int colors; private int [] answer; private boolean [] flags; }