/** * This class will keep the game variables from the Answer and Row classes * * @author Reid Roper and Nick Lamb * @1.0 */ import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Game { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); /** * The game constructor initializes the game variables * * @author Reid Roper and Nick Lamb * @version 1.0 * @param pegs the number of pegs * @param colors the number of colors * @param the number of guesses the user gets * @precondition none of the variables are set * @postcondition all of the variables are set */ Game(int pegs, int colors, int numGuess) { this.gameHist = new Row [numGuess]; this.trys = 1; answer = new Answer(pegs,colors); answer.createAnswer(); this.pegs = pegs; this.colors = colors; } /** * This method will store a new verified guess in a history * it will also have the guess scored. * * @author Reid Roper and Nick Lamb * @version 1.0 * @param i this int is the number of tries the person is on * the i sets the gameHist[i] (an array of Rows) the * next row of guesses */ public void newGuess(int i) throws IOException { this.trys = 1 + i; gameHist[i] = new Row(this.pegs); //adds a row to the gameHist array gameHist[i].setAnswer(answer); //sets an answer in the new Row Guess(in); gameHist[i].recordGuess(numbers); //numbers is set in the Guess(in), this stores gameHist[i].scoreGuess(); //scores blacks and whites } /** * This method will clear the screen and print out all of the previous guesses and * score for the game. * * @author Reid Roper and Nick Lamb * @version 1.0 * */ public void printGame() { System.out.print('\u000C'); for(int i = 0; i < trys; i++) { gameHist[i].print(); } } /** * This method will take the guess from the user and make sure that * the inputs were valid. * * @author Reid Roper and Nick Lamb * @version 1.0 * @param in thie BufferedReader is used in getting info from the user * @return run this boolean is used to keep looping till a good guess is made */ private void Guess(BufferedReader in) throws IOException { boolean done = false; //will stop the loop while(!done) { String guess = in.readLine(); char [] chararray = guess.toCharArray(); //turn to char array numbers = new int [this.pegs]; //make array the size of pegs int b = 0; //for the spot in the array try { for(int i = 0; i <= (chararray.length - 1); i += 2)//starts at zero, gets every other number { String test = new String(chararray,i,1); //make every other char a string numbers[b] = Integer.parseInt(test); //put the number in that array if(numbers[b] < 1 || numbers[b] > this.colors) //check if between 1 and colors throw new InvalidRangeException("Please enter a number between 1 and " + this.colors + "."); b++; //if thrown above, does not reach here, therefore the next } //spot in number will be used if the previous is valid done = true; //finished } catch(NumberFormatException exception) //catch { System.out.println("You have entered an invalid guess, please try again."); } catch(InvalidRangeException exception) { System.out.println(exception); } } } /** * This method will get a Row from the history * * @author Reid Roper and Nick Lamb * @version 1.0 * @param index this int is the index spot you want from the array * @return gameHist[] this is a Row from the history */ public Row getHist(int index) { return this.gameHist[index]; } /** * This method will call the print fuction of the answer class * * @author Reid Roper and Nick Lamb * @version 1.0 * */ public void printAnswer() { answer.print(); } private Answer answer; private Row gameHist []; private int trys; private int pegs; private int colors; private int [] numbers; }