import java.io.*; import java.lang.reflect.Array; /** * handles the person guessing and saving the current guess array * * @author Tyson Joehler and Aric Walker * @version 2/1/2005 */ public class Guess { Guess ( int numberPegs ) { guess = new int [numberPegs]; } /** * Creates the guess, based upon the user input */ public void createGuess() { // Takes in the user's guess BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); String color = null; int x = 0; boolean done = false; while(!done) { // System.out.print("Enter your guess: "); // try { color = console.readLine(); done = true; } catch (IOException exception) { System.out.println("Error- Input problem"); done = false; } // clears the screen System.out.println('\u000C'); // Takes the input string of numbers and parses them, adds them to the guess array for (int i = 0; i < color.length(); i++) { // Make "parsed" the substring of the original input string String parsed = color.substring(i, i+1); // Parse substring into an integer try { x = Integer.parseInt(parsed); done = true; } catch (NumberFormatException e) { System.out.println("Error- Please input integers"); done = false; break; } try { Array.setInt(guess, i, x); done = true; } catch (ArrayIndexOutOfBoundsException exception) { System.out.println("Error- Please enter the correct number of characters."); done = false; break; } } } } /** * Gets the guess at the current position * * @param position The integer representing the current position * @return the value of the guess at the requested position */ public int getGuess(int position) { return guess [ position ]; } public int getLength() { return guess.length; } private int [] guess; }