Advertisement
DiaxPlayer

Untitled

Feb 20th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <random>
  4. #include <vector>
  5. #include <windows.h>
  6.  
  7. using std::string;
  8. using std::cout;
  9. using std::endl;
  10. using std::cin;
  11.  
  12. int main()
  13. {
  14.     constexpr auto max = 10u;
  15.     const std::vector<string> words = {"agrest", "bigos", "czara", "drewno", "ekipa", "figura", "gorycz", "hiena", "iskra", "junior"};
  16.     std::mt19937 eng(std::random_device{}());
  17.     std::uniform_int_distribution<> dis(0u, max-1);
  18.     char play;
  19.     cout << "Witaj w grze wisielec!" << endl;
  20.     do
  21.     {
  22.         string target = words[dis(eng)];
  23.         int len = target.length();
  24.         string attempt(len,'-');
  25.         string badchars;
  26.         int guesses = 6;
  27.         cout << "Slowo ma " << len << " liter." << endl;
  28.         cout << "Mozesz pomylic sie " << guesses << " razy." << endl;
  29.         cout << "POWODZENIA :)" << endl;
  30.         cout << endl;
  31.         while (guesses > 0 && attempt != target)
  32.         {
  33.             cout << "Twoje slowo: " << attempt << endl;
  34.             if (badchars.length() > 0)
  35.             {
  36.                 cout << "Niepoprawne litery: " << badchars << endl;
  37.             }
  38.             cout << "Mozesz pomylic sie jeszcze " << guesses << " razy" << endl;
  39.             char letter;
  40.             cout << "Podaj litere: ";
  41.             cin >> letter;
  42.  
  43.             // system dependent
  44.             system("cls");
  45.  
  46.             if (badchars.find(letter) != string::npos ||
  47.                 attempt.find(letter) != string::npos)
  48.             {
  49.                 cout << "Ta litera juz byla. Sproboj innej." << endl;
  50.                 continue;
  51.             }
  52.  
  53.             int pos = target.find(letter);
  54.             if (pos == string::npos)
  55.             {
  56.                 cout << "Nie ma takiej litery!" << endl;
  57.                 guesses--;
  58.                 badchars += letter;
  59.             }
  60.             else
  61.             {
  62.                 cout << "Poprawna litera" << endl;
  63.                 do
  64.                 {
  65.                     attempt[pos] = letter;
  66.                     pos = target.find(letter, pos+1);
  67.                 } while(pos != string::npos);
  68.             }
  69.         }
  70.         if (guesses > 0)
  71.             cout << "To juz cale slowo! Brawo!" << endl;
  72.         else
  73.             cout << "Niestety, to slowo to " << target << endl;
  74.         cout << "Chcesz zagrac jeszcze raz? (t/n)" << endl;
  75.         cin >> play;
  76.     } while (tolower(play)=='t');
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement