#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
string words[] = {"apple", "banana", "orange", "watermelon", "strawberry"};
int numWords = sizeof(words) / sizeof(words[0]);
string getRandomWord() {
srand(time(0));
int randomIndex = rand() % numWords;
return words[randomIndex];
}
string hideWord(const string& word) {
string hiddenWord = "";
for (char c : word) {
hiddenWord += '_';
}
return hiddenWord;
}
bool isWordComplete(const string& word) {
for (char c : word) {
if (c == '_') {
return false;
}
}
return true;
}
void updateHiddenWord(string& hiddenWord, const string& word, char guess) {
for (int i = 0; i < word.length(); i++) {
if (word[i] == guess) {
hiddenWord[i] = guess;
}
}
}
int main() {
string word = getRandomWord();
string hiddenWord = hideWord(word);
int attempts = 0;
cout << "Welcome to Guess the Word game!" << endl;
while (!isWordComplete(hiddenWord)) {
cout << "Guess the word: " << hiddenWord << endl;
char guess;
cout << "Enter your guess: ";
cin >> guess;
updateHiddenWord(hiddenWord, word, guess);
attempts++;
}
cout << "Congratulations! You guessed the word \"" << word << "\" in " << attempts << " attempts." << endl;
return 0;
}