From 87d896049dc18f4869b37d12dc876c5240f12462 Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Thu, 5 Sep 2024 21:31:19 +0200 Subject: [PATCH] Working --- src/evilhangman.cpp | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/evilhangman.cpp b/src/evilhangman.cpp index fa9bfd7..b584f1b 100755 --- a/src/evilhangman.cpp +++ b/src/evilhangman.cpp @@ -151,9 +151,8 @@ int main(int argc, char *argv[]) { } std::vector dictionary = readDictionary(wordLength); - std::vector guessProgress(wordLength, '-'); - std::cout << "So far: "; - printContainer(guessProgress); + std::string guessProgress(wordLength, '-'); + std::cout << "Word: " << guessProgress << std::endl; std::vector guessedChars; size_t wrongAttempts = 0; @@ -186,6 +185,14 @@ int main(int argc, char *argv[]) { }); currentlyThinkingOf = biggestEntry->first; dictionary = biggestEntry->second; + }else{ + std::string &word = dictionary[0]; + + for (size_t i = 0; i < word.size(); i++) { + if (word[i] != guess) + continue; + currentlyThinkingOf[i] = guess; + } } if (showRemainingWords) { @@ -208,11 +215,12 @@ int main(int argc, char *argv[]) { wrongAttempts++; } - std::cout << "So far: "; - printContainer(guessProgress); // Display the current guessed word state - guessedChars.push_back(guess); + std::cout << "Used letters: "; + printContainer(guessedChars, " "); + std::cout << "Word: " << currentlyThinkingOf << std::endl; + // Check if all the characters in the final word have been guessed hasWon = std::all_of(guessProgress.begin(), guessProgress.end(), [](char c) { return c != '-'; }); @@ -221,7 +229,7 @@ int main(int argc, char *argv[]) { } while (!hasWon && !hasLost); const char *out = hasWon ? "congratulations! You've won! 🎉" : "you lost."; - std::cout << "Well, the word was '" << dictionary[0] << "', " << out + std::cout << "The word was '" << dictionary[0] << "', " << out << std::endl; return 0;