/** * The Mastermind class can start the inner workings of the mastermind game. * It will access the Questions and Game classes. * * @author Reid Roper and Nick Lamb * @version 1.0 */ import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Mastermind { private static int guesses; /** * This Mastermind constructor sets how many guesses the user is allowed to have. * * @author Reid Roper and Nick Lamb * */ Mastermind(int guesses) { this.guesses = guesses; } /** * startGame() will keep the game running until the person tells it to stop * This method will creat a Question and Game object and make the mastermind game * run. * * @author Reid Roper and Nick Lamb * @version 1.0 */ public void startGame() { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); boolean done = false; System.out.println("Welcome to Mastermind"); Questions stats = new Questions(); //start the questions stats.enterName(in); //only need the name once while(!done) { stats.enterPegs(in); //enter the pegs each round stats.enterColor(in); //enter the color each round //makes the Game Game game = new Game(stats.getPegs(),stats.getColors(),guesses); System.out.print('\u000C'); //clear screen System.out.println("Enter your guess in one line with spaces between the guess"); System.out.print("ex) 1 2 3 4\n\n"); int count = 0; while (count < guesses) //keep the person from guessing too many times { try { System.out.print("> "); game.newGuess(count); //will make guess and score } catch(IOException exception) { System.out.println("You did something bad!"); } game.printGame(); //print out history if(game.getHist(count).getBlack() == stats.getPegs()) //you win { System.out.println("Congradulations " +stats.getName()+ " !!\n\tYou have won the game."); break; } count++; if(count == guesses) //took too many guesses { System.out.println("You lost!\n\n"); System.out.print("The answer was "); game.printAnswer(); } } String quit = askQuestion("\n\nWould you like to play again (yes/no)? >",in); if(quit.compareToIgnoreCase("no") == 0) done = true; System.out.print('\u000C'); } } /** * The askQuestion will ask a question and take the users response * * @author Reid Roper and Nick Lamb * @param text is the text of the question you want asked * @param in is a bufferedReader to get info from the user * @return input the string that the user entered * */ public static String askQuestion(String text, BufferedReader in) { String input =""; try { System.out.print(text); input = in.readLine(); } catch(IOException exception) { System.out.println(exception); } return input; } }