/** Implement the features of the hangman game. */ public class Game { private String answer; private String display; private int numBadGuesses; private boolean [] alreadyGuessed; private static final int MAX_BAD_GUESSES = 5; public Game() { answer = "everything"; display = ".........."; numBadGuesses = 0; alreadyGuessed = new boolean [26]; for (int i = 0; i < alreadyGuessed.length; ++i) alreadyGuessed[i] = false; } /** The most important function is play. */ public void play(char choice) { // First, see if the letter is in the word at all. // If it isn't, write error msg, and ++ number of bad guesses. // Also, check to see if this letter already guessed. // If it has, write diff error msg, ++ # bad guesses. // If we make it this far, the letter is in the word. // Search entire answer string for this letter, and reveal // each occurrence in the display string. // Don't forget to keep track of the guess. if (answer.indexOf(choice) == -1) { System.out.printf("Sorry, your letter is not in the word.\n"); ++numBadGuesses; return; } } public boolean win() { return display.equals(answer); } public boolean lose() { return numBadGuesses >= MAX_BAD_GUESSES; } public String toString() { return display + " bad guesses = " + numBadGuesses; } public String getAnswer() { return answer; } }