Embed
Email

Hangman

Document Sample
Hangman
Shared by: HC11121006746
Categories
Tags
Stats
views:
7
posted:
12/9/2011
language:
pages:
17
Hangman

Project 3

Wed, Oct 22, 2003

Due Wed, Oct 29, 2003

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 don’t hang you.

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.

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();

Problems in main()

 A while loop controls whether to play

another game.

// Initialize some stuff

while (next-game-condition)

{

// Play another game

}

Problems in main()

 A while loop controls when to end a

particular game.



// Initialize some stuff

while (continue-game-condition)

{

// Process the next guess

}

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

}

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.

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

}

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?

Problems in substituteLetter()

 Also, to guarantee a proper match, convert

the user's guess to uppercase, in case he

entered it in lowercase.

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

}

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.

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.

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.

Problems in drawGallows()

 For example, the statement

cout << "abc/\npq" << endl;

will print abc/ on one line and pq on another.

 To print a backslash, we must print the

character '\\'.

 Thus, the statement

cout << "abc/\\npq" << endl;

will print abc/\npq, on one line.

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".


Related docs
Other docs by HC11121006746
From Random Sampling to Correct Sampling:
Views: 1  |  Downloads: 0
NildaVenegas ConcienciaCerebroMeditacion
Views: 3  |  Downloads: 0
SJ-c�tanie
Views: 99  |  Downloads: 0
OF�CIO N
Views: 0  |  Downloads: 0
Eric Orff
Views: 0  |  Downloads: 0
Lengua Espa�ola y Literatura
Views: 4  |  Downloads: 0
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!