Hangman Program in 11 Easy Steps
Please demo to the instructor or
upload .cpp to Ecampus
Hangman Pseudo Code
1. Do a nice introduction screen for your hangman program (do this step last).
2. Select a random word and store it in a string variable name SecretWord.
3. Create GuessWord which will be the same size as SecretWord, but all periods (e.g. ". . . . . . . .")
string GuessWord =
SecretWord; for (int x = 0; x < SecretWord.size(); x++) { if (SecretWord[x]==' ') GuessWord[x] = ' '; else GuessWord[x] = '.'; } |
4. Declare an integer named BadGuesses = 0
Declare a string named Letter
Declare an integer named Location
5. Set up a while loop for steps 6 -
10. It should loop as long as BadGuesses < 6 and GuessWord !=
SecretWord.
This is the main loop of the program. The game keeps playing as long as you haven't lost
(when BadGuesses
= 6) and you haven't won (when GuessWord = SecretWord).
{ // This is the opening brace for the main while loop in the program
6. Display Graphics (do this step last)
7. Display Letters Already Guessed (do this step last)
8. Cout the GuessWord variable (the placeholder will all periods)
9. Prompt player to enter a letter (their guess) and store it in the variable Letter. Add this letter to LettersGuessed.
10. If Letter is not located in SecretWord (note: use Letter.find( ), increment BadGuesses
Else continue looping and find all occurences of Letter in GuessWord and replace the periods.
//
Step 10 Location = SecretWord.find(Letter,0); if (Location > SecretWord.size()) BadGuesses++; else while (Location < SecretWord.size()) { GuessWord.replace(Location,1,Letter); Location = SecretWord.find(Letter, Location + 1); } |
}
11. If you exit the loop, then you've either won or lost. Therefore, if BadGuesses == 6, then display "you lose", otherwise display "you win".
Tips - If you do not follow these tips and ask for my help, I will simply tell you to follow these tips.
(a) Comment each step in your
code (e.g. // Step 3)
(b) Do the graphics (step 1 & 6) last. Do step 7 last.
(c) Do each step and then test it - don't try to do the whole program at once
(d) Indent your code properly --- after an opening brace, indent
Extra Credit Ideas
To get extra credit on your hangman program, you can implement some of the following ideas:
(a) Outstanding graphics. E.g. add color, clear screen when needed, do a theme.
(b) Ask the user to select a subject for their secret word - e.g. college, movies, etc.
(c) Make your program work with spaces and capitalized letters in the secret word.
(d) Keep score on how many times you've won and lost. The score could be saved to a file. The next time you run the program, it reads the numbers from the file into variables.