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 << " ";
}
}
