#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;
#define MAX_WORD_SIZE 15 // The maximum size of word
#define MAX_WORDS 255 // the max number of words
void EnterWord();
void LoadFile();
void ChooseWord();
void RunGame();
void instruction();
void DrawGallows(int State);
typedef char String [MAX_WORD_SIZE]; // a char type with 15 characters
String Words [MAX_WORDS-1];
int Count; // word count
char menu;
char Continue;
char choice;
int main()
{
instruction();
	cout << "Choice: ";
		cin >> choice;
		cout << " " << endl;
while (Continue =='Y')
{
    
  
 cout<<"Welcome to Hangman . . . Don't lose your head!"<<endl;
   
   switch(choice)
   {
       case '1':
       EnterWord();
       break;
       
       case '2':
       ChooseWord();
       break;
       
       case '3':
       cout<<"\n\nBye!"<<endl<<endl;
      	break;
		default:
			cerr << "Invalid choice - " << menu << endl
				<< "enter again please. " << endl << endl;
   }
}

}
// Function to run the program

void instruction() // show the instruction
{
 cout<<"\n\n *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.* H A N G M A N *.*.*.*.*.*.*..*.*.*.*.*.*";
   cout<<"\n\n1. Enter a word (2 Player)";
   cout<<"\n\n2. Computer chooses word (1 Player)";
   cout<<"\n\n3. Quit ";
   cin.get();
}
// if option is 1


void LoadFile()
{
     char C; 
     ifstream Datfile;
     Count = 0 ;
     Datfile.open("word.txt" );// open the data file word.txt
     while (( C=Datfile.peek()) !=EOF)
     {
          // get the next word and then increment Count
          Datfile>> Words [Count++];
     // If we surpass the max exit and tell the user
     if (Count > MAX_WORDS -1 )
     {
               cout << endl << "Too many words in the file, stopping with "<< MAX_WORDS<< "words"<< endl;
               Count = MAX_WORDS;
               break;
               }
               }
     // subtract one to get the correct number of words
     Count--;
     //Close the file
     Datfile.close();
     }
void EnterWord()
{
 char solution[20];	//holds solution
	char blank[20];		//holds "*"'s for unsolved letters
	int Subscript=0;
	int State=1;
	char Guess[20];
    char Copy [20];
    char Letter;
    int Correct =0 ;
    // input the string and getline it
	cout<<"Enter phrase 20 chars or less."<<endl;
	cin.getline(solution, 20);
	 int Word; // this will hold the subscript of our word
     int Size; // this will hold the length of our word     int State = 1 ; // This will hold the game state.     int Subscript= 0; // This will hold the subscirpt     char Guess [MAX_WORD_SIZE]; // this will hold their current word     char Copy [MAX_WORD_SIZE]// this will hold a copy of word
     char letter;// this will be their letter guess     int Correct = 0  ; // This is a true/ false value  
     //Create a null terminated string to represent the word as the player know it//
     for ( ; Subscript < Size; Subscript++)
     {
         Guess [Subscript] = '-';
         }
     // insert the null character 
     Guess [Subscript] = '\0';
     // Go until the player is hung
     while (State!=6)
     {
           DrawGallows(State);
           cout << Guess << endl;
           cout << "Guess a letter:" ;
           cin >> letter;
     // use only lower case letters
     Letter = tolower(Letter);
     for (Subscript = 0 ; Subscript < Size; Subscript ++ ) 
     {
         if (Copy[Subscript]== Letter)
     {
         Guess [Subscript] = Letter;
         Correct =1 ;
         cout<< endl<< "Good Guess";
         // If guess equals the word they won so exit
         if (strcmp (Words[Word],Guess) == 0 )
         {
                    cout << endl << "Congratulation !! You won !!" << endl;
                    return;
                    }
                    }
                    }
         // If they did not guess correctly
         if (Correct == 0)
         {
                     cout<< endl << "Sorry,bad guess!";
                     State++;
                     }
                     Correct = 0 ;
                     }
                     DrawGallows(State); // Draw the gallows at end of game
                     cout << "the word was : " << Words [Word]<< endl << endl;
                                       }
