Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <array>
  4. #include <random>
  5.  
  6. #define MAX_TRIES       6
  7.  
  8. #define EXIT_SUCCESS    0
  9. #define EXIT_ERROR      84
  10.  
  11. #define WORDS           5
  12.  
  13. #define GALLOW_0        "_________\n|  |\n|  \n|\n|\n|\n|\n"
  14. #define GALLOW_1        "_________\n|  |\n|  0\n|\n|\n|\n|\n"
  15. #define GALLOW_2        "_________\n|  |\n|  0\n|  |\n|\n|\n|\n"
  16. #define GALLOW_3        "_________\n|  |\n|  0\n| /|\n|\n|\n|\n"
  17. #define GALLOW_4        "_________\n|  |\n|  0\n| /|\\\n|\n|\n|\n"
  18. #define GALLOW_5        "_________\n|  |\n|  0\n| /|\\\n| /\n|\n|\n"
  19. #define GALLOW_6        "_________\n|  |\n|  0\n| /|\\\n| / \\\n|\n|\n"
  20.  
  21. using namespace     std;
  22.  
  23. std::string                                                     get_random_word()
  24. {
  25.     std::mt19937                                                rng;
  26.     std::uniform_int_distribution<std::mt19937::result_type>    random_index(0, WORDS - 1);
  27.     std::array<const char *, WORDS>                             words = {
  28.         "HELLO",
  29.         "WASSUP",
  30.         "SWEG",
  31.         "IDKKEV",
  32.         "FORSENBAJS"
  33.     };
  34.  
  35.     rng.seed(std::random_device()());
  36.     return (words.at(random_index(rng)));
  37. }
  38.  
  39. void                print_gallow(int status)
  40. {
  41.     switch (status)
  42.     {
  43.         case 0:
  44.             cout << GALLOW_0;
  45.             break;
  46.         case 1:
  47.             cout << GALLOW_1;
  48.             break;
  49.         case 2:
  50.             cout << GALLOW_2;
  51.             break;
  52.         case 3:
  53.             cout << GALLOW_3;
  54.             break;
  55.         case 4:
  56.             cout << GALLOW_4;
  57.             break;
  58.         case 5:
  59.             cout << GALLOW_5;
  60.             break;
  61.         default:
  62.             cout << GALLOW_6;
  63.             break;
  64.     }
  65.     cout << '\n';
  66. }
  67.  
  68. int                 main()
  69. {
  70.     std::string     word;
  71.     std::string     guessed;
  72.     std::string     buffer;
  73.     char            last;
  74.     int             word_len;
  75.     int             tries;
  76.     int             i;
  77.     char            c;
  78.     bool            is_char;
  79.     bool            won;
  80.  
  81.     tries = 0;
  82.     word = get_random_word();
  83.     won = false;
  84.     word_len = word.size();
  85.     while (tries < MAX_TRIES && !won)
  86.     {
  87.         won = true;
  88.         is_char = false;
  89.         while (!is_char)
  90.         {
  91.             cout << "Enter a letter: ";
  92.             cin >> buffer;
  93.             if (buffer.size() == 1)
  94.             {
  95.                 last = buffer[0] &= '_';
  96.                 is_char = true;
  97.             }
  98.         }
  99.         if (word.find_first_of(last) == string::npos)
  100.         {
  101.             tries++;
  102.             if (tries < MAX_TRIES)
  103.                 cout << "Oops! You missed!\nRemaining tries: " << MAX_TRIES - tries << "\n";
  104.         }
  105.         else if (guessed.find_first_of(last) == string::npos)
  106.             guessed += last;
  107.         cout << "Word:";
  108.         i = 0;
  109.         while (i < word_len)
  110.         {
  111.             c = word.at(i);
  112.             if (guessed.find_first_of(c) != string::npos)
  113.                 cout << c;
  114.             else
  115.             {
  116.                 won = false;
  117.                 cout << "_";
  118.             }
  119.             i++;
  120.         }
  121.         cout << '\n';
  122.         cout << "Guessed: ";
  123.         if (guessed.size() == 0)
  124.             cout << "None\n";
  125.         else
  126.             cout << guessed << "\n";
  127.         print_gallow(tries);
  128.     }
  129.     if (tries < MAX_TRIES)
  130.         cout << "Congrats you win!\n";
  131.     else
  132.         cout << "Sorry, you lose!\n";
  133.     return (EXIT_SUCCESS);
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement