Advertisement
plarmi

cppexam_1_4

Jul 1st, 2023
937
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <ctime>
  4. #include <cstdlib>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <windows.h>
  8.  
  9. using namespace std;
  10.  
  11. vector<string> decryptWordList(const string& fileName) {
  12.     vector<string> wordList;
  13.     ifstream file(fileName);
  14.  
  15.     if (file.is_open()) {
  16.         string encryptedWord;
  17.         while (getline(file, encryptedWord)) {
  18.             string decryptedWord;
  19.             for (char c : encryptedWord) {
  20.                 decryptedWord += c - 1; // Расшифровка символа
  21.             }
  22.             wordList.push_back(decryptedWord);
  23.         }
  24.  
  25.         file.close();
  26.     } else {
  27.         cout << "Не удалось открыть файл: " << fileName << endl;
  28.     }
  29.  
  30.     return wordList;
  31. }
  32.  
  33. string getRandomWord(const vector<string>& wordList) {
  34.     srand(static_cast<unsigned int>(time(nullptr)));
  35.     int randomIndex = rand() % wordList.size();
  36.     return wordList[randomIndex];
  37. }
  38.  
  39. bool containsLetter(const string& word, char letter) {
  40.     return find(word.begin(), word.end(), letter) != word.end();
  41. }
  42.  
  43. void updateGameState(const string& word, string& guessedLetters, string& usedLetters, int& attemptsLeft, char letter) {
  44.     if (containsLetter(word, letter)) {
  45.         if (!containsLetter(guessedLetters, letter)) {
  46.             guessedLetters += letter;
  47.         } else {
  48.             cout << "Вы уже угадали букву '" << letter << "'." << endl;
  49.         }
  50.     } else {
  51.         if (!containsLetter(usedLetters, letter)) {
  52.             usedLetters += letter;
  53.             attemptsLeft--;
  54.         } else {
  55.             cout << "Вы уже использовали эту букву '" << letter << "'." << endl;
  56.         }
  57.     }
  58.  
  59.     cout << "Угаданные буквы: " << guessedLetters << endl;
  60.     cout << "Использованные буквы: " << usedLetters << endl;
  61.     cout << "Оставшиеся попытки: " << attemptsLeft << endl;
  62. }
  63.  
  64. bool isGameOver(const string& word, const string& guessedLetters, int attemptsLeft) {
  65.     bool isWordGuessed = all_of(word.begin(), word.end(), [&guessedLetters](char c) {
  66.         return containsLetter(guessedLetters, c);
  67.     });
  68.  
  69.     return isWordGuessed || attemptsLeft <= 0;
  70. }
  71.  
  72. void printGameStatistics(const string& word, const string& guessedLetters, int attemptsLeft) {
  73.     cout << "Конец игры!" << endl;
  74.     cout << "Слово: " << word << endl;
  75.     cout << "Угаданные буквы: " << guessedLetters << endl;
  76.     cout << "Оставшиеся попытки: " << attemptsLeft << endl;
  77. }
  78.  
  79. void displayHangman(int attemptsLeft) {
  80.     cout << endl;
  81.  
  82.     switch (attemptsLeft) {
  83.         case 7:
  84.             cout << "   _____" << endl;
  85.             cout << "  |     |" << endl;
  86.             cout << "        |" << endl;
  87.             cout << "        |" << endl;
  88.             cout << "        |" << endl;
  89.             cout << "        |" << endl;
  90.             cout << "_______|_____" << endl;
  91.             break;
  92.         case 6:
  93.             cout << "   _____" << endl;
  94.             cout << "  |     |" << endl;
  95.             cout << "  O     |" << endl;
  96.             cout << "        |" << endl;
  97.             cout << "        |" << endl;
  98.             cout << "        |" << endl;
  99.             cout << "_______|_____" << endl;
  100.             break;
  101.         case 5:
  102.             cout << "   _____" << endl;
  103.             cout << "  |     |" << endl;
  104.             cout << "  O     |" << endl;
  105.             cout << "  |     |" << endl;
  106.             cout << "        |" << endl;
  107.             cout << "        |" << endl;
  108.             cout << "_______|_____" << endl;
  109.             break;
  110.         case 4:
  111.             cout << "   _____" << endl;
  112.             cout << "  |     |" << endl;
  113.             cout << "  O     |" << endl;
  114.             cout << " /|     |" << endl;
  115.             cout << "        |" << endl;
  116.             cout << "        |" << endl;
  117.             cout << "_______|_____" << endl;
  118.             break;
  119.         case 3:
  120.             cout << "   _____" << endl;
  121.             cout << "  |     |" << endl;
  122.             cout << "  O     |" << endl;
  123.             cout << " /|\\    |" << endl;
  124.             cout << "        |" << endl;
  125.             cout << "        |" << endl;
  126.             cout << "_______|_____" << endl;
  127.             break;
  128.         case 2:
  129.             cout << "   _____" << endl;
  130.             cout << "  |     |" << endl;
  131.             cout << "  O     |" << endl;
  132.             cout << " /|\\    |" << endl;
  133.             cout << " /      |" << endl;
  134.             cout << "        |" << endl;
  135.             cout << "_______|_____" << endl;
  136.             break;
  137.         case 1:
  138.             cout << "   _____" << endl;
  139.             cout << "  |     |" << endl;
  140.             cout << "  O     |" << endl;
  141.             cout << " /|\\    |" << endl;
  142.             cout << " / \\    |" << endl;
  143.             cout << "        |" << endl;
  144.             cout << "_______|_____" << endl;
  145.             break;
  146.         default:
  147.             cout << endl;
  148.             break;
  149.     }
  150.  
  151.     cout << endl;
  152. }
  153.  
  154. int main() {
  155.     SetConsoleCP(1251);
  156.     SetConsoleOutputCP(1251);
  157.  
  158.     // НУЖНО СОЗДАТЬ ФАЙЛ С ТАКИМ ЖЕ НАЗВАНИЕМ И ДОБАВИТЬ К НЕМУ ПУТЬ НИЖЕ!!!!
  159.     string wordListFile = "wordlist.txt";
  160.     vector<string> wordList = decryptWordList(wordListFile);
  161.  
  162.     string word = getRandomWord(wordList);
  163.     string guessedLetters;
  164.     string usedLetters;
  165.     int attemptsLeft = 7;
  166.  
  167.     cout << "Добро пожаловать в игру «Виселица»!" << endl;
  168.     cout << "Оставшиеся попытки: " << attemptsLeft << endl;
  169.  
  170.     while (!isGameOver(word, guessedLetters, attemptsLeft)) {
  171.         char letter;
  172.         cout << "Введите букву: ";
  173.         cin >> letter;
  174.  
  175.         updateGameState(word, guessedLetters, usedLetters, attemptsLeft, letter);
  176.  
  177.         displayHangman(attemptsLeft);
  178.  
  179.         cout << endl;
  180.     }
  181.  
  182.     printGameStatistics(word, guessedLetters, attemptsLeft);
  183.  
  184.     return 0;
  185. }
  186.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement