TEXT FILE IS ALREADY GIVEN, JUST NEED TO READ IN THE PROGRAM. Learning objective
ID: 3836813 • Letter: T
Question
TEXT FILE IS ALREADY GIVEN, JUST NEED TO READ IN THE PROGRAM.
Learning objectives: The intent of this programing project is to allow you the opportunity to demonstrate your ability to solve problems using procedural C++ programming. This project HANG MAN will focus on file I/O and string manipulation in the implementation of the game Hangman. Program Description: In this project, you will write a C++ program that simulates playing the game Hangman. This version of the game will be limited to words with up to eight letters. The program should operate as follows: 1. The program must use a function that displays the hangman's gallows (see below). This function accepts an integer for the total number of incorrect guesses. This number will range from zero, representing the start of the game, to eight representing the end of the game. L L L L L L L L L Game Two Three Four Five Six Seven Game Start Incorrect Incorrect Incorrect Incorrect Incorrect Incorrect Incorrect Over Guess Guesses Guesses Guesses Guesses Guesses Guesses 2. Each time a game is played, the program will randomly select an unknown word from a file called words txt. Each of the thirty words in the file will be... Limited to a maximum of eight letters in length. No letter will ever appear more than once in any word. All words will be in lower case. 3. The program should then display the hangman's gallows, and a string of asterisk representing the unknown word, and a prompt for the letter to be guest.Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <ctime>
#include <vector>
using namespace std;
void printHangman(int errCount, string myWord);
string getRandWord(vector<string> wordList)
{
int randPos = rand() % wordList.size(); // get random position index
return wordList[randPos];// return word
}
vector<string> getWords()
{
time_t t;
srand((unsigned) time(&t)); /* initialize seed to generate random number */
string fname;
cout <<"Enter file name"<< endl;
cin >> fname;
fstream afile;
afile.open(fname.c_str()); // open file
if (!afile) {
cerr << "File I/O error!" << endl;
exit(0);
}
vector<string> wordList;
string word;
while(afile >> word) // read all words from file
wordList.push_back(word);
afile.close();
return wordList;
}
int main()
{
vector<string> wordList = getWords();
int errCount = 0;
char letter;
string randWord = getRandWord(wordList); // select random word from list
string ansWord = randWord;
string myWord = "";
for (int i = 0; i < randWord.size(); i++) myWord += "-"; // guessed word is initially all -
while (errCount < 8) { // run till we have 8 miss
printHangman(errCount, myWord); // print hangman
cout << "guess a letter: ";
cin >> letter;
int pos = randWord.find(letter); // check if guest letter exist
if (pos != string::npos) {
randWord[pos] = '@';
myWord[pos] = letter; // if it exist replace myWord "-" at that position with letter
}
else
errCount++; // else increase miss count
if (myWord.find('-') == string::npos) break; // if there is no "-" in myWord, We have guessed the word correctly, break;
}
printHangman(errCount, myWord);
if (errCount < 8) {
cout << "Congratulations!!! You won!!!" << endl;
} else {
cout << "Sorry You have lost :( " << endl << "The test word was " << ansWord << endl;
}
return 0;
}
void printHangman(int errCount, string myWord)
{
if (errCount < 1) cout << "+" << endl;
else cout << "+----+"<< endl;
if (errCount < 2)cout << "|"<< endl;
else cout << "| |"<< endl;
if (errCount < 3)cout << "|"<< endl;
else cout << "| o"<< endl;
if (errCount < 4)cout << "|"<< endl;
else if(errCount == 4)cout << "| |"<< endl;
else if(errCount == 5)cout << "| \|"<< endl;
else cout << "| \|/"<< endl;
if (errCount < 4)cout << "|"<< endl;
else cout << "| |"<< endl;
if (errCount < 7)cout << "|"<< endl;
else if(errCount == 7)cout << "| /"<< endl;
else cout << "| / \"<< endl;
cout << "|" << endl;
cout << "+-------"<< endl;
cout << endl << endl;
for (int i = 0; i < myWord.size(); i++) cout << myWord[i] << " "; // print space separated word guessed so far
cout << endl;
for (int i = 0; i < myWord.size(); i++) cout << "-" << " ";
cout << endl;
}
sanjiv@sanjiv-VirtualBox:~$ g++ hangman.cpp -o hangman
sanjiv@sanjiv-VirtualBox:~$
sanjiv@sanjiv-VirtualBox:~$ ./hangman
Enter file name
hangman.txt
+
|
|
|
|
|
|
+-------
- - - - - - - -
- - - - - - - -
guess a letter: a
+
|
|
|
|
|
|
+-------
- a - - - - - -
- - - - - - - -
guess a letter: y
+
|
|
|
|
|
|
+-------
- a y - - - - -
- - - - - - - -
guess a letter: t
+
|
|
|
|
|
|
+-------
- a y - - - - t
- - - - - - - -
guess a letter: e
+----+
|
|
|
|
|
|
+-------
- a y - - - - t
- - - - - - - -
guess a letter: o
+----+
| |
|
|
|
|
|
+-------
- a y - - - - t
- - - - - - - -
guess a letter: i
+----+
| |
|
|
|
|
|
+-------
- a y - i - - t
- - - - - - - -
guess a letter: d
+----+
| |
|
|
|
|
|
+-------
d a y - i - - t
- - - - - - - -
guess a letter: l
+----+
| |
|
|
|
|
|
+-------
d a y l i - - t
- - - - - - - -
guess a letter: g
+----+
| |
|
|
|
|
|
+-------
d a y l i g - t
- - - - - - - -
guess a letter: h
+----+
| |
|
|
|
|
|
+-------
d a y l i g h t
- - - - - - - -
Congratulations!!! You won!!!
sanjiv@sanjiv-VirtualBox:~$
sanjiv@sanjiv-VirtualBox:~$ ./hangman
Enter file name
hangman.txt
+
|
|
|
|
|
|
+-------
- - - - - - - -
- - - - - - - -
guess a letter: a
+
|
|
|
|
|
|
+-------
a - - - - - - -
- - - - - - - -
guess a letter: e
+----+
|
|
|
|
|
|
+-------
a - - - - - - -
- - - - - - - -
guess a letter: i
+----+
| |
|
|
|
|
|
+-------
a - - - - - - -
- - - - - - - -
guess a letter: o
+----+
| |
|
|
|
|
|
+-------
a - - - o - - -
- - - - - - - -
guess a letter: m
+----+
| |
| o
|
|
|
|
+-------
a - - - o - - -
- - - - - - - -
guess a letter: n
+----+
| |
| o
| |
| |
|
|
+-------
a - - - o - - -
- - - - - - - -
guess a letter: p
+----+
| |
| o
| |
| |
|
|
+-------
a - - - o - - -
- - - - - - - -
guess a letter: r
+----+
| |
| o
| |/
| |
|
|
+-------
a - - - o - - -
- - - - - - - -
guess a letter: s
+----+
| |
| o
| |/
| |
| /
|
+-------
a - - - o - - -
- - - - - - - -
guess a letter: t
+----+
| |
| o
| |/
| |
| /
|
+-------
a - t - o - - -
- - - - - - - -
guess a letter: u
+----+
| |
| o
| |/
| |
| /
|
+-------
a - t - o u - -
- - - - - - - -
guess a letter: v
+----+
| |
| o
| |/
| |
| /
|
+-------
a - t - o u - -
- - - - - - - -
Sorry You have lost :(
The test word was although
sanjiv@sanjiv-VirtualBox:~$
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.