Question
The main program: function hangman() The hangman function will 'run' the actual game. It will look something like this: >> hangman You are playing hangman.
The main program: function hangman()
The hangman function will 'run' the actual game. It will look something like this:
>> hangman You are playing hangman. The word has 5 letters in it. So far you have: _____ What letter do you guess? a So far you have: _a___ What letter do you guess? e Sorry, there are no e's |--- | | | ----- ----- So far you have: _a___ What letter do you guess?
...
The following pseudocode is provided for your assistance.
Read the list of possible words from a file. For the hangman program, the following list of words is provided for you. Copy constitution.txt to your working directory. You can use textread() to read them in:
words = textread( 'constitution.txt', '%s' );
Remember, words will be a cell array and you must use {} to access it. (If you are unclear on what a cell array is, you should review your notes/readings.)
Randomly choose one of the words from this file (meaning: randomly choose one of the values in the array that was created by textread).
You must make sure that the chosen word is valid (use your valid_word() function). If not, randomly choose another word until a valid word is found.
Tell the user they are playing hangman and how many letters the chosen word has in it. (Print out a nice message for them.)
Create an "incomplete solution" word that is the same length as the chosen word, but put '_' for every letter instead of the actual letters.
As long as the solution hasn't been filled in and the user hasn't incorrectly guessed 8 times do the following:
Show the current incomplete solution.
Ask the user for a letter.
Keep track of the letters that have been guessed. You can use an array of 26 booleans for this. If the user guesses the same letter again, ask them to select another letter before proceeding.
Use your function above to find where (and if) the letter is in the word.
If the letter is in the word, fill in the incomplete solution word with the letter in the correct spot(s).
If not, then increase the number of missed guesses and draw the hangman.
If the word was completed, print a congratulatory message.
Otherwise, print a failure message and show the word.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started