hangman...

hi guys...

I have to write a programme that plays a game of hangman with following feeatures:

1.the programme start by reading from an input file a list of words and stores them in an array of string.
2.the programme then selects randomly one of the words. the word should appears like *****
3.the programme should includes 3 functions:
a. function to read the list of words.
b. function to find the number of occurrences and locations of a guessed letter
c.test if a game has terminated (6 incorrect guesses attempted).

sample output:

hangman
the word: **
guess a letter: f
not exists
**
guess a letter: h
h exists 1 time in the word
h*
guess a letter: i
i exists 1 time in the word
hi
bravo

play again? (Y/N): N
thank you

I need your help ,please.
Last edited on
What do you need help with, exactly?

-Albatross
I just need some hints.



I modified the following code:


Code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <cctype>
#include <string>

using namespace std;

int main()
{
char my_choice;
const int MAX_WRONG = 6;
vector<string> words;
words.push_back("guess");
words.push_back("hangman");
words.push_back("diffecult");

srand(time(0));
random_shuffle( words.begin(), words.end() );
const string THE_WORD = words[0];
int wrong = 0;
string soFar ( THE_WORD.size() , '*' );
string used = " ";

cout << "Hi, let's play hangman.\n";

while ( ( wrong < MAX_WRONG) && ( soFar != THE_WORD ) )
{

cout << "The secret word is:\n"<< soFar << endl;

//Getting the players guess. Here is where the function should start

char guess;
cout<<"Guess a leter: ";
cin>>guess;

while ( used.find(guess) != string::npos )
{

cout<<"Guess a leter: ";
cin>>guess;

}//while 2

used += guess;

if ( THE_WORD.find(guess) != string::npos )
{
cout<<"Letter "<<guess<<" is in the word."<< endl;
for ( int i = 0 ; i < THE_WORD.length() ; ++i )
if ( THE_WORD[i] == guess )
soFar[i] = guess;
}
else
{
cout<<"Letter "<<guess<<" is not part of the secret word."<<endl;
++wrong;
}
}//This is the end of the get the guess part
//Ending the game
if ( wrong == MAX_WRONG)
cout<<"You have been hanged!";
else
cout<<"Bravo! You have guessed the full secret word\n";

return 0;
}//main
but it does not conclude all features!!!
This is not a hard program and you got a good set of the features. What you need to manage (and this is important) is writing functions to handle each job.

Here's a nice layout of functions you may use:
1
2
3
4
5
6
7
8
9
10
11
12
13
vector<string> getWordsFromFile(); 
//retrieves a bunch of words from file. (separate each by a newline?)
void chooseWordFromArray(); 
// picks a word from the vector at random and puts it into a string?
char getGuessFromUser(); 
// A simple function to read in a char. 
// You may want to do it separately,
// that way you can add error checking 
// and other items to make sure the user inputs the correct case! (like 'a' instead of 'A')
void checkWord();
// sends in the users guess, the secret word, and updates the string shown to the user.
bool isWordComplete();
// checks if the string shown to the user is completely relieved. 


This seems simple, no? You just need to decide the parameters for these prototypes (you can also change the return type.)
Topic archived. No new replies allowed.