Overview: For this project youll be creating a program to play Hangman. The goal is to get practice reading files and using arrays.
Description: For this project, you are going to create the game hangman. Hangman is a simple word game where the player attempts to guess a word before they get "hung" by guessing letters that they think might be in the word. For your version of the game, youre going to use world capitals instead of words. You will randomly select a line from the file capitals.txt; you will need to have your program count how many lines are in the file and then randomly generate a number to choose which line youll use for the game. Your program should tell the user how many letters are in the place name by showing the appropriate number of blanks for each word. Anything that is not a letter, i.e. spaces and commas, should be displayed to the screen in original form (not as a blank). Allow the user to guess letters one at a time (in either upper or lowercase) until they get the full text or guess incorrectly 6 times. Have an interface which looks similar to the one below:
_
| |
|
|
|
|
---------
The place is: _ _ _ _ _, _ _ _ _ _ _ _ _ _ _ _
Make a guess: a
As the player makes guesses, update the display accordingly.
_
| |
|
|
|
|
---------
The place is: _ a _ _ _, A _ _ _ a n _ s _ a n
Make a guess:
When the player misses, draw a new part of the person hanging from the gallows. Here is an example:
_
| |
* |
-| |
|
|
---------
The place is: _ a _ _ _, A _ _ _ a n _ s t a n
Make a guess: r
r is not in the place name
_
| |
* |
-|-|
|
|
---------
The place is: _ a _ _ _, A _ _ _ a n _ s t a n
Make a guess:
In this case, the right arm would have just been drawn because of the fourth miss. When the player gets a letter correct you should update the blank where that letter occurs to show where it goes (as was the case with s and n in the example above). When the game ends, tell what the word was and whether or not the player won. Here is an example:
_
| |
* |
-|-|
/ |
|
---------
The place is: K a b u l, A f g h a n i s t a n
You lost!
When the game ends, give the user the option of playing again.
Helpful Code and Hints: When testing your program, create a new version of capitals.txt that is smaller and easier to work with!
makeGuess() This function will validate the users guess against the selected capital. It receives the array of capitals and the current string of guess progress. It can return a Boolean value depending on the success of the guess. It should also validate that the user has not already guessed a particular letter. You will want to ignore letter case issues 'a' vs 'A'. In order to do this, you can use the toLower(name[i]) function. In addition, you may need to use the .length() method on a string to determine how long the string is.