Hangman - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Hangman

Description:

This program will play the game of Hangman. The program chooses a ... Nowadays this game is called Wheel of Fortune, except they don't hang you. Variables ... – PowerPoint PPT presentation

Number of Views:3171
Avg rating:3.0/5.0
Slides: 18
Provided by: robbko
Category:
Tags: hangman

less

Transcript and Presenter's Notes

Title: Hangman


1
Hangman
  • Project 3
  • Wed, Oct 22, 2003
  • Due Wed, Oct 29, 2003

2
Hangman 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.

3
Variables
  • 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.

4
Program 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()

5
Problems in main()
  • A while loop controls whether to play another
    game.

// Initialize some stuff while
(next-game-condition) // Play another game
6
Problems in main()
  • A while loop controls when to end a particular
    game.

// Initialize some stuff while
(continue-game-condition) // Process the next
guess
7
Problems 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
8
Problems 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.

9
Outline 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
10
Problems 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?

11
Problems in substituteLetter()
  • Also, to guarantee a proper match, convert the
    user's guess to uppercase, in case he entered it
    in lowercase.

12
Outline 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
13
Problems 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.

14
Problems 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.

15
Problems 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.

16
Problems 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.

17
The 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".
Write a Comment
User Comments (0)
About PowerShow.com