MeltFrosty - demo by 11/6
Write a Java program that plays what is sometimes called "hangman", but what we will call "MeltFrosty". Here's how it goes:
- The program chooses a secret word.
- The user tries to guess the secret word, letter by letter.
- Every time the user guesses a wrong letter, Frosty melts a little.
- The object is to guess the secret word before Frosty
completely melts.
Make sure you understand how the entire game works before you begin writing code!
Rules and an Example:
- The program selects a secret word. For this example, suppose the secret word is "WILLAMETTE".
- The program replaces each letter in the secret word with an '*' and displays
the result: **********
- On each turn, the user guesses a single letter.
- If the letter is in the word, the program replaces each '*' for that letter
with the letter itself. For example, if the user guesses 'E', then
the secret word display changes to ******E**E. The next time, if
they guess 'L', the display changes to **LL**E**E.
- If the letter is not in the word, then the program says "Sorry frosty!"
and frosty melts. (Use a Frosty7 class
here -- a Frosty7
object is guaranteed to melt in 7 steps.)
- In any case, right or wrong, add the letter to the Letters Tried collection.
For example, if the user has guessed 'E', 'S', 'L', and 'X', then the Letters
Tried area would display ELSX
- After 7 guesses, the game is over. If Frosty is all melted, the player loses and the program reveals the secret word.
On the other hand, if the secret word has
been discovered, the player is the winner and the hero for saving Frosty!
Part I: Design and Basic Functionality
Design the interface for the
game. Draw a sketch of your GUI: show what Buttons, Labels,
and TextFields you will need and what their functionality is to be.
Also decide where you will place the Snowperson7. However, remember
to program incrementally and test your design as you go. For example,
to start with, you should:
- Let the secret word be "WILLAMETTE" so that you can verify your interface
is working properly
- First focus on the essentials of the game:
- Is the letter guessed by the user in the word or not?
- If so, how do I transform **LL**E**E into **LL*ME**E on the guess of an
'M'? (See Hint)
- Forget Frosty for the moment and just display a wrong guess counter.
- Add the Letters Tried display only when the above is working. Don't
worry about alphabetical order for now.
Part II: The Completed Game
Complete the project by adding the bells and whistles left out of part
I. In particular,
- At the beginning of the game, to avoid letting the other player see what you are typing, put up a Dialog box
for the user to type the secret word.
Or, if Dialogs look like trouble, you could make the foreground of the TextField white.
- Include frosty and have him melt when a guess is wrong. Use the code here
Hint: Transforming **LL**E**E into **LL*ME**E on the guess of an
'M'.
Suppose: 1) the secret word is "WILLAMETTE"; 2) the display word is currently "**LL**E**E"; and, 3) the guessed letter is 'M'.
Here is how you can produce the new form of the
display word:
For each letter in the secret word,
If it is not the guessed letter
move the corresponding character in the old display word to the new display word.
else //it is the guessed letter, so
move that letter to the new display word.
The first four characters, "**LL*", get moved, one letter at a time,
from the old display word to the new display word. Then, 'M' gets
added to the new display word to give us "**LL*M". Finally, "E**E"
gets moved, one letter at a time, from the old display word to the new
display word, yielding: "**LL*ME**E"
Extra credit
- Alphabetize the letters in the Letters Tried display.
- Instead of making the user type letter, put up all the letters and let them click them; disappear letters that have already been guessed
- Read the words from a file and choose one at random