Bit better

This commit is contained in:
Love 2024-09-05 15:55:20 +02:00
parent 16551b327f
commit e2727d343d

View File

@ -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<std::string> dictionary = readDictionary(wordLength);
std::vector<char> guesses(wordLength, '-');
std::vector<char> 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<char> 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;
}
// Only update the word families if we're not in the endgame
if (dictionary.size() > 1) {
std::unordered_map<std::string, std::vector<std::string>> families =
getWordFamilies(dictionary, guessedChars, guess);
using Entry = std::pair<std::string, std::vector<std::string>>;
auto biggestEntry = std::max_element(
families.begin(), families.end(),
[](const std::pair<std::string, std::vector<std::string>> &a,
const std::pair<std::string, std::vector<std::string>> &b) {
families.begin(), families.end(), [](const Entry &a, const Entry &b) {
return a.second.size() < b.second.size();
});
std::string familyKey = std::move(biggestEntry->first);
dictionary = std::move(biggestEntry->second);
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;
}