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>!

Single player hangman

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

Single player hangman

Postby pipey85 on Sun Sep 06, 2009 4:38 pm

sorry backlash, looks like you beat me by just a few hours.... but here is the first release of my single player hangman.
I think it only works on windows im afraid and the random generator could do with tweaking but im working on it.... enjoy this working version.

Comments always welcome.

Code: Select all
#include <iostream>
#include <string>
#include <windows.h>
#include <iomanip>
using namespace std;

void displayMain (string word, bool *pisMatchingLetter, bool *pisLetterLight, int lives);
int loseLife ();
bool guessGo (string word, bool *pisMatchingLetter, bool *pisLetterLight, int lives);
void displayAlpha (bool *pisLetterLight);
string chooseWord5 ();
string chooseWord7 ();
string chooseWord9 ();

void main ()
{
   char playAgain = 'a';

while (playAgain != 'n' && playAgain != 'N')
{
   int difficulty, lives = 9;
   string word;
   bool isMatchingLetter[10];
   bool *pisMatchingLetter = &isMatchingLetter[0];
   bool wordComplete = false;
   int letterCount;
   int wordlen;
   bool isLetterLight[26];
   bool *pisLetterLight = &isLetterLight[0];

   for (int a = 0; a <= 25; a++)   isLetterLight[a] = true;
   



   system("cls");
   cout << "Welcome to Pipeys Hangman\n\nPlease choose your difficulty\n\n";
   cout << "1.Easy\n2.Medium\n3.Hard >> ";
   cin >> difficulty;

   switch (difficulty)
   {
   case 1:{
         word = chooseWord5(); break;
         }
   case 2:{
         word = chooseWord7(); break;
         }
   case 3:{
         word = chooseWord9(); break;
         }
   }

   wordlen = word.length();

   for (int a = 0; a < word.length(); a++)
   {
         
      isMatchingLetter[a] = false;
   }

   while (lives && !wordComplete)      //gameplay begins here
   {
   if(!guessGo(word, pisMatchingLetter, pisLetterLight, lives)) lives--;
   letterCount = 0;
      for (int a = 0; a < word.length(); a++)
      {         
         if (pisMatchingLetter[a]) letterCount++;
         if (word.length() == letterCount) wordComplete = true;         
      }
   }
   displayMain(word, pisMatchingLetter, pisLetterLight, lives);
   if (wordComplete) cout << "\n\n\n" << setw(20) << "CONGRATULATIONS YOU WON!!!";
   else cout << "\nToo bad you lose EPIC FAILURE!\t\t\t the word was " << word;

   cout << "\nPlay again? [y/n] ";
   cin >> playAgain;


}//end of playAgain
}

string chooseWord5 ()
{
   string word;
   char wordList[][6] = {"JOINT", "COUCH", "COTCH", "SNIFF", "GAMER"};
   int wordChoice = rand() % 5; //digit is equal to word list length, maybe i should enum them??
   word = wordList[wordChoice];

   return word;
}

string chooseWord7()
{
   string word;
   char wordList[][8] = {"AEROSOL", "AIRSHOW", "AIRSHIP", "AROUSED", "BAFFELD", "BALDING", "BIOCIDE", "KNEECAP"};
   int wordChoice = rand() % 8;
   word = wordList[wordChoice];

   return word;
}

string chooseWord9()
{
   string word;
   char wordList[][10] = {"ILLICITLY", "ANTITOXIC", "NARCOTICS", "BIOHAZARD", "CENSORING", "COCKTAILS", "CONSCRIPT"};
   int wordChoice = rand() % 6;
   word = wordList[wordChoice];

   return word;
}

bool guessGo (string word, bool *pisMatchingLetter, bool *pisLetterLight, int lives)
{
   displayMain(word, pisMatchingLetter, pisLetterLight, lives);
   bool rtrnTrue = false;
   char userLetter;

   cout << "\nGuess a letter >> ";
   cin >> userLetter;
   userLetter = toupper(userLetter);
   
   char a = 'A';

   for (int b = 0; a <= 'Z'; a++, b++)
      if (userLetter == a) pisLetterLight[b] = false;


   for (int a = 0; a < word.length(); a++)
   {
      if (userLetter == word[a])
      {
         rtrnTrue = true;
         pisMatchingLetter[a] = true;
      }
   }

   if (rtrnTrue) return true;
   else return false;

}

void displayMain(string word, bool *pisMatchingLetter, bool *pisLetterLight, int lives)
{
   system("cls");
   displayAlpha(pisLetterLight);
   cout << "\n\n\n\n\n\n\n\n\n" << setw(40) << right;
   for (int a = 0; a < word.length(); a++)
   {
      if (pisMatchingLetter[a]) cout << word[a] << "  ";
      else cout << "_  ";
   }
   cout << "\n\nLives left = " << lives;
}


void displayAlpha(bool *pisLetterLight)
{
   char b = 'A';
   cout << setw(15);
   for (int a = 0; b <= 'Z'; a++, b++)
   {
      if (pisLetterLight[a])
         cout << right << b << ' ';
      else cout << "  ";
   }
}
Pardon my n00bness, what I lack in C++ I make up for in time spent reading about C++ :P
pipey85
 
Posts: 40
Joined: Mon Jun 22, 2009 6:11 pm

Re: Single player hangman

Postby glinka57 on Tue Sep 08, 2009 10:46 pm

Welldone pipey,, this is actualy fun. I guess u used some of backlash's code? . neat anyhow XD. I'll be posting a prog this weekend, you will like it.
User avatar
glinka57
 
Posts: 195
Joined: Fri Feb 27, 2009 7:32 pm

Re: Single player hangman

Postby pipey85 on Thu Sep 10, 2009 4:51 am

i didnt use any of backlash's code, i spent a week coming up with the algorithms and when i came to post it, backlash had just posted a two player hangman game lol, im working on a new game myslef... first time at using classes, i think it will be loved =D, looking forward to yours though glinka!
Pardon my n00bness, what I lack in C++ I make up for in time spent reading about C++ :P
pipey85
 
Posts: 40
Joined: Mon Jun 22, 2009 6:11 pm


Return to Full programs

Who is online

Users browsing this forum: No registered users and 0 guests

cron