import java.io.BufferedReader; import java.io.IOException; /** * Creates a profile for the player of the game and keeps track * of their scores, as well as carries out interactive duties * between the user and the keyboard * * @author Andrew Szatkowski * @version 1.0 */ public class User { /** * Constructs an instance of the user class * initializing all instance fields to zero */ public User() { name = "Unkown"; num_of_pegs = 0; num_of_colors = 0; num_of_wins = 0; num_of_losses = 0; } /** * Prompts the user to input their name * * @param the buffered reader */ public void setName(BufferedReader in) throws IOException { System.out.println("Enter your name: "); name = in.readLine(); } /** * Prompts the user to enter the number of pegs * they would like to use * * @param the least amount of pegs possible * the most amount of pegs possible * the buffered reader * @throws NumberRangeException * NumberFormatException */ public void setPegs(int lowBound, int highBound, BufferedReader in) throws IOException { done = false; /** * Checks for valid input */ while(!done) { System.out.println("How many pegs would you like to use? (1 to 8)"); num = in.readLine(); try { num_of_pegs = Integer.parseInt(num); if ((num_of_pegs >= lowBound) && (num_of_pegs <= highBound)) { done = true; } else { throw new NumberRangeException("The number you entered is not in range"); } } catch(NumberRangeException exception) { System.out.println("The number you entered is not in range"); } catch(NumberFormatException except) { System.out.println("Not a valid integer input"); } } } /** * Prompts the user to enter the number of colors * they would like to use * * @param the least amount of colors possible * the most amount of colors possible * the buffered reader * @throws NumberRangeException * NumberFormatException */ public void setColors(int lowBound, int highBound, BufferedReader in) throws IOException { done = false; /** * checks for valid input */ while(!done) { System.out.println("How many colors would you like to use? (1 to 6)"); num = in.readLine(); try { num_of_colors = Integer.parseInt(num); if ((num_of_colors >= lowBound) && (num_of_colors <= highBound)) { done = true; } else { throw new NumberRangeException("The number you entered is not in range"); } } catch(NumberRangeException exception) { System.out.println("The number you entered is not in range"); } catch(NumberFormatException except) { System.out.println("Not a valid integer input"); } } } /** * Prompts the user to enter a guess * * @param the buffered reader * @throws NumberOfInputException */ public void setGuess(BufferedReader in) throws IOException { done = false; /** * checks for valid input */ while(!done) { System.out.println("Enter a guess "); guess = in.readLine(); try { if(guess.length() == num_of_pegs) { done = true; } else { throw new NumberOfInputException("Wrong number of inputs"); } } catch(NumberOfInputException exception) { System.out.println("Invalid number of pegs guessed"); } } } /** * Parses the input guess into integers and then places them * in the parsedGuess array */ public void parseGuess() { parsedGuess = new int [num_of_pegs]; for(int i = 0; i < num_of_pegs; i++) { parsedGuess[i] = Integer.parseInt(guess.substring(i, i+1)); } } /** * returns the number of pegs being used * * @return the number of pegs */ public int getPegs() { return num_of_pegs; } /** * returns the number of colors being used * * @return the number of colors */ public int getColors() { return num_of_colors; } /** * returns the string input of the user's guess * * @return the user's guess */ public String getGuess() { return guess; } /** * returns the array of the user's guess * * @return the user's guess */ public int [] getParsedGuess() { return parsedGuess; } /** * adds another win to the user's tally */ public void posResult() { num_of_wins += 1; } /** * adds another loss the user's tally */ public void negResult() { num_of_losses += 1; } /** * prints the user's statistics */ public void printRecord() { System.out.println(name); System.out.println("Wins: " + num_of_wins); System.out.println("Losses: " + num_of_losses); } // instance fields private String name; private String guess; private String num; private int [] parsedGuess; private int num_of_pegs; private int num_of_colors; private int num_of_wins; private int num_of_losses; private boolean done; }