QuestionQuestion

Write a program to play hangman where the computer chooses the word and the human user tries to guess the word. The twist is that the computer starts out with a dictionary of many words. After each human guess, the computer eliminates all words containing the letters guessed so far, making the word as hard to find as possible. Correct user guesses only show up once the list has been narrowed down so far that all remaining words have the letter that the user guesses. [Inspired by the SIGCE Nifty Assignment by Keith Schwartz.]

For the version that you turn in include the debugging information as illustrated below so that we can verify your program is working correctly. A sample of running this program is shown below (though it was originally made with a different dictionary, so your numbers will likely be different):
Starting with 58528 words.
What length word do you want? 5
Now we have 3538 words of length 5

15. Letters used so far:
Letters found: _ _ _ _ _
Guess a letter: a
Now we have 2150 words.

14. Letters used so far: A
Letters found: _ _ _ _ _
Guess a letter: e
Now we have 1056 words.

13. Letters used so far: A E
Letters found: _ _ _ _ _
Guess a letter: i
Now we have 642 words.

12. Letters used so far: A E I
Letters found: _ _ _ _ _
Guess a letter: o
Now we have 230 words.

11. Letters used so far: A E I O
Letters found: _ _ _ _ _
Guess a letter: u
Now we have 15 words.

10. Letters used so far: A E I O U
Letters found: _ _ _ _ _
Guess a letter: y
You found letter Y
Now we have 3 words.

9. Letters used so far: A E I O U
Letters found: _ _ Y _ _
Guess a letter: ~
CRYPT FLYNN TRYST

9. Letters used so far: A E I O U
Letters found: _ _ Y _ _
Guess a letter: r
Now we have 1 words.

8. Letters used so far: A E I O R U
Letters found: _ _ Y _ _
Guess a letter: ~
FLYNN

8. Letters used so far: A E I O R U
Letters found: _ _ Y _ _
Guess a letter: f
You found letter F
Now we have 1 words.

7. Letters used so far: A E I O R U
Letters found: F _ Y _ _
Guess a letter: l
You found letter L
Now we have 1 words.

6. Letters used so far: A E I O R U
Letters found: F L Y _ _
Guess a letter: n
You found letter N
Now we have 0 words.
F L Y N N
*** Congratulations, you did it! ***

Done.

Notes
1. Your program must work with dictionary of different sizes. This means you need to either read the file twice (first time to find the size), or use some sort of dynamically sized storage such as a vector. Start with figuring out how to store the dictionary of 58528 words stored in dictionary.txt. You can refer to the macbeth.cpp program that we saw in class as an example of how to read from a file. When you read in words from the dictionary convert everything to uppercase so that your word and letter comparisons work correctly. Also eliminate words containing punctuation (e.g. "actor's") and convert all words to upper-case.
2. Start with 15 turns. On each turn display the number of words we are (still) working with. (If you want to make a copy for your friends then you will want to remove this part of the display.) Also display the characters the user has guessed so far, as well as any letters in the word that have been correctly guessed.
3. Each time the user guesses a letter the computer removes from the words list all words containing that letter. Eventually this no longer works, as eliminating all words with a particular letter means there are no words remaining. At this point the computer finds the first word on the list and displays the user's guessed character in the position in which it is found in that word. At this point you must also eliminate any words that have duplicates of that letter in some additional position as compared to this first word on your word list.
4. The computer still tries to be tricky, however, so it will also keep any other words still on the list that also have that letter in that position.
5. Giving input of '~' results in a "cheat" where the computer displays all words that are still possibilities.
6. All letters must be guessed individually. Because of the way the program works, this includes having to individually enter multiple copies of the same letter near the end of the game. In other words, if the final word has two 'T' letters, then you will need to enter 'T' twice.    Once the program has to start displaying letters, it must display all occurrences of any guessed letter. Note that this will affect which other words are still possibilities.

Extra
Create a graphical version of the program that either takes input from the text-based version or is completely graphical. In both cases your program must draw the hangman figure as each guess is made of letters not on the board.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

#include <cstdlib>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;

// function prototypes

/**
* convert from entered string to legal string
* @param str
* @return a legal string
*/
string convert(string str);

/**
* read file and put legal strings into a vector
* @param fileName
* @return a vector of unique words
*/
vector<string> readFile(string fileName);

/**
* filter the vector by length of word
* @param vt vector of string
* @param length
*/
void filterByWordLength(vector<string> & vt, unsigned int length);

/**
* fill _ to the array
* @param word the array
* @param length length of the array
*/
void initFoundLetters(char word[], const unsigned int length);

/**
* print content of the array
* @param word the array
* @param length length of the array
*/
void printFoundLetters(char word[], const unsigned int length);

/**
* print all item of the vector to screen
* @param usedLetter the vector
*/
void printUsedLetters(const vector<char> & usedLetter);

/**
*
* @param usedLetter the vector of letters
* @param letter a letter
* @return true if this letter is found in the vector
*/
bool containLetter(const vector<char> & usedLetter, char letter);

/**
* find a letter in a string
* @param str the string
* @param letter the letter
* @return true when the letter is found in the string
*/
int findLetter(string str, char letter);

/**
* if letter is found in the string then
* it will be put in word at the right index
* @param str the string
* @param letter the letter
* @param word the array of letter
* @return number of letter which is put into the word
*/
int fillLetter(string str, char letter, char word[]);

/**
* try to remove string which doesn't match the word
* from vt
* @param vt vector of strings
* @param word array of letter
* @param length length of the array
*/
void filterByLetter(vector<string> & vt,
       char word[], const unsigned int length);

/**
* remove all string in the vector which contains the letter
* @param vt
* @param letter
*/
void filterByLetter(vector<string> & vt, char letter);

/**
* play the game
* @param vts vector of strings
*/
void play(vector<string> & vts);

int main(int argc, char** argv) {

    cout << "File is being processed, Please be patient\n";

    unsigned int lengrth;
    vector<string> vt = readFile("dictionary.txt");

    cout << "Starting with " << vt.size() << " words.\n";
    cout << "What length word do you want? ";
    cin >> lengrth;

    filterByWordLength(vt, lengrth);

    cout << "Now we have " << vt.size()
            << " words of length " << lengrth << "\n\n";...

By purchasing this solution you'll be able to access the following files:
Solution.cpp.

$50.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats