Question: This assignment will give you practice with file input, strings, and arrays. Instructions You are going to write a program that will allow the user
This assignment will give you practice with file input, strings, and arrays.
Instructions
You are going to write a program that will allow the user to play a game like hangman (Links to an external site.)Links to an external site., where your program chooses a word from a text file and then displays blanks for the user to guess the word. The user will have a set number of incorrect guesses and if they do not guess the word within that number of tries, they will lose the game.
Max Wrong
You should define a class constant for the maximum number of incorrect guesses that the user gets before they lose the game. You may choose this number. The more guesses the user gets, the easier the game.
You should print the number of tries the user gets in the intro text to the game.
Max Words
You should define a class constant for the maximum number of words in the dictionary.txt file. This constant will be used when you construct the array to hold all the words in the file.
Methods
At a minimum, your program should have the following static methods in addition to the main:
a method to load the input from a file into an array
a method to pick the word for this game from the list of words
a method to print the word
a method to play one game of hangman
a method to check if the word is guessed
a method to check if the guess was in the word
Method: Load Input
You need to create a method that will read in words from a text file called dictionary.txt
The dictionary.txt file has 127,100 words in so you can construct an array of the appropriate size before reading in all the words.
Your method should return an array of all the words
Your method should receive a String parameter that indicates the file to be loaded.
Note that this method, in addition to the main, will need to throw a FileNotFoundException in the case that dictionary.txt does not exist
Method: Pick Word
You should have a method that takes an array of words as a parameter and a Random number generator object. This method should return a random word to be guessed. (This method returns a String.)
Method: Play game
You should have a method that plays one game of hangman.
You are expected to create a boolean array to track whether or not each letter in the word should be displayed to the user. When a spot in this array is true, you display the letter to the user. When a spot in this array is false, you display an underscore ("_") to the user.
The user should be prompted to guess a letter as long as they have not guessed incorrectly more times than the MAX_WRONG and they have not guessed the word.
If the user's guess is not in the word, they should lose one of their tries for guessing; however, if the user does not guess incorrectly, they do not lose a try.
A message should be printed to the console to indicate whether the user's guess was in the word. If it is, print how many times it occurred. If it isn't, print the number of tries they still have before the game ends.
If the user does not solve the word correctly, the correct word should be displayed at the end of the game.
Method: Print Word
This method should print the word to the screen according to the true/false values contained in the boolean array that is passed to it as a parameter.
Method: Check if Solved
This method should return true if all the boolean values in the array are true; false, otherwise. The puzzle is solved when all the characters in the puzzle are displayed to the user.
Method: Check Guess
This method should check if the user's guess is in the word and should return the number of occurrences of the guess in the word.
If the user's guess is entered in lowercase but appears in the puzzle in uppercase, it should still be marked to be displayed. Likewise, if the user's guess is entered in uppercase but appears in the puzzle in lowercase, it should also be marked to be displayed.
Hints and Other Notes
Note that you can solve this program creating exactly two arrays (one for the words read in from the file and one for the true/false values of whether or not to display a character). If you solve the problem this way, you will utilize string operation methods (like charAt) to look at each letter in the word String.
Remember that to lowercase a string, you say s.toLowerCase(), where s is a String. To lowercase a character, you say Character.toLowerCase(c), where c is a char
Remember that arrays are passed by reference to methods, so if you change an array in the method you do NOT need to return it to see the changes take place.
Sample Output
Example where the puzzle is solved
Let's play a game of hangman. You can guess wrong up to 10 times. _ _ _ _ _ _ _ Guess a letter: a _ _ _ _ _ _ _ a was not in the word. You have 9 incorrect tries. Guess a letter: e _ e _ _ _ e _ e was in the word 2 times. Guess a letter: n _ e n _ n e _ n was in the word 2 times. Guess a letter: t t e n _ n e _ t was in the word 1 times. Guess a letter: s t e n _ n e _ s was not in the word. You have 8 incorrect tries. Guess a letter: i t e n _ n e _ i was not in the word. You have 7 incorrect tries. Guess a letter: y t e n _ n e _ y was not in the word. You have 6 incorrect tries. Guess a letter: u t e n _ n e _ u was not in the word. You have 5 incorrect tries. Guess a letter: l t e n _ n e _ l was not in the word. You have 4 incorrect tries. Guess a letter: d t e n _ n e d d was in the word 1 times. Guess a letter: o t e n o n e d o was in the word 1 times. You won!
Example where the puzzle is not solved
Let's play a game of hangman. You can guess wrong up to 10 times. _ _ _ _ _ _ _ Guess a letter: a _ _ _ _ _ _ _ a was not in the word. You have 9 incorrect tries. Guess a letter: e _ _ _ _ _ e _ e was in the word 1 times. Guess a letter: i _ i _ _ _ e _ i was in the word 1 times. Guess a letter: o _ i _ _ _ e _ o was not in the word. You have 8 incorrect tries. Guess a letter: u _ i _ _ _ e _ u was not in the word. You have 7 incorrect tries. Guess a letter: y _ i _ _ _ e _ y was not in the word. You have 6 incorrect tries. Guess a letter: b _ i _ _ _ e _ b was not in the word. You have 5 incorrect tries. Guess a letter: c _ i _ _ _ e _ c was not in the word. You have 4 incorrect tries. Guess a letter: d _ i d d _ e _ d was in the word 2 times. Guess a letter: r _ i d d _ e r r was in the word 1 times. Guess a letter: l _ i d d l e r l was in the word 1 times. Guess a letter: f _ i d d l e r f was not in the word. You have 3 incorrect tries. Guess a letter: v _ i d d l e r v was not in the word. You have 2 incorrect tries. Guess a letter: m _ i d d l e r m was not in the word. You have 1 incorrect tries. Guess a letter: n _ i d d l e r n was not in the word. You have 0 incorrect tries. You lost, the correct word was piddler
Please help me
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
