Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <ctime>
- #include <cstdlib>
- #include <vector>
- #include <algorithm>
- #include <windows.h>
- using namespace std;
- vector<string> decryptWordList(const string& fileName) {
- vector<string> wordList;
- ifstream file(fileName);
- if (file.is_open()) {
- string encryptedWord;
- while (getline(file, encryptedWord)) {
- string decryptedWord;
- for (char c : encryptedWord) {
- decryptedWord += c - 1; // Расшифровка символа
- }
- wordList.push_back(decryptedWord);
- }
- file.close();
- } else {
- cout << "Не удалось открыть файл: " << fileName << endl;
- }
- return wordList;
- }
- string getRandomWord(const vector<string>& wordList) {
- srand(static_cast<unsigned int>(time(nullptr)));
- int randomIndex = rand() % wordList.size();
- return wordList[randomIndex];
- }
- bool containsLetter(const string& word, char letter) {
- return find(word.begin(), word.end(), letter) != word.end();
- }
- void updateGameState(const string& word, string& guessedLetters, string& usedLetters, int& attemptsLeft, char letter) {
- if (containsLetter(word, letter)) {
- if (!containsLetter(guessedLetters, letter)) {
- guessedLetters += letter;
- } else {
- cout << "Вы уже угадали букву '" << letter << "'." << endl;
- }
- } else {
- if (!containsLetter(usedLetters, letter)) {
- usedLetters += letter;
- attemptsLeft--;
- } else {
- cout << "Вы уже использовали эту букву '" << letter << "'." << endl;
- }
- }
- cout << "Угаданные буквы: " << guessedLetters << endl;
- cout << "Использованные буквы: " << usedLetters << endl;
- cout << "Оставшиеся попытки: " << attemptsLeft << endl;
- }
- bool isGameOver(const string& word, const string& guessedLetters, int attemptsLeft) {
- bool isWordGuessed = all_of(word.begin(), word.end(), [&guessedLetters](char c) {
- return containsLetter(guessedLetters, c);
- });
- return isWordGuessed || attemptsLeft <= 0;
- }
- void printGameStatistics(const string& word, const string& guessedLetters, int attemptsLeft) {
- cout << "Конец игры!" << endl;
- cout << "Слово: " << word << endl;
- cout << "Угаданные буквы: " << guessedLetters << endl;
- cout << "Оставшиеся попытки: " << attemptsLeft << endl;
- }
- void displayHangman(int attemptsLeft) {
- cout << endl;
- switch (attemptsLeft) {
- case 7:
- cout << " _____" << endl;
- cout << " | |" << endl;
- cout << " |" << endl;
- cout << " |" << endl;
- cout << " |" << endl;
- cout << " |" << endl;
- cout << "_______|_____" << endl;
- break;
- case 6:
- cout << " _____" << endl;
- cout << " | |" << endl;
- cout << " O |" << endl;
- cout << " |" << endl;
- cout << " |" << endl;
- cout << " |" << endl;
- cout << "_______|_____" << endl;
- break;
- case 5:
- cout << " _____" << endl;
- cout << " | |" << endl;
- cout << " O |" << endl;
- cout << " | |" << endl;
- cout << " |" << endl;
- cout << " |" << endl;
- cout << "_______|_____" << endl;
- break;
- case 4:
- cout << " _____" << endl;
- cout << " | |" << endl;
- cout << " O |" << endl;
- cout << " /| |" << endl;
- cout << " |" << endl;
- cout << " |" << endl;
- cout << "_______|_____" << endl;
- break;
- case 3:
- cout << " _____" << endl;
- cout << " | |" << endl;
- cout << " O |" << endl;
- cout << " /|\\ |" << endl;
- cout << " |" << endl;
- cout << " |" << endl;
- cout << "_______|_____" << endl;
- break;
- case 2:
- cout << " _____" << endl;
- cout << " | |" << endl;
- cout << " O |" << endl;
- cout << " /|\\ |" << endl;
- cout << " / |" << endl;
- cout << " |" << endl;
- cout << "_______|_____" << endl;
- break;
- case 1:
- cout << " _____" << endl;
- cout << " | |" << endl;
- cout << " O |" << endl;
- cout << " /|\\ |" << endl;
- cout << " / \\ |" << endl;
- cout << " |" << endl;
- cout << "_______|_____" << endl;
- break;
- default:
- cout << endl;
- break;
- }
- cout << endl;
- }
- int main() {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- // НУЖНО СОЗДАТЬ ФАЙЛ С ТАКИМ ЖЕ НАЗВАНИЕМ И ДОБАВИТЬ К НЕМУ ПУТЬ НИЖЕ!!!!
- string wordListFile = "wordlist.txt";
- vector<string> wordList = decryptWordList(wordListFile);
- string word = getRandomWord(wordList);
- string guessedLetters;
- string usedLetters;
- int attemptsLeft = 7;
- cout << "Добро пожаловать в игру «Виселица»!" << endl;
- cout << "Оставшиеся попытки: " << attemptsLeft << endl;
- while (!isGameOver(word, guessedLetters, attemptsLeft)) {
- char letter;
- cout << "Введите букву: ";
- cin >> letter;
- updateGameState(word, guessedLetters, usedLetters, attemptsLeft, letter);
- displayHangman(attemptsLeft);
- cout << endl;
- }
- printGameStatistics(word, guessedLetters, attemptsLeft);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement