GIDForums  

Go Back   GIDForums > Computer Programming Forums > MS Visual C++ / MFC 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 10-Oct-2006, 22:50
krobort krobort is offline
New Member
 
Join Date: Oct 2006
Posts: 1
krobort is on a distinguished road

Hangman Game - Need Help!!


I need 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.

CPP / C++ / C 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;
}
Last edited by LuciWiz : 11-Oct-2006 at 00:12. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 18-Oct-2006, 19:12
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,234
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Hangman Game - Need Help!!


I can think of two things:

1. read the file (yours, or one the user can provide) into a vector, and then randomly choose one of the vector entries. This avoids re-reads on the file, and to limit the user's file size, either limit the vector size and/or the size of the file.

2. (maybe overkill) a database interface that queries a random record [word] from a table.

(maybe others will offer more ideas)
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 
 

Recent GIDBlogNot selected for officer school by crystalattice

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
Looking for opinions crystalattice Miscellaneous Programming Forum 6 27-Sep-2006 21:02
Thoughts on Software Piracy pcxgamer Computer Software Forum - Games 9 23-Apr-2004 06:36
want to add a pause in a game. Help jjj93421 C++ Forum 1 11-Feb-2004 09:33
Trying to create the game of life warny_maelstrom C Programming Language 10 21-Jan-2004 21:14
Tips for game troubleshooting pcxgamer Computer Software Forum - Games 0 02-Jan-2004 05:27

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

All times are GMT -6. The time now is 10:39.


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