Bit better
This commit is contained in:
parent
1f87a9257c
commit
703ddd90bb
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 =
|
std::unordered_map<std::string, std::vector<std::string>> families =
|
||||||
getWordFamilies(dictionary, guessedChars, guess);
|
getWordFamilies(dictionary, guessedChars, guess);
|
||||||
|
|
||||||
|
using Entry = std::pair<std::string, std::vector<std::string>>;
|
||||||
auto biggestEntry = std::max_element(
|
auto biggestEntry = std::max_element(
|
||||||
families.begin(), families.end(),
|
families.begin(), families.end(), [](const Entry &a, const Entry &b) {
|
||||||
[](const std::pair<std::string, std::vector<std::string>> &a,
|
|
||||||
const std::pair<std::string, std::vector<std::string>> &b) {
|
|
||||||
return a.second.size() < b.second.size();
|
return a.second.size() < b.second.size();
|
||||||
});
|
});
|
||||||
std::string familyKey = std::move(biggestEntry->first);
|
currentlyThinkingOf = biggestEntry->first;
|
||||||
dictionary = std::move(biggestEntry->second);
|
dictionary = 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;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user