From e2727d343d3d8efc001274193beaf67df93d3e9d Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Thu, 5 Sep 2024 15:55:20 +0200 Subject: [PATCH] Bit better --- src/evilhangman.cpp | 59 ++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/src/evilhangman.cpp b/src/evilhangman.cpp index fc3692a..fa9bfd7 100755 --- a/src/evilhangman.cpp +++ b/src/evilhangman.cpp @@ -146,21 +146,20 @@ int main(int argc, char *argv[]) { bool showRemainingWords; { std::string aux = input("Should I tell you my thoughts? (y/N): "); - // To lowercase std::transform(aux.begin(), aux.end(), aux.begin(), ::tolower); showRemainingWords = aux == "y" || aux == "yes"; } std::vector dictionary = readDictionary(wordLength); - std::vector guesses(wordLength, '-'); + std::vector guessProgress(wordLength, '-'); 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 guessedChars; size_t wrongAttempts = 0; bool hasWon = false, hasLost = false; + std::string currentlyThinkingOf(wordLength, '-'); + do { char guess; while (true) { @@ -175,39 +174,55 @@ int main(int argc, char *argv[]) { std::cout << "You've already guessed '" << guess << "'!" << std::endl; } - std::unordered_map> families = - getWordFamilies(dictionary, guessedChars, guess); + // Only update the word families if we're not in the endgame + if (dictionary.size() > 1) { + std::unordered_map> families = + getWordFamilies(dictionary, guessedChars, guess); - auto biggestEntry = std::max_element( - families.begin(), families.end(), - [](const std::pair> &a, - const std::pair> &b) { - return a.second.size() < b.second.size(); - }); - std::string familyKey = std::move(biggestEntry->first); - dictionary = std::move(biggestEntry->second); + using Entry = std::pair>; + auto biggestEntry = std::max_element( + families.begin(), families.end(), [](const Entry &a, const Entry &b) { + return a.second.size() < b.second.size(); + }); + currentlyThinkingOf = biggestEntry->first; + dictionary = biggestEntry->second; + } if (showRemainingWords) { std::cout << "Possible words: "; printContainer(dictionary, ", "); } - bool wasCorrect = familyKey.find(guess) != std::string::npos; - if (wasCorrect) + bool wasCorrect = false; + 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; - else { + } else { std::cout << "Wrong!" << std::endl; wrongAttempts++; } - std::cout << "So far: " << familyKey << std::endl; + + std::cout << "So far: "; + printContainer(guessProgress); // Display the current guessed word state 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; + } while (!hasWon && !hasLost); - const char *out = hasWon ? "congartulations! You've won! 🎉" : "you lost."; - std::cout << "The word was '" << dictionary[0] << "', " << out << std::endl; + const char *out = hasWon ? "congratulations! You've won! 🎉" : "you lost."; + std::cout << "Well, the word was '" << dictionary[0] << "', " << out + << std::endl; return 0; }