GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Apr-2009, 20:40
YQI YQI is offline
New Member
 
Join Date: Apr 2009
Posts: 2
YQI is on a distinguished road

Hangman game


i'm making a hangman game! i have started some code, but i'm running into problems.
here is my code:
CPP / C++ / C Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

const int MAX_WORDS = 256;
const int MAX_WORD_SIZE = 20;

//Prototypes
void ReadFile(); // Open hangman.txt for reading
void Playgame(int i);
void PrintGallows();

char String[MAX_WORD_SIZE];
string Words[MAX_WORDS];
int Count;

//main function
int main(){
  int run = 0;
  ReadFile();
  for (; run < MAX_WORDS; run++){

  Playgame(run);
  }
  return 0;
}

//Functions

//Get data from file
void ReadFile(){
  ifstream inFile;
  int  count = 0;
  string curr_string;

  inFile.open("hangman.txt");
  getline (inFile, curr_string);
  while(!inFile.eof() and (count < MAX_WORDS)){
    Words[count] = curr_string;
    count++;
    getline(inFile, curr_string);
  }
  inFile.close();
}

//Play the game
void Playgame(int i){
  int size;
  int state = 0;
  int subscript = 0;
  char guess[MAX_WORD_SIZE];
  char copy[MAX_WORD_SIZE];
  int correct = 0;
  char letter;

  Words[i] >> copy[];
  size = Words[i].length();

  for (; subscript < size; subscript++){
    guess[subscript];
  }

  guess[subscript] = ' ';
  while(state !=6){
    PrintGallows(state);
    cout << guess << endl;

    cout << "Guess a letter: " << endl;
    cin >> letter;
    letter = toupper(letter);

    for (subscript = 0; subscript < size; subscript++){
        if (copy[subscript] == letter){
          guess[subscript] = letter;
          correct = 1;
          cout << "Good!" << endl;

          if (copy == guess){
cout << "you won!" << endl;
            return;
          }
        }
    }
    if (correct == 0){
      cout << "wrong!" << endl;
      state++;
    }
    correct = 0;
  }
}
void PrintGallows (int state) {

  // Return without printing if num_limbs is bad
  if (state <= 0 or state > 6) {
    return;
  }

  cout << " _________     \n";
  cout << "|         |    \n";
  cout << "|         0    \n";

  switch(state) {

  case 1:
    cout << "|              \n";
    cout << "|              \n";
    break;
  case 2:
    cout << "|         |    \n";
    cout << "|         |    \n";
    break;
  case 3:
    cout << "|        /|    \n";
    cout << "|         |    \n";
    break;
  case 4:
    cout << "|        /|\\    \n";
    cout << "|         |    \n";
    break;
  case 5:
 cout << "|        /|\\    \n";
    cout << "|         |      \n";
    cout << "|        /     \n";
    break;
  case 6:
    cout << "|        /|\\    \n";
    cout << "|         |      \n";
    cout << "|        / \\    \n";
    break;
  }

  cout << "|              \n";
  cout << "|              \n";
}
  #2  
Old 28-Apr-2009, 00:32
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: need help on hangman


Thanks for using CODE tags!!!!

But give us a clue what the problem is.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #3  
Old 28-Apr-2009, 09:31
YQI YQI is offline
New Member
 
Join Date: Apr 2009
Posts: 2
YQI is on a distinguished road

Re: Hangman game


i think this Function have problem
CPP / C++ / C Code:
//Play the game
void Playgame(int i){
  int size;
  int state = 0;
  int subscript = 0;
  char guess[MAX_WORD_SIZE];
  char copy[MAX_WORD_SIZE];
  int correct = 0;
  char letter;

  Words[i] >> copy[];
  size = Words[i].length();

  for (; subscript < size; subscript++){
    guess[subscript];
  }

  guess[subscript] = ' ';
  while(state !=6){
    PrintGallows(state);
    cout << guess << endl;

    cout << "Guess a letter: " << endl;
    cin >> letter;
    letter = toupper(letter);

    for (subscript = 0; subscript < size; subscript++){
        if (copy[subscript] == letter){
          guess[subscript] = letter;
          correct = 1;
          cout << "Good!" << endl;

          if (copy == guess){
cout << "you won!" << endl;
            return;
          }
        }
    }
    if (correct == 0){
      cout << "wrong!" << endl;
      state++;
    }
    correct = 0;
  }
}
  #4  
Old 28-Apr-2009, 10:02
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 761
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Hangman game


There are quite a few things wrong with your code:

1) Why aren't you using string objects instead of char[]?
2) There are no comments in the code to tell us what is supposed to be happening so I don't wish to read on.

Here is how I would go about it:
CPP / C++ / C Code:
void PlayGame(const string& word)
{
  int state = 0;

  // Create a copy of word with all letters being '_' characters
  string guessWord(word.length(), '_');

  // Game Loop
  while(state < 6)
  {
    // Draw the screen
    //...

    // Check for a winner (Once there is no '_' characters left in guessWord, we have a winner)
    if(guessWord.find_first_of('_') == npos) break;
    
    // Get a character from the user
    char c = GetGuess();// a function you can write

    // Process the guess
    bool found = false;
    for(int i=0;i<guessWord.length();++i)
    {
      // If the guess is good, replace the '_' character with the guess
      if(guessWord[i] == '_' && word[i] == c)
      {
        found = true;
        guessWord[i] = c;
      }
    }
    if(!found) ++state;

  }//end Game Loop
}
  #5  
Old 28-Apr-2009, 11:34
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Hangman game


Let me be more explicit:
You need to tell us what your code is doing wrong. Just posting your code and asking us to fix whatever problems you have is not appropriate.

Please reread item 2 of the Guidelines -- carefully.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hangman in Assembly zeliie Assembly Language 1 24-Nov-2007 11:32
Hangman Game - Need Help!! krobort MS Visual C++ / MFC Forum 1 18-Oct-2006 19:12
hangman program help! Ichigo C++ Forum 27 10-Apr-2006 18:26
Suggestions for my Code for Hangman /* Ansi C-based */ Lan C Programming Language 7 19-Mar-2006 18:29
hangman and showing the letters Krandygrl00 C++ Forum 14 11-Jul-2005 15:32

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:36.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.