Advertisement
Sheyshya

hangman final 1

Sep 21st, 2019
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.21 KB | None | 0 0
  1. #include<iostream>
  2. #include<conio.h>
  3. #include<vector>
  4. #include<fstream>
  5. #include<time.h>
  6. #include <cstdlib>
  7. #include<cstring>
  8. #include <iomanip>
  9. #include<stdio.h>
  10. using namespace std;
  11.  
  12.     struct hangmans
  13.     {
  14.         int score;
  15.         char name[50];
  16.         char times[50];
  17.     };
  18.  
  19.     class hangman
  20.     {
  21.  
  22.     public:
  23. char time1[50];
  24.     int score1;
  25.      int score2;
  26.     char names[50];
  27.     hangman()
  28.         {
  29.             score1=0;
  30.         }
  31.         void login();
  32.         void board();
  33.     void printmsg(string,bool,bool);
  34.     void DrawHangman(int);
  35.     void PrintLetters(string,char,char);
  36.     void PrintAvaLet(string);
  37.     bool PrintCheck(string,string);
  38.     string LoadRW(string);
  39.     string Loadhint(string);
  40.     int triesleft(string,string);
  41.     int hangmangame(string,string);
  42.     void homepage();
  43. void signup();
  44.     void guess();
  45.     void scores();
  46.     void create();
  47.     void menu();
  48.     void scoreboard();
  49.     int reshint1;
  50. };
  51.  
  52. void hangman::printmsg(string message, bool printTop = true, bool printBottom = true)
  53. {
  54.     if(printTop)
  55.     {
  56.         cout<<"\t\t\t\t     +--------------------------------------------------+"<<endl;
  57.         cout<<"\t\t\t\t     |";
  58.     }
  59.     else
  60.     {
  61.         cout<<"\t\t\t\t     |";
  62.     }
  63.     bool front = true;
  64.     for (int i = message.length();i<50;i++)
  65.     {
  66.         if(front)
  67.             {
  68.                 message= " "+ message;
  69.             }
  70.         else
  71.             {
  72.                 message=message +" ";
  73.             }
  74.         front = !front;
  75.     }
  76.     cout<<message.c_str();
  77.     if(printBottom)
  78.         {
  79.             cout<<"|"<<endl;
  80.             cout<<"\t\t\t\t     +--------------------------------------------------+"<<endl;
  81.         }
  82.     else
  83.     {
  84.         cout<<"|"<<endl;
  85.     }
  86. }
  87.  
  88.  
  89. void hangman::menu()
  90. {
  91.  
  92.     system("cls");
  93.     printmsg("HANGMAN",true,true);
  94.     printmsg("1. PLAY THE GAME",false);
  95.     printmsg("2. Scoreboard ",false);
  96.     printmsg("3.Exit ",false);
  97.     switch(_getch())
  98.     {
  99.         case '1':guess();
  100.         break;
  101.  
  102.         case '2': scoreboard();
  103.         break;
  104.  
  105.         case '3': exit(0);
  106.         break;
  107.  
  108.         default:
  109.             cout<<"\n\n";
  110.             printmsg("Invalid key");
  111.             printmsg("PRESS ANY KEY",false);
  112.             _getch();
  113.             menu();
  114.         break;
  115.     }
  116. }
  117.  
  118. void hangman::DrawHangman(int guessCount=0)
  119. {
  120.     printmsg("|",false,false);
  121.     printmsg("|",false,false);
  122.  
  123.     if(guessCount>=1)
  124.     {
  125.         printmsg("O",false,false);
  126.     }
  127.     else
  128.     {
  129.         printmsg("",false,false);
  130.     }
  131.     if(guessCount==2)
  132.     {
  133.         printmsg("|",false,false);
  134.     }
  135.     if(guessCount==3)
  136.     {
  137.         printmsg("/| ",false,false);
  138.     }
  139.     if(guessCount>=4)
  140.     {
  141.         printmsg("/|\\",false,false);
  142.     }
  143.     else
  144.     {
  145.         printmsg("",false,false);
  146.     }
  147.     if(guessCount==5)
  148.     {
  149.         printmsg("/  ",false,false);
  150.     }
  151.     if(guessCount==6)
  152.     {
  153.         printmsg("/ \\",false,false);
  154.     }
  155.     else
  156.     {
  157.         printmsg("",false,false);
  158.     }
  159. }
  160. void hangman::PrintLetters(string input, char from, char to)
  161. {
  162.     string s;
  163.     for(char i= from;i<=to;i++)
  164.     {
  165.         if (input.find(i) == string::npos)
  166.         {
  167.             s += i;
  168.             s += "  ";
  169.         }
  170.         else
  171.         {
  172.             s += "   ";
  173.         }
  174.     }
  175.     printmsg(s,false,false);
  176. }
  177. void hangman:: PrintAvaLet(string taken)
  178. {
  179.     printmsg("Available Letters",true,true);
  180.     printmsg("",false,false);
  181.     PrintLetters(taken,'A','M');
  182.     printmsg("",false,false);
  183.     PrintLetters(taken,'N','Z');
  184.     printmsg("",false,true);
  185. }
  186. bool hangman::PrintCheck(string word, string guessed)
  187. {
  188.     bool won= true;
  189.     string s;
  190.     for(int i=0;i<word.length();i++)
  191.     {
  192.         if(guessed.find(word[i])== string::npos)
  193.         {
  194.             won = false;
  195.             s += "_ ";
  196.         }
  197.         else
  198.         {
  199.             s += word[i];
  200.             s += " ";
  201.         }
  202.     }
  203.     printmsg(s,false);
  204.     return won;
  205. }
  206. string hangman::LoadRW(string path)
  207. {
  208.     int lineCount=0;
  209.     string word;
  210.     vector<string> v;
  211.     int c=0;
  212.     ifstream reader(path.c_str());
  213.     if(reader.is_open())
  214.     {
  215.         while(getline(reader, word))
  216.         v.push_back(word);
  217.         int randomLine=rand()%v.size();
  218.         reshint1 = randomLine;
  219.         word = v.at(randomLine);
  220.         reader.close();
  221.     }
  222.     return word;
  223. }
  224. string hangman::Loadhint(string path)
  225. {
  226.     int lineCount=0;
  227.     string hint;
  228.     vector<string> w;
  229.     int c=0;
  230.     ifstream reader(path.c_str());
  231.     if(reader.is_open())
  232.     {
  233.         while(getline(reader, hint))
  234.         w.push_back(hint);
  235.         hint = w.at(reshint1);
  236.         reader.close();
  237.     }
  238.     return hint;
  239. }
  240. int hangman::triesleft(string word, string guessed)
  241. {
  242.     int error=0;
  243.     for(int i=0; i < guessed.length();i++)
  244.     {
  245.     if(word.find(guessed[i])==string::npos)
  246.     error++;
  247.     }
  248.     return error;
  249. }
  250. void hangman::homepage()
  251. {
  252.     cout<<"\n\n\n\n";
  253.     cout<<"\t\t\t\t***************************************************************\n";
  254.     cout<<"\t\t\t\t***************************************************************\n";
  255.     cout<<"\t\t\t\t********-----------------------------------------------********\n";
  256.     cout<<"\t\t\t\t******** __    __   ________   ___    __   ___________ ********\n";
  257.     cout<<"\t\t\t\t********|  |  |  | |   __   | |   \\  |  | |       |********\n";
  258.     cout<<"\t\t\t\t********|  |__|  | |  |__|  | |  | \\ |  | |   _____   |********\n";
  259.     cout<<"\t\t\t\t********|   __    | |   __   | |  |\\ \\|  | |  |  |  |  |********\n";
  260.     cout<<"\t\t\t\t********|  |  |  | |  |  |  | |  | \\ |  | |  |  0  |  |********\n";
  261.     cout<<"\t\t\t\t********|__|  |__| |__|  |__| |__|  \\___| |  | /|\\ |  |********\n";
  262.     cout<<"\t\t\t\t********--------------------------------- |  | / \\ |  |********\n";
  263.     cout<<"\t\t\t\t******** __    __   ________   ___    __  |  |_____|  |********\n";
  264.     cout<<"\t\t\t\t********|  \\  /  | |   __   | |   \\  |  | |________   |********\n";
  265.     cout<<"\t\t\t\t********|   \\/   | |  |__|  | |  | \\ |  |          |  |********\n";
  266.     cout<<"\t\t\t\t********| |\\  /| | |   __   | |  |\\ \\|  |  ________|  |********\n";
  267.     cout<<"\t\t\t\t********| | \\/ | | |  |  |  | |  | \\ |  | |           |********\n";
  268.     cout<<"\t\t\t\t********|_|    |_| |__|  |__| |__|  \\___| |___________|********\n";
  269.     cout<<"\t\t\t\t********-----------------------------------------------********\n";
  270.     cout<<"\t\t\t\t***************************************************************\n";
  271.     cout<<"\t\t\t\t*****************------------------------------- **************\n";
  272.     cout<<"\t\t\t\t****************|PRESS ANY KEY TO ENTER THE GAME|**************\n";
  273.     cout<<"\t\t\t\t*****************------------------------------- **************\n";
  274.     cout<<"\t\t\t\t****************|PRESS 'X' KEY TO  EXIT THE GAME|**************\n";
  275.     cout<<"\t\t\t\t*****************------------------------------- **************\n";
  276.     cout<<"\t\t\t\t***************************************************************\n";
  277.     cout<<"\t\t\t\t***************************************************************\n";
  278.     cout<<"\t\t\t\t             !!!Enter Your Decision!!!";
  279. }
  280.  
  281. int hangman:: hangmangame(string wordToguess1,string HINT)
  282. {
  283.     string guesses;
  284.  
  285.  
  286.     char b;
  287.     bool win=false;
  288.     int tries=0;
  289.  
  290.     do
  291.     {
  292.         system("cls");
  293.         printmsg("HANG MAN");
  294.         DrawHangman(tries);
  295.         PrintAvaLet(guesses);
  296.         cout<<"\n";
  297.         printmsg("!!!HINT!!!");
  298.         printmsg(HINT,false);
  299.         cout<<"\n";
  300.         printmsg("Guess The Word!!!");
  301.         win=PrintCheck(wordToguess1,guesses);
  302.  
  303.         if (win)
  304.         {
  305.             break;
  306.         }
  307.         char x;
  308.  
  309.         x=_getch();
  310.         x=toupper(x);
  311.         if (guesses.find(x) == string::npos)
  312.             guesses += x;
  313.         tries = triesleft(wordToguess1,guesses);
  314.     }
  315.     while(tries<7);
  316.  
  317.         if(win)
  318.         {
  319.             score1++;
  320.             cout<<"\n";
  321.             printmsg("!!!YOU HAVE WON THE GAME!!!");
  322.             printmsg("PRESS ANY KEY TO CONTINUE",false);
  323.             printmsg("PRESS B TO GO BACK",false);
  324.             printmsg("PRESS X TO EXIT THE GAME",false);
  325.             b=_getch();
  326.          if ((b=='x')||(b=='X'))
  327.          {
  328.              scores();
  329.              exit(0);
  330.          }
  331.          else if ((b=='b')||(b=='B'))
  332.          {
  333.              score2=score1;
  334.             scores();
  335.             menu();
  336.          }
  337.          else
  338.          {
  339.             guess();
  340.          }
  341.     }
  342.             else
  343.         {
  344.             scores();
  345.             cout<<"\n";
  346.             printmsg("!!!GAME-OVER!!!");
  347.             printmsg("THE CORRECT WORD IS",false);
  348.             printmsg(wordToguess1,false);
  349.             _getch();
  350.             system("cls");
  351.             cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n";
  352.             printmsg("",true,false);
  353.             printmsg("PRESS X TO EXIT THE GAME",false,false);
  354.             printmsg("",false,true);
  355.             printmsg("",false,false);
  356.             printmsg("PRESS ANY KEY TO GO BACK",false,false);
  357.             printmsg("",false,true);
  358.             b=_getch();
  359.             if(b=='x'||b=='X')
  360.                 {
  361.                     exit(0);
  362.                 }
  363.             else
  364.             {
  365.                 menu();
  366.             }
  367.         }
  368. }
  369.  
  370. void hangman::board()
  371. {
  372.     top:
  373.     system("cls");
  374.     printmsg("HANGMAN",true,true);
  375.     printmsg("1. Sign up",false);
  376.     printmsg("2. login",false);
  377.     printmsg("3.Exit ",false);
  378.     switch(_getch())
  379.     {
  380.         case '1':signup();
  381.         break;
  382.  
  383.         case '2':login();
  384.         break;
  385.  
  386.         case '3':exit(0);
  387.  
  388.         default:cout<<"Invalid key"<<endl;
  389.         _getch();
  390.         goto top;
  391.  
  392. }
  393. }
  394. void hangman::signup()
  395. {
  396.     int o;
  397.     struct hangmans p;
  398.         struct hangmans q;
  399.   system("cls");
  400.     printmsg("HANGMAN");
  401.     FILE *f;
  402.        FILE *fb;
  403.  
  404.      f=fopen("Players.txt","ab");
  405.      f=fopen("Players.txt","rb");
  406.       fb=fopen("Players.txt","ab");
  407.      printmsg("Enter name:",false);
  408.      printf("\n\t\t\t\t");
  409.     scanf("%s",&names);
  410.     fflush(stdin);
  411.      fwrite(&p, sizeof(p), 1, f);
  412.     while (fread(&p,sizeof(p),1,f)==1)
  413.     {
  414.         if (strcmp(names,p.name)==0)
  415.         {
  416.             fclose(f);
  417.             o=1;
  418.         }
  419.     }
  420.     if (o!=1)
  421.     {
  422.        strcpy(q.name,names);
  423.         fwrite(&q, sizeof(q), 1, fb);
  424.         printf("\t\t\tThe user is available\n");
  425.  
  426.         fclose(fb);
  427.         _getch();
  428.         board();
  429.  
  430.  
  431.     }
  432.     else if (o==1){
  433.         printf("\t\t\tThe user already exists!!\n");
  434.         _getch();
  435.      board();
  436.     }
  437.  
  438. }
  439.  
  440. void hangman::login()
  441. {
  442.     system("cls");
  443.  
  444.     struct hangmans q;
  445.     FILE *fb;
  446.     fb=fopen("Players.txt","rb");
  447.  
  448.        printmsg("HANGMAN");
  449.        printmsg("Enter name: ",false);
  450.        scanf("%s",&names);
  451.        fflush(stdin);
  452.        int o;
  453.        while(fread(&q,sizeof(q),1,fb)==1)
  454.        {
  455.            if (strcmp(names,q.name)==0)
  456.            {
  457.              o=1;
  458.            }
  459.        }
  460.        if (o!=1)
  461.     {
  462.         cout<<"The user doesn't exists"<<endl;
  463.         fclose(fb);
  464.         _getch();
  465.         board();
  466.  
  467.  
  468.  
  469.     }
  470.     else if (o==1)
  471.     {
  472.  
  473.         cout<<"The user exists"<<endl;
  474.         _getch();
  475.         create();
  476.         menu();
  477.     }
  478. }
  479. void hangman::create()
  480. {
  481.  
  482.  
  483.      time_t rawtime;
  484.   struct tm * timeinfo;
  485.  
  486.   time ( &rawtime );
  487.   timeinfo = localtime ( &rawtime );
  488.   strcpy(time1,asctime(timeinfo));
  489.  
  490.  
  491. }
  492.  
  493. void hangman::scores()
  494. {
  495.     struct hangmans r;
  496.     FILE *fb;
  497.     fb=fopen("Playerss.txt","ab");
  498.     strcpy(r.name,names);
  499.     r.score=score2;
  500.     strcpy(r.times,time1);
  501.     fwrite(&r, sizeof(r), 1, fb);
  502.     fclose(fb);
  503. }
  504.  
  505.  
  506. void hangman:: guess()
  507. {create();
  508.     srand(time(0));
  509.     string wordToguess;
  510.     wordToguess = LoadRW("ans.txt");
  511.     string HINT;
  512.     HINT = Loadhint("ques.txt");
  513.     hangmangame(wordToguess,HINT);
  514.  
  515. }
  516.  
  517.  
  518. void hangman::scoreboard()
  519. {
  520.     char a;
  521.     system("cls");
  522.     struct hangmans r;
  523.     FILE *f;
  524.     f=fopen("Playerss.txt","rb");
  525.     printmsg("SCOREBOARD",true,true);
  526.     if (f==NULL)
  527.         {
  528.             printf("\nERROR OPENING FILE, THE RECORD FILE DOES NOT EXIST!!!");
  529.             getch();
  530.             menu();
  531.         }
  532.     while(fread(&r,sizeof(r),1,f)==1)
  533.         {
  534.             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);
  535.         }
  536.     fclose(f);
  537.     printmsg("PRESS ANY KEY TO GO BACK",true,false);
  538.         printmsg("PRESS X TO EXIT",true,true);
  539.         a=_getch();
  540.         if ((a=='x')||(a=='X'))
  541.             {
  542.                 exit(0);
  543.             }
  544.         else
  545.             {
  546.                 menu();
  547.             }
  548. }
  549. /////////////////////////////////////////////////////////////////////////////
  550. /////////////////////////////////////////////////////////////////////////////
  551. //DRIVER CODE................................................................
  552. int main()
  553. {
  554.     hangman p;
  555.     char a;
  556.  
  557.     p.homepage();
  558.     a=_getch();
  559.     if(a=='x'||a=='X')
  560.     {
  561.         exit(0);
  562.     }
  563.     else
  564.     {
  565.         p.board();
  566.     }
  567.     return 0;
  568. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement