Hangman Game - Need Help!!

This is a discussion on Hangman Game - Need Help!! within the C++ Programming forums, part of the General Programming Boards category; I need help to improve below program interface, store a list of words in a file and read the words ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    1

    Hangman Game - Need Help!!

    I need help to improve below program interface, store a list of words in a file and read the words randomly for each game the player guess. Anyone can help? TQ.

    Code:
    // Hangman game
    //
    
     #include <iostream>
     #include <string>
    #include <cctype>
    
    
    using namespace std;
    
    const int KILL_COUNT = 9;
    
    // Makes a string consisting of only '_' with same length
    string make_display_word(string s)
    {
      string t = "";
      for (int i = 0; i < s.size(); i++)
        t += '_';
      return t;
    }
    
    // return true if char ch is in string s
    bool ch_in_string(char ch, string s)
    {
      for (int i=0; i < s.size(); i++)
        if (ch == s[i])
          return true;
      return false;
    }
    
    // enter ch into display at position where it appears 
    // in secret
    void enter_char(char ch, string secret, string &display)
    {
      for (int i = 0; i < secret.size(); i++) {
        if (secret[i] == ch)
          display[i] = ch;
      }
    }
    
    int main()
    {
      // The word to guess
      string secret_word = "superposition";
    
      // How many wrong guesses
      int wrong_count = 0;
    
      string display_word = make_display_word(secret_word);
      string already_guessed = "";
    
      while (true) {
        cout << "\nYour guess so far: " << display_word
             << "\nWrong guesses: " << wrong_count
    	 << "   Letters guessed: " << already_guessed
    	 << endl;
        if (display_word == secret_word) {
          cout << "You got it!\n";
          exit(0);
        }
        if (wrong_count >= KILL_COUNT) {
          cout << "You are dead!\n";
          exit(0);
        }
        cout << "\nGuess a letter: ";
        char ch;
        cin >> ch;
        ch = tolower(ch);
        if (!isalpha(ch)) {
          cout << "Only letters!\n\n";
        } else {
          if (ch_in_string(ch, already_guessed)) {
    	cout << "You already guessed '" << ch << "'\n";
          } else {
    	already_guessed += ch;
    	if (ch_in_string(ch, secret_word)) {
    	  enter_char(ch, secret_word, display_word);
    	} else {
    	  cout << "Sorry, wrong guess!\n";
    	  wrong_count++;
    	
          }
    	 }
    	}
      }
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Well, how you want to improve the program's interface is largely up to you. You could store a file of words, delimited by whitespace, then read each word using fscanf (be sure to specify a character limit in case someone tampers with the file to cause a buffer overflow) and pick a random one.

    Alternatively, you could store each word using a fixed number of characters (not a problem unless you have several long words), possibly padding shorter words with whitespace (so you can use words of different lengths). Then you just generate a random number, seek to that position in the file and read it in.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  3. #3
    Registered User SpaceCadet's Avatar
    Join Date
    Oct 2006
    Posts
    23
    Code:
    make_display_word may exhibit ndefined behaviour because it returns a copy of a local variable. You can deprecate this function by using a string constructor to initialise the display word string.
    
    http://www.cppreference.com/cppstring
    
    string display_word(secret_word.length(), '_');
    
    
    Deprecate ch_in_string by using the std::string::find method.
    
    if (already_guessed.find(ch, 0) != std::string::npos)
    {
        // do stuff
    }
    
    
    You can use std::string API to deprecate enter_char aswell.
    
    Regards string the words to file. You might consider parsing the strem content using an ifstream (or relative) into a string and then tokenising that string using std::string::substr.
    Something like....
    
    size_type start=0;
    size_type end=0;
    vector<string> words;
    
    const string DELIM(" ");
    
    while (start != string::npos)
    {
       end=myString.find(DELIM, start);
       std::string mySubStr=myString.substr(start, end);
       words.push_back(mySubStr);
       start=end;
    }
    
    
    
    Off the top of my head. I haven't compiled it, but I hope it helps...

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    You'll also need to include <vector> to use vectors.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please comment on my c++ game
    By MegaManZZ in forum Game Programming
    Replies: 10
    Last Post: 01-22-2008, 10:03 AM
  2. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 06:58 PM
  3. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  4. hangman game, copied staight from book.. 31 errors?!?!?!?!
    By Blizzarddog in forum C++ Programming
    Replies: 10
    Last Post: 10-25-2002, 12:20 AM
  5. Using 'if' with char arrays or string objects
    By c++_n00b in forum C++ Programming
    Replies: 36
    Last Post: 06-06-2002, 09:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21