PurposeIn this homework, you will write a word guessing game, similar to the hangman game that most people played when they were young. Because we don't want to get fired, we won't be calling it hangman, even though that is what everybody else calls it. Instead will use a more "politically correct" name, such as The Word Guessing Game (or TWoGG, for short). It sounds (intentionally) silly, but it allows us to keep our jobs.... The idea behind the game is simple: a word is chosen by the computer, and is initially shown to the player with all blanks. The player gets a certain number of guesses, and on each guess, they can select a letter. If that letter appears in the word, then the word is shown with that letter filled in in the correct spot(s), along with all previously guessed letters filled in. If the letter does not appear in the word, then a list of "misses", meaning letters guessed that are not in the word, is displayed as well. While the player can guess many times, the game ends when either (1) the player completely fills in the word, or (2) the number of misses hits a pre-determined number (we'll use 8 for this program). We are assuming that most people in the class are familiar with the game. If not, then more details on TWoGG (a.k.a. hangman) can be found here. There are two files to download: TWoGG.java, the skeleton code, and TWoGGHelper.class, which we describe below. AlgorithmThis game has a number of steps. We define a "turn" as a single guess, whether the guessed letter is in the word, not in the word, or a letter already guessed.
Note: If you don't understand the above steps, then you are just going to be confused with what is listed below. Don't worry if you don't know how to implement each step in Java yet -- that is explained below. But you should understand the basic idea of what each step does, and the entire algorithm should make sense. Class TWoGGHelperWe have provided you with a TWoGGHelper class file, which has a number of method that you should call to get your game working.
The skeleton code defines a TWoGGHelper object named helper. You can use that to call the various methods (helper.getWord(), etc.). Let c be a char variable. When a letter c is guessed by the player, you should call helper.guessLetter(c). This will tell the TWoGGHelper to add that letter to its list of letters that have already been guessed. Then, to find out if a given letter c has already been guessed, you should call helper.alreadyGuessedLetter(c). This will return true or false, depending on whether the passed letter has already been guessed. Lastly, to find out if all the letters in a word have been guessed, you should call helper.fullyGuessedWord(word), where word is a String. This will also return true or false. Note that three of the methods (alreadyGuessedLetter(), guessLetter(), and fullyGuessedWord()) are provided to help you remember which characters have been guessed. You can choose to use them or not (some may want to implement the same functionality differently, such as with Strings or Vectors). However, you MUST use getWord() or getHardWord() (we don't care which one) to choose your word! This is what we will use to grade your program. Revisiting the AlgorithmHaving given the algorithm above, we give a bit more detail to the steps here.
Incremental Development
Sample ExecutionAs before, the text in red is what was input by the user. Welcome to our game of TWoGG! Word: _ _ _ _ _ _ _ _ Misses: You have used 0 of 8 guesses Please enter your letter guess: a That guess is a miss! Word: _ _ _ _ _ _ _ _ Misses: a You have used 1 of 8 guesses Please enter your letter guess: b That guess is a miss! Word: _ _ _ _ _ _ _ _ Misses: a b You have used 2 of 8 guesses Please enter your letter guess: c That guess is a hit! Word: c _ _ _ _ _ _ _ Misses: a b You have used 2 of 8 guesses Please enter your letter guess: o That guess is a hit! Word: c o _ _ _ _ _ _ Misses: a b You have used 2 of 8 guesses Please enter your letter guess: r That guess is a hit! Word: c o _ _ _ _ _ r Misses: a b You have used 2 of 8 guesses Please enter your letter guess: e That guess is a hit! Word: c o _ _ _ _ e r Misses: a b You have used 2 of 8 guesses Please enter your letter guess: a You already guessed that letter! Word: c o _ _ _ _ e r Misses: a b You have used 2 of 8 guesses Please enter your letter guess: m That guess is a hit! Word: c o m _ _ _ e r Misses: a b You have used 2 of 8 guesses Please enter your letter guess: e You already guessed that letter! Word: c o m _ _ _ e r Misses: a b You have used 2 of 8 guesses Please enter your letter guess: t That guess is a hit! Word: c o m _ _ t e r Misses: a b You have used 2 of 8 guesses Please enter your letter guess: q That guess is a miss! Word: c o m _ _ t e r Misses: a b q You have used 3 of 8 guesses Please enter your letter guess: z That guess is a miss! Word: c o m _ _ t e r Misses: a b q z You have used 4 of 8 guesses Please enter your letter guess: x That guess is a miss! Word: c o m _ _ t e r Misses: a b q x z You have used 5 of 8 guesses Please enter your letter guess: u That guess is a hit! Word: c o m _ u t e r Misses: a b q x z You have used 5 of 8 guesses Please enter your letter guess: v That guess is a miss! Word: c o m _ u t e r Misses: a b q v x z You have used 6 of 8 guesses Please enter your letter guess: w That guess is a miss! Word: c o m _ u t e r Misses: a b q v w x z You have used 7 of 8 guesses Please enter your letter guess: n That guess is a miss! Sorry, you lost. Better luck next time! The word was computer Miscellaneous NotesThe good programming practices from HW J1 need to be in this homework as well. As the homeworks in the course progress, less detailed information will be provided, and it is up to you to fill in the detail that is left out (in this case, the legend, Scanner creation, test runs as comments, etc.). The output, above, is going to be a bit long. Rather than having to put a // in front of each line for your test case, there is an easier way to comment out a block of text. Put a /* at the beginning of the block, and a */ at the end. This will cause everything between them to be a comment (even if it spans multiple lines). Lastly, the maximum number of guesses (set at 8) should be set to a constant somewhere in your code. SubmissionWhen you are finished, submit the TWoGG.java file. |