Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<conio.h>
- #include<vector>
- #include<fstream>
- #include<time.h>
- #include <cstdlib>
- #include<cstring>
- #include <iomanip>
- #include<stdio.h>
- using namespace std;
- struct hangmans
- {
- int score;
- char name[50];
- char times[50];
- };
- class hangman
- {
- public:
- char time1[50];
- int score1;
- int score2;
- char names[50];
- hangman()
- {
- score1=0;
- }
- void login();
- void board();
- void printmsg(string,bool,bool);
- void DrawHangman(int);
- void PrintLetters(string,char,char);
- void PrintAvaLet(string);
- bool PrintCheck(string,string);
- string LoadRW(string);
- string Loadhint(string);
- int triesleft(string,string);
- int hangmangame(string,string);
- void homepage();
- void signup();
- void guess();
- void scores();
- void create();
- void menu();
- void scoreboard();
- int reshint1;
- };
- void hangman::printmsg(string message, bool printTop = true, bool printBottom = true)
- {
- if(printTop)
- {
- cout<<"\t\t\t\t +--------------------------------------------------+"<<endl;
- cout<<"\t\t\t\t |";
- }
- else
- {
- cout<<"\t\t\t\t |";
- }
- bool front = true;
- for (int i = message.length();i<50;i++)
- {
- if(front)
- {
- message= " "+ message;
- }
- else
- {
- message=message +" ";
- }
- front = !front;
- }
- cout<<message.c_str();
- if(printBottom)
- {
- cout<<"|"<<endl;
- cout<<"\t\t\t\t +--------------------------------------------------+"<<endl;
- }
- else
- {
- cout<<"|"<<endl;
- }
- }
- void hangman::menu()
- {
- system("cls");
- printmsg("HANGMAN",true,true);
- printmsg("1. PLAY THE GAME",false);
- printmsg("2. Scoreboard ",false);
- printmsg("3.Exit ",false);
- switch(_getch())
- {
- case '1':guess();
- break;
- case '2': scoreboard();
- break;
- case '3': exit(0);
- break;
- default:
- cout<<"\n\n";
- printmsg("Invalid key");
- printmsg("PRESS ANY KEY",false);
- _getch();
- menu();
- break;
- }
- }
- void hangman::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 hangman::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 hangman:: PrintAvaLet(string taken)
- {
- printmsg("Available Letters",true,true);
- printmsg("",false,false);
- PrintLetters(taken,'A','M');
- printmsg("",false,false);
- PrintLetters(taken,'N','Z');
- printmsg("",false,true);
- }
- bool hangman::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 hangman::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();
- reshint1 = randomLine;
- word = v.at(randomLine);
- reader.close();
- }
- return word;
- }
- string hangman::Loadhint(string path)
- {
- int lineCount=0;
- string hint;
- vector<string> w;
- int c=0;
- ifstream reader(path.c_str());
- if(reader.is_open())
- {
- while(getline(reader, hint))
- w.push_back(hint);
- hint = w.at(reshint1);
- reader.close();
- }
- return hint;
- }
- int hangman::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 hangman::homepage()
- {
- cout<<"\n\n\n\n";
- cout<<"\t\t\t\t***************************************************************\n";
- cout<<"\t\t\t\t***************************************************************\n";
- cout<<"\t\t\t\t********-----------------------------------------------********\n";
- cout<<"\t\t\t\t******** __ __ ________ ___ __ ___________ ********\n";
- cout<<"\t\t\t\t********| | | | | __ | | \\ | | | |********\n";
- cout<<"\t\t\t\t********| |__| | | |__| | | | \\ | | | _____ |********\n";
- cout<<"\t\t\t\t********| __ | | __ | | |\\ \\| | | | | | |********\n";
- cout<<"\t\t\t\t********| | | | | | | | | | \\ | | | | 0 | |********\n";
- cout<<"\t\t\t\t********|__| |__| |__| |__| |__| \\___| | | /|\\ | |********\n";
- cout<<"\t\t\t\t********--------------------------------- | | / \\ | |********\n";
- cout<<"\t\t\t\t******** __ __ ________ ___ __ | |_____| |********\n";
- cout<<"\t\t\t\t********| \\ / | | __ | | \\ | | |________ |********\n";
- cout<<"\t\t\t\t********| \\/ | | |__| | | | \\ | | | |********\n";
- cout<<"\t\t\t\t********| |\\ /| | | __ | | |\\ \\| | ________| |********\n";
- cout<<"\t\t\t\t********| | \\/ | | | | | | | | \\ | | | |********\n";
- cout<<"\t\t\t\t********|_| |_| |__| |__| |__| \\___| |___________|********\n";
- cout<<"\t\t\t\t********-----------------------------------------------********\n";
- cout<<"\t\t\t\t***************************************************************\n";
- cout<<"\t\t\t\t*****************------------------------------- **************\n";
- cout<<"\t\t\t\t****************|PRESS ANY KEY TO ENTER THE GAME|**************\n";
- cout<<"\t\t\t\t*****************------------------------------- **************\n";
- cout<<"\t\t\t\t****************|PRESS 'X' KEY TO EXIT THE GAME|**************\n";
- cout<<"\t\t\t\t*****************------------------------------- **************\n";
- cout<<"\t\t\t\t***************************************************************\n";
- cout<<"\t\t\t\t***************************************************************\n";
- cout<<"\t\t\t\t !!!Enter Your Decision!!!";
- }
- int hangman:: hangmangame(string wordToguess1,string HINT)
- {
- string guesses;
- char b;
- bool win=false;
- int tries=0;
- do
- {
- system("cls");
- printmsg("HANG MAN");
- DrawHangman(tries);
- PrintAvaLet(guesses);
- cout<<"\n";
- printmsg("!!!HINT!!!");
- printmsg(HINT,false);
- cout<<"\n";
- printmsg("Guess The Word!!!");
- win=PrintCheck(wordToguess1,guesses);
- if (win)
- {
- break;
- }
- char x;
- x=_getch();
- x=toupper(x);
- if (guesses.find(x) == string::npos)
- guesses += x;
- tries = triesleft(wordToguess1,guesses);
- }
- while(tries<7);
- if(win)
- {
- score1++;
- cout<<"\n";
- printmsg("!!!YOU HAVE WON THE GAME!!!");
- printmsg("PRESS ANY KEY TO CONTINUE",false);
- printmsg("PRESS B TO GO BACK",false);
- printmsg("PRESS X TO EXIT THE GAME",false);
- b=_getch();
- if ((b=='x')||(b=='X'))
- {
- scores();
- exit(0);
- }
- else if ((b=='b')||(b=='B'))
- {
- score2=score1;
- scores();
- menu();
- }
- else
- {
- guess();
- }
- }
- else
- {
- scores();
- cout<<"\n";
- printmsg("!!!GAME-OVER!!!");
- printmsg("THE CORRECT WORD IS",false);
- printmsg(wordToguess1,false);
- _getch();
- system("cls");
- cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n";
- printmsg("",true,false);
- printmsg("PRESS X TO EXIT THE GAME",false,false);
- printmsg("",false,true);
- printmsg("",false,false);
- printmsg("PRESS ANY KEY TO GO BACK",false,false);
- printmsg("",false,true);
- b=_getch();
- if(b=='x'||b=='X')
- {
- exit(0);
- }
- else
- {
- menu();
- }
- }
- }
- void hangman::board()
- {
- top:
- system("cls");
- printmsg("HANGMAN",true,true);
- printmsg("1. Sign up",false);
- printmsg("2. login",false);
- printmsg("3.Exit ",false);
- switch(_getch())
- {
- case '1':signup();
- break;
- case '2':login();
- break;
- case '3':exit(0);
- default:cout<<"Invalid key"<<endl;
- _getch();
- goto top;
- }
- }
- void hangman::signup()
- {
- int o;
- struct hangmans p;
- struct hangmans q;
- system("cls");
- printmsg("HANGMAN");
- FILE *f;
- FILE *fb;
- f=fopen("Players.txt","ab");
- f=fopen("Players.txt","rb");
- fb=fopen("Players.txt","ab");
- printmsg("Enter name:",false);
- printf("\n\t\t\t\t");
- scanf("%s",&names);
- fflush(stdin);
- fwrite(&p, sizeof(p), 1, f);
- while (fread(&p,sizeof(p),1,f)==1)
- {
- if (strcmp(names,p.name)==0)
- {
- fclose(f);
- o=1;
- }
- }
- if (o!=1)
- {
- strcpy(q.name,names);
- fwrite(&q, sizeof(q), 1, fb);
- printf("\t\t\tThe user is available\n");
- fclose(fb);
- _getch();
- board();
- }
- else if (o==1){
- printf("\t\t\tThe user already exists!!\n");
- _getch();
- board();
- }
- }
- void hangman::login()
- {
- system("cls");
- struct hangmans q;
- FILE *fb;
- fb=fopen("Players.txt","rb");
- printmsg("HANGMAN");
- printmsg("Enter name: ",false);
- scanf("%s",&names);
- fflush(stdin);
- int o;
- while(fread(&q,sizeof(q),1,fb)==1)
- {
- if (strcmp(names,q.name)==0)
- {
- o=1;
- }
- }
- if (o!=1)
- {
- cout<<"The user doesn't exists"<<endl;
- fclose(fb);
- _getch();
- board();
- }
- else if (o==1)
- {
- cout<<"The user exists"<<endl;
- _getch();
- create();
- menu();
- }
- }
- void hangman::create()
- {
- time_t rawtime;
- struct tm * timeinfo;
- time ( &rawtime );
- timeinfo = localtime ( &rawtime );
- strcpy(time1,asctime(timeinfo));
- }
- void hangman::scores()
- {
- struct hangmans r;
- FILE *fb;
- fb=fopen("Playerss.txt","ab");
- strcpy(r.name,names);
- r.score=score2;
- strcpy(r.times,time1);
- fwrite(&r, sizeof(r), 1, fb);
- fclose(fb);
- }
- void hangman:: guess()
- {create();
- srand(time(0));
- string wordToguess;
- wordToguess = LoadRW("ans.txt");
- string HINT;
- HINT = Loadhint("ques.txt");
- hangmangame(wordToguess,HINT);
- }
- void hangman::scoreboard()
- {
- char a;
- system("cls");
- struct hangmans r;
- FILE *f;
- f=fopen("Playerss.txt","rb");
- printmsg("SCOREBOARD",true,true);
- if (f==NULL)
- {
- printf("\nERROR OPENING FILE, THE RECORD FILE DOES NOT EXIST!!!");
- getch();
- menu();
- }
- while(fread(&r,sizeof(r),1,f)==1)
- {
- printf("\n\t\t\t\t\t\tName: %s\n\t\t\t\t\t\tScore: %d\n\t\t\t\t\t\tDate and time: %s\n\n",r.name,r.score,r.times);
- }
- fclose(f);
- printmsg("PRESS ANY KEY TO GO BACK",true,false);
- printmsg("PRESS X TO EXIT",true,true);
- a=_getch();
- if ((a=='x')||(a=='X'))
- {
- exit(0);
- }
- else
- {
- menu();
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////
- //DRIVER CODE................................................................
- int main()
- {
- hangman p;
- char a;
- p.homepage();
- a=_getch();
- if(a=='x'||a=='X')
- {
- exit(0);
- }
- else
- {
- p.board();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement