#include "WordManager.h"
#include <fstream>
#include <iostream>
#include <algorithm>
#include <random>

WordManager::WordManager() {
    std::ifstream file("words.txt");
    if (!file.is_open()) {
        std::cerr << "Ошибка открытия файла words.txt\n";
        exit(EXIT_FAILURE);
    }
    std::vector<std::string> words;
    std::string word;
    while (std::getline(file, word)) {
        words.push_back(word);
    }
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(0, words.size() - 1);
    secretWord = words[dis(gen)];
    displayedWord = std::string(secretWord.length(), '_');
}

bool WordManager::checkLetter(char letter) {
    bool found = false;
    char lowerLetter = tolower(letter);
    for (size_t i = 0; i < secretWord.length(); ++i) {
        if (tolower(secretWord[i]) == lowerLetter) {
            displayedWord[i] = secretWord[i];
            found = true;
        }
    }
    if (found) guessedLetters.insert(lowerLetter);
    return found;
}

std::string WordManager::getDisplayedWord() const {
    return displayedWord;
}

bool WordManager::isWordGuessed() const {
    return displayedWord.find('_') == std::string::npos;
}
