Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include<fstream>
- #include<time.h>
- #include <cstdlib>
- #include<cstring>
- using namespace std;
- void printmsg(string message, bool printTop = true, bool printBottom = true)
- {
- if(printTop)
- {
- cout<<"+---------------------------------+"<<endl;
- cout<<"|";
- }
- else
- {
- cout<<"|";
- }
- bool front = true;
- for (int i = message.length();i<33;i++)
- {
- if(front)
- {
- message= " "+ message;
- }
- else
- {
- message=message +" ";
- }
- front = !front;
- }
- cout<<message.c_str();
- if(printBottom)
- {
- cout<<"|"<<endl;
- cout<<"+---------------------------------+"<<endl;
- }
- else
- {
- cout<<"|"<<endl;
- }
- }
- void DrawHangman(int guessCount=0)
- {
- printmsg("|",false,false);
- printmsg("|",false,false);
- if(guessCount>=1)
- {
- printmsg("O",false,false);
- }
- else
- {
- printmsg("",false,false);
- }
- if(guessCount==2)
- {
- printmsg("|",false,false);
- }
- if(guessCount==3)
- {
- printmsg("/| ",false,false);
- }
- if(guessCount>=4)
- {
- printmsg("/|\\",false,false);
- }
- else
- {
- printmsg("",false,false);
- }
- if(guessCount==5)
- {
- printmsg("/ ",false,false);
- }
- if(guessCount==6)
- {
- printmsg("/ \\",false,false);
- }
- else
- {
- printmsg("",false,false);
- }
- }
- void PrintLetters(string input, char from, char to)
- {
- string s;
- for(char i= from;i<=to;i++)
- {
- if (input.find(i) == string::npos)
- {
- s += i;
- s += " ";
- }
- else
- {
- s += " ";
- }
- }
- printmsg(s,false,false);
- }
- void PrintAvaLet(string taken)
- {
- printmsg("Available Letters",true,true);
- PrintLetters(taken,'A','M');
- PrintLetters(taken,'N','Z');
- }
- bool PrintCheck(string word, string guessed)
- {
- bool won= true;
- string s;
- for(int i=0;i<word.length();i++)
- {
- if(guessed.find(word[i])== string::npos)
- {
- won = false;
- s += "_ ";
- }
- else
- {
- s += word[i];
- s += " ";
- }
- }
- printmsg(s,false);
- return won;
- }
- string LoadRW(string path)
- {
- int lineCount=0;
- string word;
- vector<string> v;
- int c=0;
- ifstream reader(path.c_str());
- if(reader.is_open())
- {
- while(getline(reader, word))
- v.push_back(word);
- int randomLine=rand()%v.size();
- word = v.at(randomLine);
- reader.close();
- }
- return word;
- }
- int triesleft(string word, string guessed)
- {
- int error=0;
- for(int i=0; i < guessed.length();i++)
- {
- if(word.find(guessed[i])==string::npos)
- error++;
- }
- return error;
- }
- void homepage()
- {
- cout<<"***************************************************************\n";
- cout<<"***************************************************************\n";
- cout<<"********-----------------------------------------------********\n";
- cout<<"******** __ __ ________ ___ __ ___________ ********\n";
- cout<<"********| | | | | __ | | \\ | | | |********\n";
- cout<<"********| |__| | | |__| | | | \\ | | | _____ |********\n";
- cout<<"********| __ | | __ | | |\\ \\| | | | | | |********\n";
- cout<<"********| | | | | | | | | | \\ | | | | 0 | |********\n";
- cout<<"********|__| |__| |__| |__| |__| \\___| | | /|\\ | |********\n";
- cout<<"********--------------------------------- | | / \\ | |********\n";
- cout<<"******** __ __ ________ ___ __ | |_____| |********\n";
- cout<<"********| \\ / | | __ | | \\ | | |________ |********\n";
- cout<<"********| \\/ | | |__| | | | \\ | | | |********\n";
- cout<<"********| |\\ /| | | __ | | |\\ \\| | ________| |********\n";
- cout<<"********| | \\/ | | | | | | | | \\ | | | |********\n";
- cout<<"********|_| |_| |__| |__| |__| \\___| |___________|********\n";
- cout<<"********-----------------------------------------------********\n";
- cout<<"***************************************************************\n";
- cout<<"*****************------------------------------- **************\n";
- cout<<"****************|PRESS ANY KEY TO ENTER THE GAME|**************\n";
- cout<<"*****************------------------------------- **************\n";
- cout<<"****************|PRESS 'X' KEY TO EXIT THE GAME|**************\n";
- cout<<"*****************------------------------------- **************\n";
- cout<<"***************************************************************\n";
- cout<<"***************************************************************\n";
- cout<<" Enter Your Decision :";
- }
- int main()
- {
- srand(time(0));
- string guesses;
- string wordToguess;
- wordToguess = LoadRW("dic.txt");
- homepage();
- char a= getchar();
- if(a=='x'||a=='X')
- {
- exit(0);
- }
- else{
- bool win=false;
- int tries=0;
- do
- {
- system("cls");
- printmsg("HANG MAN");
- DrawHangman(tries);
- PrintAvaLet(guesses);
- printmsg("Guess The Word!!!");
- win=PrintCheck(wordToguess,guesses);
- if (win)
- {
- break;
- }
- char x;
- cout<< ">";
- x=_getch();
- x=toupper(x);
- if (guesses.find(x) == string::npos)
- guesses += x;
- tries = triesleft(wordToguess,guesses);
- }
- while(tries<7);
- if(win){printmsg("!!!YOU HAVE WON THE GAME!!!");
- exit(0);}
- else {printmsg("!!!GAME-OVER!!!");}
- system("pause");
- getchar();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement