Write a JavaFX GUI program to play a game of Hangman. (If you're not familiar with the game, check out the Wikipedia page here (Links to an external site.).) Your program reads in a dictionary file and randomly chooses a word. The user guesses letters until they either guess the word or run out of guesses.
There are two parts to this project. The first is writing the game and getting it working properly. The second is adding exception handling. I strongly recommend you complete Part I before moving on to Part II.
I've provided a GUI shell for the game. The shell sets up all of the GUI components. You will complete two methods: the chooseWord method and the handleGuessField method.
Your game must follow these rules:
The provided file uses several text components that you will update throughout the program:
chooseWord method
This method randomly chooses a word. The method should read in words from a file (such as words.txt, provided) and randomly choose a word.
The method should also provide code to account for a missing file. In this case, the outcomeText should display an error message (such as "Error: No dictionary."). The guessField should then be disabled because the game cannot be played. The program should not crash or throw other exceptions.
Hint for testing: Print out the randomly selected word you are trying to guess. This makes for much easier testing!
handleGuessField method
This method responds to the user guess (typed into the guessField). This is basically the method that makes the game run!
In this method, you should read in the user's guess and check if it is valid. If it's not valid, update the displays.
If the guess is valid, you should check if the user has guessed the letter already. If they have, update the displays.
If it's a new guess, check if it is right or wrong and update the displays. Then also check if the game is over (either the user guessed the word and won or reached the maximum wrong guesses and lost).
For full credit:
You can add code to the start() method to initialize variables and/or invoke helper methods.
I strongly recommend getting the game working before moving on to Part II.
Add exception handling to cover three erroneous occurrences.
For all three situations, create your own exception type(s) to represent the situation.
The user continues to guess after invalid guesses.
Invalid guesses does not count against the user's wrong guess count.
Erroneous situations:
Your program method should not terminate or crash because of any of these thrown exceptions. All thrown exceptions should be caught and handled and the game should continue.
Allow the user to play multiple games. Add a "play again" button that is only visible when the game is over. Choose a new random word for each game.