Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

One player Hangman

Finished making your own program (thats not part of the practicing forums) ? Show it off here

One player Hangman

Postby backlash53 on Sun Sep 13, 2009 12:22 pm

Download here: http://www.mediafire.com/?nwaiyzxyamt
(You must include the file "Words.txt" in the same folder as the program.)
Code: Select all
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>

using std::string;

class hangman
{
public:
   hangman(); // prompt names.
   void readWords(); //read words from a txt file and store them into a vector.
   void setWord();// sets a random word from the vector
   void promptUser(); // main function, prompts user for characters.
   std::string getWord() { return theWord; } // accessor
   std::string getWordC() { return theWordC; } // accessor
   bool get_status() { return game_over; } // accessor
   

private:
   bool game_over; // Game finished ?
   std::string words;
   std::vector<string> allWords; // vector to store the words from the .txt
   std::ifstream file;
   std::string pone; // player one's name
   int random;
   std::string theWord; // chosen word
   std::string theWordC; // The chosen word concealed.
   char letter; // letter user has chosen
   int tries;
   
};

int main()
{
   bool cont = 1;
   srand((unsigned)time(0)); // make sure word is random every time.
   hangman game; // create instance of hangman called game.
   game.readWords(); // read the words from the text file
   game.setWord(); // set a random word

   
   while (cont && !game.get_status())
   {
        game.promptUser();
   }
   
   char f;
   std::cin >> f;
   return 0;
}
hangman::hangman() : tries(0), game_over(0)
{
   
   std::cout << "Player one, enter your name: ";
   std::cin >> pone;
   
}
void hangman::readWords()
{
   file.open("words.txt");
   
   if (!file )
   {
      std::cout << "Unable to open file";
   }

   while (file >> words )
   {
      allWords.push_back(words);
   }
   file.close();
}

void hangman::setWord()
{
  random = (rand()%10)+1; // 1 - 10
 
  for ( int i = 0; i != random; i++)
  {
     theWord = allWords[random];
  }
  std::string temp(theWord.size(), '_');
   theWordC = temp;
}

void hangman::promptUser()
{
   const time_t elapsed = clock()/CLOCKS_PER_SEC;
   std::cout << "TRIES: " << tries << "\t" << "TIME ELAPSED: " << clock()/CLOCKS_PER_SEC << std::endl;
   std::cout << "WORD SO FAR: " << theWordC << std::endl;


   if ( clock()/CLOCKS_PER_SEC >= 50 || tries == 30 )
   {
      std::cout << "Game over  :(" << std::endl;
      game_over = 1;
      
   }
   
   std::cout << pone << ", guess a letter: ";
   std::cin >> letter;
   if ( letter  != 'a' || 'b'|| 'c'|| 'd'|| 'e'|| 'f'|| 'g'|| 'h'|| 'i'|| 'j'|| 'k'|| 'l'|| 'm'|| 'n'|| 'o'|| 'p'|| 'q'|| 'r'|| 's'|| 't'|| 'u'|| 'v'|| 'w'|| 'x'|| 'y'|| 'z' )
   {
      std::cout << "wrong input, please try again." << std::endl;
   }
   tries++;
   
   

   for ( int i = 0; i != theWord.size(); i++ )
      {
         if ( letter == theWord[i] )
         {
            theWordC[i] = letter;
         }
   }

   if (theWordC == theWord)
   {
      std::cout << "Congratulations !, you win the word was: " << getWord() << std::endl;
      game_over = 1;
      
   }
   
}
backlash53
 
Posts: 26
Joined: Wed Feb 04, 2009 1:42 pm

Re: One player Hangman

Postby KingNoobie on Mon Sep 14, 2009 1:47 pm

Isnt it possible to make words load in the game? otherwise, the players can "cheat" i know its for practice, i've tried it myself to make it load but i cant. Just giving my opinion, nice work to.
KingNoobie
 
Posts: 4
Joined: Fri Jul 31, 2009 5:40 pm

Re: One player Hangman

Postby damsol on Mon Sep 14, 2009 9:25 pm

KingNoobie wrote:Isnt it possible to make words load in the game? otherwise, the players can "cheat" i know its for practice, i've tried it myself to make it load but i cant. Just giving my opinion, nice work to.


Of course you can make them load in the game. There is also a way to add a menu at the beginning as an admin interface to allow a person to add their own words which when a word is added, it will be encrypted before writing to the txt file. Then when a person goes to play the game the code would randomly pick a line in the file (1 word per line) and decrypt it using the same format that it was encrypted. I'm not getting into the code for that since I'm not sure the exact syntax for encryption in C++ quite yet.
damsol
 
Posts: 3
Joined: Sun Sep 13, 2009 2:20 am

Re: One player Hangman

Postby KingNoobie on Tue Sep 15, 2009 6:35 am

damsol wrote:
KingNoobie wrote:Isnt it possible to make words load in the game? otherwise, the players can "cheat" i know its for practice, i've tried it myself to make it load but i cant. Just giving my opinion, nice work to.


Of course you can make them load in the game. There is also a way to add a menu at the beginning as an admin interface to allow a person to add their own words which when a word is added, it will be encrypted before writing to the txt file. Then when a person goes to play the game the code would randomly pick a line in the file (1 word per line) and decrypt it using the same format that it was encrypted. I'm not getting into the code for that since I'm not sure the exact syntax for encryption in C++ quite yet.


:o ok thank you for your explanation, i wanna make something like this work out one day :P
KingNoobie
 
Posts: 4
Joined: Fri Jul 31, 2009 5:40 pm

Re: One player Hangman

Postby KingNoobie on Tue Sep 15, 2009 6:36 am

damsol wrote:
KingNoobie wrote:Isnt it possible to make words load in the game? otherwise, the players can "cheat" i know its for practice, i've tried it myself to make it load but i cant. Just giving my opinion, nice work to.


Of course you can make them load in the game. There is also a way to add a menu at the beginning as an admin interface to allow a person to add their own words which when a word is added, it will be encrypted before writing to the txt file. Then when a person goes to play the game the code would randomly pick a line in the file (1 word per line) and decrypt it using the same format that it was encrypted. I'm not getting into the code for that since I'm not sure the exact syntax for encryption in C++ quite yet.


:o ok thank you for your explanation, i wanna make something like this work out one day :P
KingNoobie
 
Posts: 4
Joined: Fri Jul 31, 2009 5:40 pm


Return to Full programs

Who is online

Users browsing this forum: No registered users and 0 guests