Hangman

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
i have done this much so far
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;

int main()
{

srand(time(NULL));


string Word [10] = { "cat", "hat", "bat", "uno", "dos", "tres", "cutro", "pento", "nine", "ten"};
string SecretWord = Word [rand()%10];

string GuessWord = SecretWord;
for (int x = 0; x < SecretWord.size(); x++)
{
if (SecretWord[x]==' ')
GuessWord[x] = ' ';
else
GuessWord[x] = '.';
}

int Location, BadGuesses =0;
string Letter;
while(GuessWord != SecretWord && BadGuesses < 6)
{

// step 6 is - draw guest
// step 7 - display letters guessword
//step 8 - display guess word
cout<<"Guess next letter in the following word:" << GuessWord << endl;

cout<< "phrase to guess \n";


cout << "ENTER LETTER: ";
cin>>Letter;

// 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);
}
if (BadGuesses == 0)
cout << "you lose";
else
cout << "you won";
}


return 0;
}

Topic archived. No new replies allowed.