Question: Implement Guess the Word game in an object-oriented manner. This program would involve some basic manipulation of strings/char array and files reading, writing, comparing. The
Implement Guess the Word game in an object-oriented manner. This program would involve some basic manipulation of strings/char array and files reading, writing, comparing. The driver program is given to you and you are NOT allowed to change it. Hence, the obvious overloaded operators will be input >> and output << which need to be implemented.
This assignment should be written as a modular C++ program. You will develop a class (module) called GuessTheWord, with header (.h) and implementation (.cpp) files. The main program should be in its own module, and is given to you.
The game starts by loading the dictionary from an input file. The program then randomly picks a word from the dictionary and displays the appropriate number of dashes along with an initial clue. The player will be given word length + 3 extra attempts to guess the word correctly. As soon as the player enters a character (which should be considered case insensitive), if found in the word it will be updated at all the occurrences.
To summarize the steps:
1. Load the dictionary of words from a user specified file input file if not the default file. (A sample dictionary is given; a bigger dictionary would be available for the demo)
2. The word to be guessed should be chosen randomly from the file.
3. Display the partial word with appropriate number of dashes.
4. Read user input and display corresponding result.
5. Repeat 3 and 4 until done
6. Get user choice to continue/ stop playing.
Programming concepts that are expected in the solution:
1. Object oriented modular solution
2.File IO and string operations
3. Use of selection and repetition constructs
4. Use of random function
5. The class used should have constructors and overloaded input and output operators.
The driver program is given below
#include
#include "GuessTheWord.h"
using namespace std;
int main() {
int playAgain=-1, WordLength = 0;
GuessTheWord game(6);
game.LoadDictionary("WordList_6.txt");
do
{
game.Clear();
//Read from the dictionary of words and select a secret word
WordLength = game.GetSecretWord();
cout << "Game starts | Attempts : " << WordLength + 3 << " / " << WordLength + 3 << endl;
//Print the initial clue using overloaded <<
cout << game;
int j = 0;
for (int i = 0; i <= WordLength + 3; i++)
{
cout << "Enter your next Guess"<< endl;
cin >> game;
//Check if guessed character is present in the word
if (game.CheckChar())
cout << "You got it right | Attempts : "<<(WordLength + 3)-i <<"/"<< WordLength + 3 << endl;
else
cout << "Oops. You got it wrong | Attempts : " << (WordLength + 3) - i << "/" << WordLength + 3 << endl;
cout << game;
//Check for win condition i.e word is complete
if (game.IsWin()) {
cout << "You won!!";
break;
}
}
if(!game.IsWin())
cout << "Sorry, You lost." << endl;
cout << "Press 1 to play again, any key to exit";
cin >> playAgain;
if (playAgain != 1)
break;
playAgain = -1;
} while (true);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
