Write a program to play hangman where the computer chooses the word and the huma
ID: 3732481 • Letter: W
Question
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):
Notes
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.
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.
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.
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.
Giving input of '~' results in a "cheat" where the computer displays all words that are still possibilities.
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.
CODE BLOCK AND C++. PLEASE MAKE SURE THE PROGRAM RUNS
DICTIONARY LINK: https://sites.google.com/site/uiccs141/programs/4-twisted-hangman/dictionary.txt?attredirects=0
Extra Credit
Up to 10 points extra credit if you create a graphical version of the program that either takes input from the text-based version (up to 5 points) or is completely graphical (up to 10 points). In both cases your program must draw the hangman figure as each guess is made of letters not on the board. If you implement the graphical version, plan on demonstrating it in person to the TA to be graded.
You may not submit a program into both the regular submission as well as the extra credit submission. If you do, you will not be eligible for any extra credit points. Submit your extra credit program using the same naming conventions shown below, into the Blackboard assignment Program 4 Extra Credit.
Explanation / Answer
*** Check out this code
#include <iostream>
#include <fstream>
#include<bits/stdc++.h>
#include <stdlib.h>
#include <time.h>
#include <algorithm>
using namespace std;
vector <string> dictionary;
int word_length = 0;
char char_used[15];
string choosen_string;
int len = 0;
void displayLetter(int till, bool is_print) {
len = 0;
for(int i=0; i<choosen_string.length(); i++) {
bool is_found = false;
for(int j=0; j<till; j++) {
if(choosen_string[i] == char_used[j]) {
if(is_print)
cout << " " << choosen_string[i];
is_found = true;
len ++;
break;
}
}
if(!is_found) {
if(is_print)
cout << " _" ;
}
}
}
void removeWords(char check_char) {
for (int i=0; i<dictionary.size(); i++) {
if (dictionary[i].find(check_char) != string::npos && dictionary[i].compare(choosen_string) != 0) {
dictionary.erase(dictionary.begin()+i);
i--;
}
}
cout << "Now we have " << dictionary.size() << " words";
}
int checkLengthWords() {
for (int i=0; i<dictionary.size(); i++) {
if(dictionary[i].length() != word_length) {
dictionary.erase(dictionary.begin()+i);
i--;
}
}
cout << endl << "Now we have " << dictionary.size() << " words of length " << word_length;
srand (time(NULL));
int random_pos = rand() % dictionary.size();
choosen_string = dictionary[random_pos];
cout << endl << choosen_string;
}
void readDataFromFile(char f_name[20]) {
ifstream ifile;
ifile.open(f_name);
string linebuffer;
while (ifile && getline(ifile, linebuffer)){
if (linebuffer.length() == 0) continue;
transform(linebuffer.begin(), linebuffer.end(),linebuffer.begin(), ::toupper);
dictionary.push_back(linebuffer);
}
ifile.close();
cout << "Starting with " << dictionary.size() << " words" << endl;
}
int main()
{
readDataFromFile("dictionary.txt");
cout << endl << "What length word do you want? ";
cin >> word_length;
checkLengthWords();
bool is_found = false;
for(int i=0; i<15; i++) {
cout << endl << endl << (15 - i) << ". Letters used so far: " << char_used;
cout << endl << "Letters found: " ;
displayLetter(i, true);
cout << endl << "Guess a letter: ";
cin >> char_used[i];
char_used[i] = toupper(char_used[i]);
if (choosen_string.find(char_used[i]) != string::npos) {
cout << "You found letter " << char_used[i] << endl;
displayLetter(i + 1, false);
if(len == choosen_string.length()) {
is_found = true;
break;
}
}
removeWords(char_used[i]);
}
if(is_found)
cout << endl << "*** Congratulations, you did it! ***";
else
cout << endl << "Ooops, You loose it !!";
return 0;
}
** Output **
Starting with 500 words
What length word do you want? 5
Now we have 90 words of length 5
ALGAE
15. Letters used so far:
Letters found: _ _ _ _ _
Guess a letter: T
Now we have 80 words
14. Letters used so far: T
Letters found: _ _ _ _ _
Guess a letter: G
You found letter G
Now we have 55 words
13. Letters used so far: TG
Letters found: _ _ G _ _
Guess a letter: A
You found letter A
Now we have 10 words
12. Letters used so far: TGA
Letters found: A _ G A _
Guess a letter: L
You found letter L
Now we have 1 words
11. Letters used so far: TGAL
Letters found: A L G A _
Guess a letter: E
You found letter E
*** Congratulations, you did it! ***
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.