import java.io.*; /** * This class handles all of the user input issues, such as the BufferedReader * and InputStreamReader. See the method descritpions for a more detailed look * at what the UserInterface class can do. * * @author Tyson Joehler and Aric Walker * @version January 26, 2005 */ public class UserInterface { /** * Constructor for objects of class UserInterface */ public UserInterface() { name = null; console = new BufferedReader(new InputStreamReader(System.in)); } /** * Asks the person for their name and then returns it. * pre- there is no name * post- a name has been entered * * @return The person's name */ public String getName() { System.out.println("*********************** MASTERMIND ***********************"); System.out.print("* Please enter your name: "); try { name = console.readLine(); } catch (IOException exception) { System.out.println(exception); } return name; } /** * This method prompts the user to enter the number of pegs to be used, * and then makes sure that the input is between the specified numbers. * Then it returns the specified value. * pre- there isn't a specific number of pegs * post- the user has entered and computer stored a number of pegs to be used * * @return The number of pegs to be used */ public int getNumPegs() { String pegs = null; int numPegs = 0; boolean done = false; while (!done) { System.out.print("* Enter the number of pegs (1-8): "); try { pegs = console.readLine(); } catch (IOException exception) { System.out.println(exception); numPegs = 0; } try { numPegs = Integer.parseInt(pegs); if ((numPegs >= LOW_BOUND) && (numPegs <= HIGH_BOUND_PEGS)) { done = true; } } catch (NumberFormatException exception) { // } } return numPegs; } /** * This method prompts the user to enter the number of colors to be used, * and then makes sure that the input is between the specified numbers. * Then it returns the specified value. * pre- there isn't a specific number of colors * post- the user has entered and computer stored a number of colors to be used * * @return The number of colors to be used */ public int getNumColors() { String colors = null; int numColors = 0; boolean done = false; while (!done) { System.out.print("* Enter the number of different colors to be used (1-6): "); try { colors = console.readLine(); } catch (IOException exception) { System.out.println(exception); numColors = 0; } try { numColors = Integer.parseInt(colors); if ((numColors >= LOW_BOUND) && (numColors <= HIGH_BOUND_COLORS)) { done = true; } } catch (NumberFormatException exception) { // } } return numColors; } /** * This method is called when the play has either run out of tries, or gets * all of the guesses correct. If the player wins, the system tells them that * they are a mastermind. Otherwise, the system tells them that they took too * many tries. Then the program asks the player if they want to try again. * If yes, the method returns the correct boolean to make the game keep going. * pre- the person has run out of chances or won * post- the person is asked to go again, and returns a boolean depending upon the answer * * @param wins The number of times in the current session that the user has won * @param losses The number of times the user las lost the game * @param result The win/loss result of the just finished game * @return Wether or not the user chooses to play again */ public boolean tryAgain(int wins, int losses, boolean result, Answer answer) { if (result == true) { // System.out.println("You win, You are a Mastermind!"); } else { System.out.println("Too bad, you took too many guesses."); System.out.print("The answer was: "); for(int i=0; i < answer.getLength(); i++) { System.out.print(answer.getPeg(i) + " "); } System.out.println(" "); } System.out.println("Your current win - loss record for " + name +" is: " + wins + "-" + losses); boolean done = false; boolean tryLoopDone = false; String again = null; while (!tryLoopDone) { System.out.println("Would you like to try again? (y/n) > "); // try { again = console.readLine(); } catch (IOException exception) { System.out.println(exception); } if (again.equalsIgnoreCase("n") || again.equalsIgnoreCase("y")) { // if (again.equalsIgnoreCase("n")) { tryLoopDone = true; done = true; return done; } else { tryLoopDone = true; return false; } } else { tryLoopDone = false; } } return false; } private static final int LOW_BOUND = 1; private static final int HIGH_BOUND_PEGS = 8; private static final int HIGH_BOUND_COLORS = 6; private BufferedReader console; private String name; }