void ChooseWord() // input choice = 2
{
     LoadFile(); // call the function to open the file
     int Word; // this will hold the subscript of our word
     int Size; // this will hold the length of our word
     int State = 1 ; // This will hold the game state.
     int Subscript= 0; // This will hold the subscirpt
     char Guess [MAX_WORD_SIZE]; // this will hold their current word
     char Copy [MAX_WORD_SIZE];// this will hold a copy of word
     char Letter;// this will be their letter guess
     int Correct = 0  ; // This is a true/ false value
     //Create a random number
     srand (( unsigned)time (NULL));
     Word = rand () % Count; // find the remainder
     // Make a local copy of the word
     strcpy(Copy,Words[Word]);
     Size = strlen ( Words[Word]); // get the word's size
     //Create a null terminated string to represent the word as the player know it//
     for ( ; Subscript < Size; Subscript++)
     {
         Guess [Subscript] = '-';
         }
     // insert the null character 
     Guess [Subscript] = '\0';
     // Go until the player is hung
     while (State!=6)
     {
           DrawGallows(State);
           cout << Guess << endl;
           cout << "Guess a letter:" ;
           cin >> Letter;
     // use only lower case letters
     Letter = tolower(Letter);
     for (Subscript = 0 ; Subscript < Size; Subscript ++ ) 
     {
         if (Copy[Subscript]== Letter)
     {
         Guess [Subscript] = Letter;
         Correct =1 ;
         cout<< endl<< "Good Guess";
         // If guess equals the word they won so exit
         if (strcmp (Words[Word],Guess) == 0 )
         {
                    cout << endl << "Congratulation !! You won !!" << endl;
                    return;
                    }
                    }
                    }
         // If they did not guess correctly
         if (Correct == 0)
         {
                     cout<< endl << "Sorry,bad guess!";
                     State++;
                     }
                     Correct = 0 ;
                     }
                     DrawGallows(State); // Draw the gallows at end of game
                     cout << "the word was : " << Words [Word]<< endl << endl;
                     }

void DrawGallows(int State)
{
 if(State==6)
 {
  // The \\ will translate as '\' because it is a special char
  cout<<endl<<endl
   <<"   +----+     "<<endl
   <<"   |    |     "<<endl
   <<"   |    O     "<<endl
   <<"   |   /|\\   "<<endl
   <<"   |   / \\   "<<endl
   <<"   |Your Dead "<<endl
   <<"  ============"<<endl<<endl;
 }
 else if(State==5)
 {
  cout<<endl<<endl
   <<"   +----+  "<<endl
   <<"   |    |  "<<endl
   <<"   |    O  "<<endl
   <<"   |   /|\\ "<<endl
   <<"   |     \\ "<<endl
   <<"   |       "<<endl
   <<"  ============"<<endl<<endl;
 }
 else if(State==4)
 {
  cout<<endl<<endl
   <<"   +----+  "<<endl
   <<"   |    |  "<<endl
   <<"   |    O  "<<endl
   <<"   |   /|\\ "<<endl
   <<"   |       "<<endl
   <<"   |       "<<endl
   <<"  ============="<<endl<<endl;
 }
 else if(State==3)
 {
  cout<<endl<<endl
   <<"   +----+  "<<endl
   <<"   |    |  "<<endl
   <<"   |    O  "<<endl
   <<"   |   /|  "<<endl
   <<"   |       "<<endl
   <<"   |       "<<endl
   <<"  ============="<<endl<<endl;
 }
 else if(State==2)
 {
  cout<<endl<<endl
   <<"   +----+  "<<endl
   <<"   |    |  "<<endl
   <<"   |    O  "<<endl
   <<"   |    |  "<<endl
   <<"   |       "<<endl
   <<"   |       "<<endl
   <<"  ============="<<endl<<endl;
 }
 else if(State==1)
 {
  cout<<endl<<endl
   <<"   +----+  "<<endl
   <<"   |    |  "<<endl
   <<"   |       "<<endl
   <<"   |       "<<endl
   <<"   |       "<<endl
   <<"   |       "<<endl
   <<"  ============="<<endl<<endl;
 }
}

thank you for all of your feed back and this is my updated version
but the things is the program did not run yet. though I tried to developed almost everything.
when I input value to the program to ask for each function. it does not run and exit automatically.
I dont' know why . please help me. it will due tommorow. thank you

Recommended Answers

All 6 Replies

choice is char
1 would be 49 decimal..

I pretty sure there's a better way of drawing the scene with an initial array that you print in a loop, then add the new characters to the array every new state.

I don't know what do you mean? I am sorry iam a foreigner, but can you please explain it a bit clearly

Sorry i did not see '
Initialize Continue='Y' first..

while (Continue =='Y')
{


cout<<"Welcome to Hangman . . . Don't lose your head!"<<endl;

I did ,man

I'm not sure..

char c = 'Y';
while (c == 'Y')
{//...}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.