Skip to content

Instantly share code, notes, and snippets.

@soachishti
Last active December 3, 2015 18:17
Show Gist options
  • Save soachishti/d9bc40f60e656c34ccb2 to your computer and use it in GitHub Desktop.
Save soachishti/d9bc40f60e656c34ccb2 to your computer and use it in GitHub Desktop.
Hanging Man
/*
* Name: Syed Owais Ali Chishti
* Task: Hangman
* Major: BS(CS)
*/
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <iomanip>
#include <conio.h>
using namespace std;
/*
* Draw hangman to STDOUT
*
*@param tryUse Number of tries used
*@return To STDOUT
*/
void hangman(int tryUse = 1){
cout<<endl<<endl;
switch(tryUse){
case 1:
cout<<"------------------"<<endl;
cout<<"| | "<<endl;
cout<<"| "<<endl;
cout<<"| "<<endl;
cout<<"| (*_*) "<<endl;
cout<<"| /-------/ "<<endl;
cout<<"| | "<<endl;
cout<<"| | "<<endl;
cout<<"| | "<<endl;
cout<<"| / \\ "<<endl;
cout<<"| / \\ "<<endl;
cout<<"------------------"<<endl;
break;
case 2:
cout<<"------------------"<<endl;
cout<<"| | "<<endl;
cout<<"| "<<endl;
cout<<"| (*o*) "<<endl;
cout<<"| /-------/ "<<endl;
cout<<"| | "<<endl;
cout<<"| | "<<endl;
cout<<"| | "<<endl;
cout<<"| / \\ "<<endl;
cout<<"| / \\ "<<endl;
cout<<"| "<<endl;
cout<<"------------------"<<endl;
break;
case 3:
cout<<"------------------"<<endl;
cout<<"| | "<<endl;
cout<<"| (-_-) "<<endl;
cout<<"| /-------/ "<<endl;
cout<<"| | "<<endl;
cout<<"| | "<<endl;
cout<<"| | "<<endl;
cout<<"| / \\ "<<endl;
cout<<"| / \\ "<<endl;
cout<<"| "<<endl;
cout<<"| "<<endl;
cout<<"------------------"<<endl;
}
}
int main() {
const int SIZE = 11;
//Word List
string words[SIZE];
words[0] = "thesaurus";
words[1] = "pakhtunkhwa";
words[2] = "crypt";
words[3] = "curacao";
words[4] = "inaccessible ";
words[5] = "neutral";
words[6] = "procastination";
words[7] = "strengths";
words[8] = "chishti";
words[9] = "unknown";
words[10] = "unbeatable";
cout<<endl<<endl<<endl<<endl<<endl<<endl;
cout<<setw(45)<<"*******************"<<endl<<endl;
cout<<setw(45)<<"* HANGMAN *"<<endl<<endl;
cout<<setw(45)<<"*******************"<<endl<<endl;
cout<<setw(46)<<"--- SOAC PROJECT ---"<<endl;
cout<<setw(49)<<"--- Roll no: P14-6011 ---"<<endl;
cout<<endl<<endl<<endl<<endl<<endl<<endl;
cout<<setw(80)<<"Press any key to continue.";
_getch();
system("cls");
char choice = NULL;
do {
system("cls");
int randNum, wordLen;
int correct = 0;
int space = 0;
int wrong = -1;
int preCorrect = 0;
srand((unsigned int)time(NULL));
randNum = rand() % SIZE;
wordLen = words[randNum].size();
int numOne = rand()%wordLen;
int numTwo = rand()%wordLen;
int *tmpArray = new int[wordLen];
int indexCount = 0;
int added = false;
tmpArray[indexCount++] = words[randNum][numOne];
tmpArray[indexCount++] = words[randNum][numTwo];
while(true) {
correct = preCorrect;
cout << "HANGMAN GAME - (lowercase word only)" << endl;
cout << "Word is: " << words[randNum] << endl << endl;
for(int i = 0; i < wordLen; i++) {
if(words[randNum][i] == choice) {
cout<<words[randNum][i];
tmpArray[indexCount] = choice;
indexCount++;
choice = -1;
correct++;
}
else {
for(int j = 0 ; j < indexCount ; j++){
if(words[randNum][i] == tmpArray[j]) {
cout<<words[randNum][i];
added = true;
break;
}
}
if(added == false) {
space++;
cout<<"_";
}
}
added = false;
}
if(correct == preCorrect){
wrong++;
}
cout<<endl<<(3-wrong)<<" tries left: ";
hangman(wrong);
if(wrong == 3){
cout<<"YOU LOST!!!";
break;
}
if(space == 0){
cout<<"YOU WON!!!";
break;
}
else{
space = 0;
}
cout<<endl<<"Enter Choice : ";
choice = NULL;
cin>>choice;
system("CLS");
}
cout<<endl<<endl<<"Do you want to play again (Y/N): ";
cin>>choice;
}
while(choice == 'Y' || choice == 'y');
system("PAUSE");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment