Code: Select all
// Hangman
// Uses io manipulators to format output to
// create a crude hangman game.
#include <iostream>
#include <iomanip>
#include <cctype>
#include <string>
#include <cstdlib>
#include <ctime>
#define WRONG false
using namespace std;
bool GameOver;
bool Win;
bool quit;
char Screen[21][20];
char alphabet[26];
int numWrong;
string Word;
string SoFar;
void UpdateHangman(int wrong)
{
switch(wrong)
{
case 0: // Clears Board
for(int j=0;j<21;j++)
{
for(int k=0;k<20;k++)
Screen[j][k]=32;
}
// Creates Stand
for(int x=4; x<=12; x++)
Screen[0][x]='X';
for(int y=1; y<20;y++)
Screen[y][4]='X';
for(int x=0; x<9; x++)
Screen[20][x]='X';
Screen[1][12]='X';
Screen[2][12]='X';
break;
case 1: // Creates Head
Screen[3][11]='X';
Screen[3][12]='X';
Screen[3][13]='X';
Screen[4][10]='X';
Screen[4][11]='X';
Screen[4][12]='X';
Screen[4][13]='X';
Screen[4][14]='X';
Screen[5][11]='X';
Screen[5][12]='X';
Screen[5][13]='X';
Screen[6][12]='X';
break;
case 2: // Creates Body
for(int y=7; y<13; y++)
for(int x=11; x<14; x++)
Screen[y][x]='X';
break;
case 3: // Creates Left Arm
for(int y=3,x=6; y<8; y++,x++)
Screen[y][x]='X';
break;
case 4: // Creates Right Arm
for(int y=7,x=14; y>2; y--,x++)
Screen[y][x]='X';
break;
case 5: // Creates Left Leg
for(int x=11,y=12; y<17; y++,x--)
Screen[y][x]='X';
break;
case 6: // Creates Right Leg
for(int x=13,y=12; y<17; y++,x++)
Screen[y][x]='X';
break;
default:
break;
}
if(numWrong >= 6)
GameOver = true;
return;
}
void DisplayHangman()
{
for(int y=0; y<21; y++)
{
for(int x=0; x<20; x++)
cout << Screen[y][x];
cout << endl;
}
}
void InitAlph()
{
char x='A';
for(int j=0; j<26; j++)
alphabet[j]=x++;
}
void DisplayLetterBox()
{
cout << setw(5+15) << "XXXXXXXXXXXXXXXXX" << endl;
cout << setw(5+15) << "X X" << endl;
cout << setw(5) << "X ";
for(int j=0; j<26; j++)
{
cout << alphabet[j];
if(j==12)
{
cout << " X" << endl;
cout << setw(5) << "X ";
}
}
cout << " X" << endl;
cout << setw(5+15) << "X X" << endl;
cout << setw(5+15) << "XXXXXXXXXXXXXXXXX" << endl;
}
bool AlreadyGuessed(char guess)
{
for(int j=0; j<26;j++)
{
if(alphabet[j] == guess)
{
alphabet[j]=32; // Set equal to space
return 0;
}
}
return 1;
}
bool UpdateWord(char guess)
{
bool InWord = false;
for(int j=0; j<Word.length(); j++)
{
if(Word[j] == guess)
{
SoFar[j] = guess;
InWord = true;
}
}
if(SoFar == Word)
Win = true;
return InWord;
}
void DisplayWord()
{
int CenterPoint = Word.length()/2;
int AlignPoint = 10 - CenterPoint+Word.length();
cout << setw(AlignPoint) << SoFar << endl;
}
void FinishGame()
{
char Again;
DisplayHangman();
cout << endl;
DisplayLetterBox();
cout << endl;
int CenterPoint = Word.length()/2;
int AlignPoint = 10 - CenterPoint+Word.length();
cout << setw(AlignPoint) << Word << endl;
cout << endl;
if(GameOver == true)
cout << setw(15) << "You Lose! ";
if(Win == true)
cout << setw(14) << "You Win! ";
cout << "Play Again?";
cin >> Again;
if(Again == 'n' || Again == 'N')
quit = true;
cout << endl;
}
void CloseGame()
{
cout << endl << endl << endl << endl << endl << endl << endl << endl;
cout << endl << endl << endl << endl << endl << endl << endl << endl;
cout << endl << endl << endl << endl << endl << endl << endl << endl;
cout << setw(25) << " " << "Thank You For Playing!" << endl << endl << endl;
cout << "Play Again some time. ";
cout << endl;
cout << endl << setw(10) << " " << "Games 4 The Future" << endl << setw(10);
cout << endl << endl << endl << endl << endl << endl << endl << endl;
cout << endl << endl << endl << endl << endl << endl << endl << endl;
for(int j=0; j!=-1; j++);
for(int j=0; j!=-1; j++);
}
int main()
{
quit = false;
char guess;
while(quit != true)
{
numWrong = 0;
GameOver = false;
Win = false;
PickWord();
InitAlph();
UpdateHangman(numWrong);
DisplayHangman();
cout << endl;
DisplayLetterBox();
cout << endl;
DisplayWord();
while(1)
{
cout << endl;
cout << setw(10) << "Guess:";
cin >> guess;
cout << endl;
guess = toupper(guess);
if(AlreadyGuessed(guess)!= true)
{
if(UpdateWord(guess)== WRONG)
{
numWrong++;
UpdateHangman(numWrong);
}
}
if(GameOver == true || Win == true)
break;
DisplayHangman();
cout << endl;
DisplayLetterBox();
cout << endl;
DisplayWord();
}
FinishGame();
}
CloseGame();
return 0;
}
// Contains the words to use in Hangman
const int NumWords = 100;
void PickWord()
{
srand(time(0));
int random = rand() % NumWords +1;
switch(random)
{
case 1: Word = "ABACUS";
break;
case 2: Word = "ABANDON";
break;
case 3: Word = "ABATE";
break;
case 4: Word = "ABBEY";
break;
case 5: Word = "ABBREVIATE";
break;
case 6: Word = "ABDOMEN";
break;
case 7: Word = "ABDUCT";
break;
case 8: Word = "ABERRATION";
break;
case 9: Word = "ABET";
break;
case 10: Word = "ABIDE";
break;
case 11: Word = "ABILITY";
break;
case 12: Word = "ABLEBODIED";
break;
case 13: Word = "ANTI-BALLISTIC MISSLE";
break;
case 14: Word = "ABNORMAL";
break;
case 15: Word = "ABOARD";
break;
case 16: Word = "ABODE";
break;
case 17: Word = "ABOLISH";
break;
case 18: Word = "ABOLITION";
break;
case 19: Word = "ABOMINABLE SNOW-MAN";
break;
case 20: Word = "ABORITON";
break;
case 21: Word = "ABOUT";
break;
case 22: Word = "ABOVE";
break;
case 23: Word = "ABRASIVE";
break;
case 24: Word = "ABROGATE";
break;
case 25: Word = "ABRUPT";
break;
case 26: Word = "ABSCISSA";
break;
case 27: Word = "ABSENT";
break;
case 28: Word = "ABSOLUTE";
break;
case 29: Word = "ABSOLUTION";
break;
case 30: Word = "ABSOLVE";
break;
case 31: Word = "ABSORB";
break;
case 32: Word = "ABSTAIN";
break;
case 33: Word = "ABSTRACT";
break;
case 34: Word = "ABSURD";
break;
case 35: Word = "ABUNDANCE";
break;
case 36: Word = "ABUSE";
break;
case 37: Word = "ABYSMAL";
break;
case 38: Word = "ACADEMIC";
break;
case 39: Word = "ACAPPELLA";
break;
case 40: Word = "ACCELERATOR";
break;
case 41: Word = "ACCENT";
break;
case 42: Word = "ACCEPT";
break;
case 43: Word = "ACCEPTANCE";
break;
case 44: Word = "ACCESS";
break;
case 45: Word = "ACCORDING";
break;
case 46: Word = "ACCORDION";
break;
case 47: Word = "ACCOUNT";
break;
case 48: Word = "ACCUSE";
break;
case 49: Word = "ACE";
break;
case 50: Word = "ACE BANDAGE";
break;
case 51: Word = "ACE IN THE HOLE";
break;
case 52: Word = "ACETATE";
break;
case 53: Word = "ACHIEVE";
break;
case 54: Word = "ACID";
break;
case 55: Word = "ACID RAIN";
break;
case 56: Word = "ACNE ROCKETS";
break;
case 57: Word = "ACRE";
break;
case 58: Word = "ACROSS";
break;
case 59: Word = "ACROSS THE BOARD";
break;
case 60: Word = "ACTIVE";
break;
case 61: Word = "ACTOR";
break;
case 62: Word = "ACTRESS";
break;
case 63: Word = "ACTUAL";
break;
case 64: Word = "ACUTE";
break;
case 65: Word = "ANNO DOMINI";
break;
case 66: Word = "ADAMS FAMILY";
break;
case 67: Word = "ADAM'S APPLE";
break;
case 68: Word = "ADD";
break;
case 69: Word = "ADDEND";
break;
case 70: Word = "ADDER";
break;
case 71: Word = "ADDICT";
break;
case 72: Word = "ADDITION";
break;
case 73: Word = "ADDRESS";
break;
case 74: Word = "ADJUST";
break;
case 75: Word = "ADMINISTER";
break;
case 76: Word = "ADMINISTRATE";
break;
case 77: Word = "ADMIRE";
break;
case 78: Word = "ADMIT";
break;
case 79: Word = "ADOLESCENCE";
break;
case 80: Word = "ADOPT";
break;
case 81: Word = "ADVANCE";
break;
case 82: Word = "ADVENTURE";
break;
case 83: Word = "ADVERB";
break;
case 84: Word = "ADVERTISE";
break;
case 85: Word = "ADVICE";
break;
case 86: Word = "ADVISE";
break;
case 87: Word = "ADVOCATE";
break;
case 88: Word = "AEGEAN SEA";
break;
case 89: Word = "AERIAL";
break;
case 90: Word = "AEROBATICS";
break;
case 91: Word = "AEROBIC";
break;
case 92: Word = "AERODYNAMICS";
break;
case 93: Word = "AERONAUTICS";
break;
case 94: Word = "AEROSOL CAN";
break;
case 95: Word = "AEROSPACE";
break;
case 96: Word = "AFFABLE";
break;
case 97: Word = "AFFAIR";
break;
case 98: Word = "AFFECTIONATE";
break;
case 99: Word = "AFFILIATE";
break;
case 100: Word = "AFFINITY";
break;
default: Word = "MISTAKE";
break;
}
SoFar = Word;
for(int j = 0; j<SoFar.length(); j++)
{
if(SoFar[j] != 32 && SoFar[j] != '-' && SoFar[j] != ''')
SoFar[j] = '?';
else;
}
}