/** * A class to keep track of generated answer, the guess * and to score the guess in comparison to the answer * * @author John Paxton augmented by Andrew Szatkowski * @version 1.0 */ public class Row { /** * Constructs an instance of the row class and initializes * the instance fields * * @param the number of pegs to be used */ Row (int numberPegs) { guess = new int [ numberPegs ]; answer = new int [ numberPegs ]; black = 0; white = 0; } /** * Copies an array of values into the guess array * * @param the array of guessed values * * Pre: a guess has been made and is in array form */ public void recordGuess ( int [] colorsGuessed ) { for (int i = 0; i < guess.length; i++) { guess[i] = colorsGuessed[i]; } } /** * Copies an array of values into the answer array * * @param the array of the generated answer */ public void recordAnswer ( int [] answer ) { for (int i = 0; i < guess.length; i++ ) { this.answer[i] = answer[i]; } } /** * Scores the guess and determines the number of correct * colors in the correct positions, and the number of * correct colors in the wrong positions. * * pre: both guess and answer arrays have been filled */ public void scoreGuess() { // reinitializes the counters to zero black = 0; white = 0; /** * looks for blacks and then negates the position */ for (int i = 0; i < guess.length; i++) { if (guess[i] == answer[i]) { black++; answer[i] = - answer[i]; } } /** * looks for whites and then negates the position */ for (int i = 0; i < guess.length; i++) { if (guess[i] != -answer[i]) { for (int j = 0; j < answer.length; j++) { if (guess[i] == answer[j]) { white++; answer[j] = -answer[j]; break; } } } } /** * Reverses the negation of the previous loops */ for (int i = 0; i < answer.length; i++) { if (answer[i] < 0) { answer[i] = -answer[i]; } } } /** * Prints out the guess, followed by the results */ public void print () { System.out.print("Guess: "); for (int i = 0; i < guess.length; i++) { System.out.print (guess[i] + " "); } System.out.println("Black: " + black + " White: " + white); } /** * returns the number of blacks * * @return the number of blacks */ public int getBlack() { return black; } /** * returns the number of whites * * @return the number of whites */ public int getWhite() { return white; } /** * Determines if the game has been won and * returns a boolean value * * @return boolean that determines game over or not */ public boolean done() { if(black == guess.length) { return true; } else { return false; } } // instance fields private int [] guess; private int [] answer; private int black; private int white; }