Bit better
This commit is contained in:
		@@ -146,21 +146,20 @@ int main(int argc, char *argv[]) {
 | 
				
			|||||||
    bool showRemainingWords;
 | 
					    bool showRemainingWords;
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        std::string aux = input("Should I tell you my thoughts? (y/N): ");
 | 
					        std::string aux = input("Should I tell you my thoughts? (y/N): ");
 | 
				
			||||||
        // To lowercase
 | 
					 | 
				
			||||||
        std::transform(aux.begin(), aux.end(), aux.begin(), ::tolower);
 | 
					        std::transform(aux.begin(), aux.end(), aux.begin(), ::tolower);
 | 
				
			||||||
        showRemainingWords = aux == "y" || aux == "yes";
 | 
					        showRemainingWords = aux == "y" || aux == "yes";
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    std::vector<std::string> dictionary = readDictionary(wordLength);
 | 
					    std::vector<std::string> dictionary = readDictionary(wordLength);
 | 
				
			||||||
    std::vector<char> guesses(wordLength, '-');
 | 
					    std::vector<char> guessProgress(wordLength, '-');
 | 
				
			||||||
    std::cout << "So far: ";
 | 
					    std::cout << "So far: ";
 | 
				
			||||||
    printContainer(guesses);
 | 
					    printContainer(guessProgress);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // There won't be many chars here, so we benefit from vector being
 | 
					 | 
				
			||||||
    // continous memory
 | 
					 | 
				
			||||||
    std::vector<char> guessedChars;
 | 
					    std::vector<char> guessedChars;
 | 
				
			||||||
    size_t wrongAttempts = 0;
 | 
					    size_t wrongAttempts = 0;
 | 
				
			||||||
    bool hasWon = false, hasLost = false;
 | 
					    bool hasWon = false, hasLost = false;
 | 
				
			||||||
 | 
					    std::string currentlyThinkingOf(wordLength, '-');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    do {
 | 
					    do {
 | 
				
			||||||
        char guess;
 | 
					        char guess;
 | 
				
			||||||
        while (true) {
 | 
					        while (true) {
 | 
				
			||||||
@@ -175,39 +174,55 @@ int main(int argc, char *argv[]) {
 | 
				
			|||||||
                std::cout << "You've already guessed '" << guess << "'!" << std::endl;
 | 
					                std::cout << "You've already guessed '" << guess << "'!" << std::endl;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        std::unordered_map<std::string, std::vector<std::string>> families =
 | 
					        // Only update the word families if we're not in the endgame
 | 
				
			||||||
            getWordFamilies(dictionary, guessedChars, guess);
 | 
					        if (dictionary.size() > 1) {
 | 
				
			||||||
 | 
					            std::unordered_map<std::string, std::vector<std::string>> families =
 | 
				
			||||||
 | 
					                getWordFamilies(dictionary, guessedChars, guess);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        auto biggestEntry = std::max_element(
 | 
					            using Entry = std::pair<std::string, std::vector<std::string>>;
 | 
				
			||||||
            families.begin(), families.end(),
 | 
					            auto biggestEntry = std::max_element(
 | 
				
			||||||
            [](const std::pair<std::string, std::vector<std::string>> &a,
 | 
					                families.begin(), families.end(), [](const Entry &a, const Entry &b) {
 | 
				
			||||||
                                                 const std::pair<std::string, std::vector<std::string>> &b) {
 | 
					                    return a.second.size() < b.second.size();
 | 
				
			||||||
                return a.second.size() < b.second.size();
 | 
					                });
 | 
				
			||||||
            });
 | 
					            currentlyThinkingOf = biggestEntry->first;
 | 
				
			||||||
        std::string familyKey = std::move(biggestEntry->first);
 | 
					            dictionary = biggestEntry->second;
 | 
				
			||||||
        dictionary = std::move(biggestEntry->second);
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (showRemainingWords) {
 | 
					        if (showRemainingWords) {
 | 
				
			||||||
            std::cout << "Possible words: ";
 | 
					            std::cout << "Possible words: ";
 | 
				
			||||||
            printContainer(dictionary, ", ");
 | 
					            printContainer(dictionary, ", ");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        bool wasCorrect = familyKey.find(guess) != std::string::npos;
 | 
					        bool wasCorrect = false;
 | 
				
			||||||
        if (wasCorrect)
 | 
					        for (size_t i = 0; i < currentlyThinkingOf.size(); ++i) {
 | 
				
			||||||
 | 
					            if (currentlyThinkingOf[i] == guess) {
 | 
				
			||||||
 | 
					                guessProgress[i] = guess;
 | 
				
			||||||
 | 
					                wasCorrect = true;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (wasCorrect) {
 | 
				
			||||||
            std::cout << "Correct!" << std::endl;
 | 
					            std::cout << "Correct!" << std::endl;
 | 
				
			||||||
        else {
 | 
					        } else {
 | 
				
			||||||
            std::cout << "Wrong!" << std::endl;
 | 
					            std::cout << "Wrong!" << std::endl;
 | 
				
			||||||
            wrongAttempts++;
 | 
					            wrongAttempts++;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        std::cout << "So far: " << familyKey << std::endl;
 | 
					
 | 
				
			||||||
 | 
					        std::cout << "So far: ";
 | 
				
			||||||
 | 
					        printContainer(guessProgress); // Display the current guessed word state
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        guessedChars.push_back(guess);
 | 
					        guessedChars.push_back(guess);
 | 
				
			||||||
        hasWon = dictionary.size() == 1;
 | 
					
 | 
				
			||||||
 | 
					        // Check if all the characters in the final word have been guessed
 | 
				
			||||||
 | 
					        hasWon = std::all_of(guessProgress.begin(), guessProgress.end(),
 | 
				
			||||||
 | 
					                             [](char c) { return c != '-'; });
 | 
				
			||||||
        hasLost = wrongAttempts == maxAttempts;
 | 
					        hasLost = wrongAttempts == maxAttempts;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    } while (!hasWon && !hasLost);
 | 
					    } while (!hasWon && !hasLost);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const char *out = hasWon ? "congartulations! You've won! 🎉" : "you lost.";
 | 
					    const char *out = hasWon ? "congratulations! You've won! 🎉" : "you lost.";
 | 
				
			||||||
    std::cout << "The word was '" << dictionary[0] << "', " << out << std::endl;
 | 
					    std::cout << "Well, the word was '" << dictionary[0] << "', " << out
 | 
				
			||||||
 | 
					              << std::endl;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user