Title: Hangman
1Hangman
- Project 3
- Wed, Oct 22, 2003
- Due Wed, Oct 29, 2003
2Hangman Project
- This program will play the game of Hangman.
- The program chooses a secret word at random.
- The player guesses letters until either he
guesses the word or he gets hanged. - Nowadays this game is called Wheel of Fortune,
except they dont hang you.
3Variables
- word the secret word.
- Example CALENDAR
- puzzle the mixture of underscores and correct
guesses, so far. - Example _ A _ E N _ A R
- letter the guessed letter.
- Example N
- numOfBadGuesses the number of bad guesses, so
far.
4Program Functions
- The program will have six functions
- int main()
- void printWelcome()
- string createUnderscores(const string word)
- int substituteLetter(string puzzle, const
string word, char letter) - void drawGallows(int numOfBadGuesses)
- string getWord()
5Problems in main()
- A while loop controls whether to play another
game.
// Initialize some stuff while
(next-game-condition) // Play another game
6Problems in main()
- A while loop controls when to end a particular
game.
// Initialize some stuff while
(continue-game-condition) // Process the next
guess
7Problems in main()
- You may use a while loop to reject guesses that
use a letter that was already guessed.
while (guessed-letter-was-already-guessed) //
Print an error message // Get a new guess
8Problems in createUnderscores()
- The underscores must be separated by blanks.
- Begin with an empty string of underscores.
- Use a for loop.
- For each letter in the secret word, concatenate
"_ " to the string of underscores. - Thus, the underscores will be in positions 0, 2,
4, 6, 8, and so on.
9Outline of createUnderscores()
string createUnderscores(const string word) //
Initialize underscore string // for loop, once
for each letter in word // For each
letter in word, concatenate "_ " to underscore
string // Return underscore string
10Problems in substituteLetter()
- Use a for loop to compare each letter of the
secret word to the guessed letter. - If the guessed letter matches the letter in
position i of the secret word, we must replace
the i-th underscore with that letter. - What is the position of the i-th underscore in
the string of underscores?
11Problems in substituteLetter()
- Also, to guarantee a proper match, convert the
user's guess to uppercase, in case he entered it
in lowercase.
12Outline of substituteLetter()
int substituteLetter(string puzzle, const
string word, char letter) // for loop, once
for each letter in word // For each
letter in word, compare it to the
guessed letter // If they match,
// Replace the corresponding underscore
// Count the match //
Return the number of matching letters
13Problems in drawGallows()
- Make a separate decision for each line of the
drawing, depending on the number of bad guesses. - Depending on the number of possibilities, use
either a switch statement or a multi-way if
statement.
14Problems in drawGallows()
- For example, the fourth line will be one of the
following strings -
- /
- /
- /\
- depending on whether the number of bad guesses
is less than 2, equal to 2, equal to 3, or
greater than 3.
15Problems in drawGallows()
- Printing the backslash \ presents a technical
problem. - The backslash is used to indicate an escape
character. - It is used to indicate special control
characters - '\n' is a newline character.
- '\b' is a backspace character.
16Problems in drawGallows()
- For example, the statement
- cout ltlt "abc/\npq" ltlt endl
- will print abc/ on one line and pq on another.
- To print a backslash, we must print the character
'\\'. - Thus, the statement
- cout ltlt "abc/\\npq" ltlt endl
- will print abc/\npq, on one line.
17The Backslash Problem
- This also is a problem in writing path names such
as - "C\My Documents\prog.cpp".
- That is why, in C programs, we use the regular
slash /. - "C/My Documents/prog.cpp".
- However, sometimes you will see it written as
- "C\\My Documents\\prog.cpp".