Lab 8. Hangman
Write an application that allows the user to play Hangman.
In the game Hangman, the player tries to guess a word or phrase (we will
just be doing words) one letter
at a time. The player has a finite number (we will use 7) of wrong guesses
before they lose the game. If they guess the word in less than 7 guesses, they
win.
For this lab, we will begin to program the game. Your next programming
assignment will be to finish the game. No graphics are needed for this lab.
You will need the following controls to complete today's lab:
- A textbox to enter each guess.
- A label to output the word to be guessed. You
should display a "-" for each letter that has not been guessed yet.
- A label to output the letters that have been
guessed wrong already.
- A label to output how many wrong guesses
left.
- A label to announce the player won or lost at
the end of the game.
- A button to submit a guess after the player has
typed a letter in the textbox.
Example Layout (yours can look much nicer of course)
You do not need the submit word button, the textbox
above it, or the reset button to get full credit for this lab. They
are covered in the bonus section and will be part of Programming
Assignment 3.
You will need the following variables to complete today's lab:
- A String variable to hold the word to be
guessed. For this lab, to make the program easier, you can hardcode or
initialize this variable with any word so that every time the game is
played it will be the same word. Choose a word that does not have the
same letter twice for now.
- A String variable to hold the guessed word.
This variable should be initialized to all "-"'s for each letter in the
word to be guessed. We will change this variable's value as each correct
letter is guessed.
- A String variable to hold the one letter guess
entered by the user in the textbox.
- A Integer variable to hold the number of
guesses left. Initialize this variable to 7.
- A Integer variable to hold the position of a
correct guess in the word to be guessed.
For today's lab, you should put all code in the button's click event
function.
Algorithm for the button's click event function:
if the length of the guess is not 1 (use the Len function)
Output error message in a MsgBox
else
Get position of guess in word (use InStr function)
if position = 0 (incorrect guess)
decrement guesses left
Output incorrect guess to label (use &= so you don't lose previous wrong guesses)
else
update guessed word variable (use Microsoft.VisualBasic.Left and .Mid functions) (see below)
Output new guessed word to label
end if
end if
if guessed word equals word to be guessed
Output Player Wins
else if guesses left = 0
Output Player Loses
end if
Updating guessed word variable:
To add the guess to the guessed word variable on a correct guess you
will need to concatenate 3 strings to together.
The 3 strings:
- The portion of the guessed word to the left of the guess position.
- The guessed letter.
- The portion of the guessed word to the right of the guess position.
For example:
- The word is "vader"
- The current guessed word is "---e-"
- The current guess is "d"
- So, the position is 3.
- You must concatenate the following 3 strings to get the new guessed word
- "--" (use Microsoft.VisualBasic.Left)
- "d" (this is the guess)
- "e-" (use Microsoft.VisualBasic.Mid)
Bonus: Up to 30% possible (10% for each item)
- (10%)Add a reset button that allows the game to start over without
restarting the application. It should reset all appropriate labels,
variables, and textbox to their starting values.
- (10%)Allow words with multiples of the same letter. If the word has 2
e's for example, you need to add both e's to the guessed word if the
player guesses e. You will need a loop to do this.
- (10%)Add a textbox and button to allow a user to input the word to be
guessed. Because the word can be any size, you now have to initialize
the guessed word to the correct number of "-"'s.
These bonus items will be part of the final program turned in as
Programming Assignment 3, so you will also have less to do for that if you
complete one or more of these.