Advertisement
Sheyshya

beta 1 simple hangman concept(refrence)

Aug 10th, 2019
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.93 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<fstream>
  4. #include<time.h>
  5. #include <cstdlib>
  6. #include<cstring>
  7.  
  8. using namespace std;
  9. void printmsg(string message, bool printTop = true, bool printBottom = true)
  10. {
  11.    if(printTop)
  12.     {
  13.         cout<<"+---------------------------------+"<<endl;
  14.         cout<<"|";
  15.     }
  16.     else
  17.     {
  18.         cout<<"|";
  19.     }
  20.     bool front = true;
  21.     for (int i = message.length();i<33;i++)
  22.     {
  23.         if(front)
  24.             {
  25.                 message= " "+ message;
  26.             }
  27.         else
  28.             {
  29.                 message=message +" ";
  30.             }
  31.         front = !front;
  32.     }
  33.     cout<<message.c_str();
  34.     if(printBottom)
  35.         {
  36.             cout<<"|"<<endl;
  37.             cout<<"+---------------------------------+"<<endl;
  38.         }
  39.     else
  40.     {
  41.         cout<<"|"<<endl;
  42.     }
  43. }
  44. void DrawHangman(int guessCount=0)
  45. {
  46.     printmsg("|",false,false);
  47.     printmsg("|",false,false);
  48.  
  49.     if(guessCount>=1)
  50.     {
  51.         printmsg("O",false,false);
  52.     }
  53.     else
  54.     {
  55.         printmsg("",false,false);
  56.     }
  57.     if(guessCount==2)
  58.     {
  59.         printmsg("|",false,false);
  60.     }
  61.     if(guessCount==3)
  62.     {
  63.         printmsg("/| ",false,false);
  64.     }
  65.     if(guessCount>=4)
  66.     {
  67.         printmsg("/|\\",false,false);
  68.     }
  69.     else
  70.     {
  71.         printmsg("",false,false);
  72.     }
  73.     if(guessCount==5)
  74.     {
  75.         printmsg("/  ",false,false);
  76.     }
  77.     if(guessCount==6)
  78.     {
  79.         printmsg("/ \\",false,false);
  80.     }
  81.     else
  82.     {
  83.         printmsg("",false,false);
  84.     }
  85. }
  86. void PrintLetters(string input, char from, char to)
  87. {
  88.     string s;
  89.     for(char i= from;i<=to;i++)
  90.     {
  91.         if (input.find(i) == string::npos)
  92.         {
  93.             s += i;
  94.             s += " ";
  95.         }
  96.         else
  97.         {
  98.             s += "  ";
  99.         }
  100.     }
  101.     printmsg(s,false,false);
  102. }
  103. void PrintAvaLet(string taken)
  104. {
  105.     printmsg("Available Letters",true,true);
  106.     PrintLetters(taken,'A','M');
  107.     PrintLetters(taken,'N','Z');
  108. }
  109. bool PrintCheck(string word, string guessed)
  110. {
  111.     bool won= true;
  112.     string s;
  113.     for(int i=0;i<word.length();i++)
  114.     {
  115.         if(guessed.find(word[i])== string::npos)
  116.         {
  117.             won = false;
  118.             s += "_ ";
  119.         }
  120.         else
  121.         {
  122.             s += word[i];
  123.             s += " ";
  124.         }
  125.     }
  126.     printmsg(s,false);
  127.     return won;
  128. }
  129. string LoadRW(string path)
  130. {
  131.     int lineCount=0;
  132.     string word;
  133.     vector<string> v;
  134. int c=0;
  135.     ifstream reader(path.c_str());
  136.     if(reader.is_open())
  137.     {
  138.  
  139.         while(getline(reader, word))
  140.         v.push_back(word);
  141.         int randomLine=rand()%v.size();
  142.         word = v.at(randomLine);
  143.  
  144.         reader.close();
  145.     }
  146.     return word;
  147. }
  148. int triesleft(string word, string guessed)
  149. {
  150.     int error=0;
  151.     for(int i=0; i < guessed.length();i++)
  152.     {
  153.     if(word.find(guessed[i])==string::npos)
  154.     error++;
  155.     }
  156.     return error;
  157. }
  158.  
  159.  
  160. void homepage()
  161. {
  162.     cout<<"***************************************************************\n";
  163.     cout<<"***************************************************************\n";
  164.     cout<<"********-----------------------------------------------********\n";
  165.     cout<<"******** __    __   ________   ___    __   ___________ ********\n";
  166.     cout<<"********|  |  |  | |   __   | |   \\  |  | |       |********\n";
  167.     cout<<"********|  |__|  | |  |__|  | |  | \\ |  | |   _____   |********\n";
  168.     cout<<"********|   __    | |   __   | |  |\\ \\|  | |  |  |  |  |********\n";
  169.     cout<<"********|  |  |  | |  |  |  | |  | \\ |  | |  |  0  |  |********\n";
  170.     cout<<"********|__|  |__| |__|  |__| |__|  \\___| |  | /|\\ |  |********\n";
  171.     cout<<"********--------------------------------- |  | / \\ |  |********\n";
  172.     cout<<"******** __    __   ________   ___    __  |  |_____|  |********\n";
  173.     cout<<"********|  \\  /  | |   __   | |   \\  |  | |________   |********\n";
  174.     cout<<"********|   \\/   | |  |__|  | |  | \\ |  |          |  |********\n";
  175.     cout<<"********| |\\  /| | |   __   | |  |\\ \\|  |  ________|  |********\n";
  176.     cout<<"********| | \\/ | | |  |  |  | |  | \\ |  | |           |********\n";
  177.     cout<<"********|_|    |_| |__|  |__| |__|  \\___| |___________|********\n";
  178.     cout<<"********-----------------------------------------------********\n";
  179.     cout<<"***************************************************************\n";
  180.     cout<<"*****************------------------------------- **************\n";
  181.     cout<<"****************|PRESS ANY KEY TO ENTER THE GAME|**************\n";
  182.     cout<<"*****************------------------------------- **************\n";
  183.     cout<<"****************|PRESS 'X' KEY TO  EXIT THE GAME|**************\n";
  184.     cout<<"*****************------------------------------- **************\n";
  185.     cout<<"***************************************************************\n";
  186.     cout<<"***************************************************************\n";
  187.     cout<<"         Enter Your Decision :";
  188. }
  189.  
  190. int main()
  191. {
  192.     srand(time(0));
  193.     string guesses;
  194.     string wordToguess;
  195.     wordToguess = LoadRW("dic.txt");
  196.  
  197.  
  198.  
  199.      homepage();
  200.     char a= getchar();
  201.     if(a=='x'||a=='X')
  202.     {
  203.         exit(0);
  204.     }
  205.  
  206. else{
  207.                bool win=false;
  208.     int tries=0;
  209.     do
  210.     {
  211.  
  212.         system("cls");
  213.         printmsg("HANG MAN");
  214.         DrawHangman(tries);
  215.         PrintAvaLet(guesses);
  216.         printmsg("Guess The Word!!!");
  217.  
  218.  
  219.     win=PrintCheck(wordToguess,guesses);
  220.  
  221.  
  222. if (win)
  223. {
  224.     break;
  225. }
  226.  
  227.         char x;
  228.         cout<< ">";
  229.  
  230.       x=_getch();
  231.      x=toupper(x);
  232.  
  233.  
  234.         if (guesses.find(x) == string::npos)
  235.             guesses += x;
  236.  
  237.         tries = triesleft(wordToguess,guesses);
  238.  
  239.     }
  240.  
  241.      while(tries<7);
  242.     if(win){printmsg("!!!YOU HAVE WON THE GAME!!!");
  243.         exit(0);}
  244.  
  245.     else {printmsg("!!!GAME-OVER!!!");}
  246. system("pause");
  247.     getchar();
  248.  
  249. }
  250.  return 0;
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement