Evil Hangman Credit: Assignment originally created by Keith Schwarz 1 INTRODUCTION In this assignment, you will build a mischievous program that bends the rules of Hangman to trounce its human...

1 answer below »
Starting code in Code.zip. Need to fill in the code in EvilHangman.cpp, FamilySet.cpp and FamilySet.h. Instruction in the pdf file.


Evil Hangman Credit: Assignment originally created by Keith Schwarz 1 INTRODUCTION In this assignment, you will build a mischievous program that bends the rules of Hangman to trounce its human opponent time and time again. In doing so, you'll cement your skills with abstract data types and iterators, and will hone your general programming savvy. Plus, you'll end up with a piece of software which will be highly entertaining. In case you aren't familiar with the game Hangman, the rules are as follows: 1. One player chooses a secret word, then writes out a number of dashes equal to the word length. 2. The other player begins guessing letters. Whenever she guesses a letter contained in the hidden word, the first player reveals each instance of that letter in the word. Otherwise, the guess is wrong. 3. The game ends either when all the letters in the word have been revealed or when the guesser has run out of guesses. 2 EVIL HANGMAN STRATEGY Fundamental to the game is the fact the first player accurately represents the word she has chosen. That way, when the other players guess letters, she can reveal whether that letter is in the word. But what happens if the player doesn't do this? This gives the player who chooses the hidden word an enormous advantage. For example, suppose that you're the player trying to guess the word, and at some point, you end up revealing letters until you arrive at this point with only one guess remaining: D O – B L E There are only two words in the English language that match this pattern: “doable” and “double.” If the player who chose the hidden word is playing fairly, then you have a fifty-fifty chance of winning this game if you guess 'A' or 'U' as the missing letter. However, if your opponent is cheating and hasn't actually committed to either word, then there is no possible way you can win this game. No matter what letter you guess, your opponent can claim that she had picked the other word, and you will lose the game. That is, if you guess that the word is “doable,” the opponent can pretend that the committed word was “double” the whole time, and vice-versa. 2.1 EXAMPLE OF EVIL STRATEGY Suppose that you are playing Hangman and it's your turn to choose a word, which we'll assume is of length four. Rather than committing to a secret word, you instead compile a list of every four-letter word in the English language. For simplicity, let's assume that English only has a few four-letter words, all of which are reprinted here: ALLY BETA COOL DEAL ELSE FLEW GOOD HOPE IBEX Now, suppose that your opponent guesses the letter 'E.' You now need to tell your opponent which letters in the word you've “picked” are E's. You haven't picked a word so you have multiple options about where you reveal the E's. Here's the above word list, with E's highlighted in each word: ALLY BETA COOL DEAL ELSE FLEW GOOD HOPE IBEX If you'll notice, every word in your word list falls into one of five “word families”:  ---- contains the word ALLY, COOL, and GOOD  -E-- contains BETA and DEAL  --E- contains FLEW and IBEX  E--E contains ELSE  ---E contains HOPE You can choose to reveal any one of the above five families. There are many ways to pick which family to reveal – perhaps you want to steer your opponent toward a smaller family with more obscure words, or toward a larger family in the hopes of keeping your options open. In this assignment, in the interests of simplicity, we'll adopt the latter approach and always choose the largest of the remaining word families. In this case, it means that you should pick the family ----. This reduces your word list down to ALLY COOL GOOD and since you didn't reveal any letters, you would tell your opponent that his guess was wrong. Now the list of words you have to choose from has been reduced to these three words. If your opponent guesses the letter O, then you would break your word list down into two families:  -OO- contains COOL and GOOD  ---- contains ALLY Since the -OO- family is larger (two words) we go ahead and choose it, revealing two O's in the word and reducing the list down to COOL GOOD But what happens if your opponent guesses a letter that doesn't appear anywhere in your word list? For example, what happens if your opponent now guesses 'T'? This isn't a problem. If you try splitting these words apart into word families, you'll find that there's only one family; the family ---- in which T appears nowhere and which contains both COOL and GOOD. Since there is only one word family here, it's already the largest family, and by picking it you would maintain the word list you already had. There are two possible outcomes of this game. 1. Your opponent might be smart enough to pare the word list down to one word and then guess what that word is. In this case, you should congratulate the player. 2. By far the most common case, your opponent will be completely stumped and will run out of guesses. When this happens, you can pick any word you'd like from your list and say it's the word that you had chosen all along. The beauty of this setup is that your opponent will have no way of knowing that you were dodging guesses the whole time – it looks like you simply picked an unusual word and stuck with it the whole way. 3 THE ASSIGNMENT Your assignment is to write a computer program which plays a game of Hangman using this “Evil Hangman” algorithm. In particular, your program should do the following: 1. Read the file dictionary.txt, which contains the full contents of the Official Scrabble Player's Dictionary, Second Edition. This word list has over 120,000 words, which should be more than enough for our purposes. 2. Prompt the user for a word length, re-prompting as necessary until there's at least one word that's exactly that long. That is, if the user wants to play with words of length -42 or 137, since no English words are that long, you should re-prompt the user. 3. Prompt the user for a number of guesses, which must be an integer greater than zero. Don't worry about unusually large numbers of guesses – after all, having more than 26 guesses is clearly not going to help your opponent! 4. Prompt the user for asking if a running total of the number of words remaining in the word list should be displayed. This completely ruins the illusion of a fair game that you'll be cultivating, but it's quite useful for testing. 5. Play a game of Hangman using the Evil Hangman algorithm, as described below: 1. Construct a list of all words in the English language whose length matches the input length. 2. Print out how many guesses the user has remaining, along with any letters the player has guessed and the current blanked-out version of the word. If the user chose earlier to see the number of words remaining, print that out too. 3. Prompt the user for a single letter guess, re-prompting until the user enters a letter that hasn't guessed yet. Make sure that the input is exactly one character long and that it's a letter of the alphabet. 4. Partition the words in the dictionary into groups by word family. 5. Find the most common “word family” in the remaining words, remove all words from the word list that aren't in that family, and report the position of the letters (if any) to the user. If the word family doesn't contain any copies of the letter, subtract a remaining guess from the user. 6. If the player has run out of guesses, pick a word from the word list and display it as the word that the computer initially “chose.” 7. If the player correctly guesses the word, congratulate the user. 6. Ask if the user wants to play again and loop accordingly. 4 DATA STRUCTURES There are many different ways to go about organizing the data. The most convenient and the one that first comes to mind is some sort of lookup table. While we are playing the game, we need to be able to associate each word with a particular family. To accomplish this, we will use hash table that uses the word family as a key and some sort of list or dictionary to store the words associated with that key. At the very beginning of the game, the user will be asked to give a word length for the mystery word. This will immediately limit the size of the word list. 4.1 UNORDERED MAP The STL library implementation of a hash table is called an unordered map. It has a key and a value associated with the key. Below is an example of how to use this data type: int main() { // Initialize an unordered_map through initializer_list std::unordered_map wordMap(); wordMap.insert({"1st", 1 }); wordMap.insert({"2nd", 2 }); wordMap.insert({"3rd", 3 }); // Iterate over an unordered_map using range based for loop for (std::pair element : wordMap) { std::cout < element.first="">< "="" ::="" "="">< element.second="">< std::endl;="" }="" std::cout="">< "*******************"="">< std::endl;="" get="" an="" iterator="" pointing="" to="" beginning="" of="" map="">::iterator it = wordMap.begin(); // Iterate over the map using iterator while (it != wordMap.end()) { std::cout < it-="">first < "="" ::="" "="">< it-="">second < std::endl; it++; std::endl;="">
Answered Same DayDec 02, 2021

Answer To: Evil Hangman Credit: Assignment originally created by Keith Schwarz 1 INTRODUCTION In this...

Arun Shankar answered on Dec 04 2021
153 Votes
FamilySet.cpp
#include "FamilySet.h"
FamilySet::FamilySet()
{
    // Default constructor. Nothing to code
}
FamilySet::FamilySet(string file)
{
    /* ifstream myFile(file);
    myFile.close(); */
}
FamilySet::FamilySet(string file, int len)
{
    std::ifstream myFile(file);
    string w;
int i = 0;
while (myFile>> w)
{
if(len==w.length())
{
masterList.push_back(w);
++i;
}
}
    myFile.close();
}
FamilySet::~FamilySet()
{
    // Nothing t
o do here.
}
void FamilySet::filterFamilies(string letter, string guessPattern)
{
string family = "";
// The first step is to clear dictionaries to prepare
    // for the new filter based on the letter and pattern
dictionaries.clear();
    
// Iterate over the master list
vector::iterator ptr;
for (ptr = masterList.begin(); ptr string word = *ptr;
family = guessPattern;
for(int i=0;i {
if(word[i]==letter[0])
family[i] = letter[0];
}
unordered_map >::const_iterator got = dictionaries.find(family);
if(got == dictionaries.end())
{
vector v;
v.push_back(word);
dictionaries.insert(family,v);
}
else
{
vector newv(got->second);
newv.push_back(word);
*got->second = newv;
}
}    
}
void FamilySet::setFamily(string family)
{
// This updates the master list. The new master list
    // should contain the words within the dictionary for family.
    // dictionaries should be cleared after updating the master list.
vector::iterator ptr;
for (ptr = masterList.begin(); ptr {
string word = *ptr;
//check if word matches the family pattern
bool match = true;
for(int i=0;i {
if(family[i]!='*')
{
if(family[i]!=word[i]) //remove word from masterList
match = false;
}
}
if(!match)
ptr = masterList.erase(ptr);
else
++ptr;
}
dictionaries.clear();
}
vector FamilySet::getFamily(string family)
{
    return dictionaries.get(family);
}
string FamilySet::getRandomWord()
{
int count = numWords();
    int i = rand()%count;
    return string();
}
int FamilySet::numWords()
{
    return masterList.size();
}
int FamilySet::numFamilies()
{
    return dictionaries.size();
}
void FamilySet::resetFamilyIter()
{
    iterCount = 0;
}
bool FamilySet::hasNext() {
    // Returns true as long as there is another family to return
if(iterCount     return true;
return false;
}
string FamilySet::getNextFamily()
{
int i = 0;
auto it;
    for(it = dictionaries.begin(); ((it != dictionaries.end()) && (i    iterCount++;
return it->first;
}
int FamilySet::familySize(string family)
{
    // Returns the size of the dictionary of family.
vector v = getFamily(family);
    return v.size();
}
FamilySet.h
#pragma once
#include
#include
#include
#include // for random number
#include
using namespace std;
class FamilySet
{
public:
    FamilySet();
    // Default
    FamilySet(string file);
    // Initializes from a word file
    FamilySet(string file, int len);
    // Initializes from a word file where the word
    // must be of length len.
    ~FamilySet();
    void filterFamilies(string letter, string guessPattern);
    // Filters each word from the master list based on the
    // letter and guessPattern.
    // Example: letter: "a",
    // word: "ally" (a word from the master list)
    // pattern: "***y"
    // In this case, the family is a**y. Once this
    // is determined, then "ally" can be added to
    // this family dictionary
    void setFamily(string family);
    // This updates the master list. The new master list
    // should contain the words within the dictionary for family.
    // dictionaries should be cleared after updating the master list.
    string getRandomWord();
    // Gets a random word from the master list
    int numWords();
    // Returns total number of words in master list
    int numFamilies();
    // Returns total number of families
    int familySize(string family);
    // Returns the number of words in a family
vector getFamily(string family);
    // Iterator
    void resetFamilyIter();
    bool hasNext();
    string getNextFamily();
    
private:
    vector masterList;    
    // This stores all words currently "valid"
    unordered_map > dictionaries;    
    // Stores a dictionary for each family. Each word from
    // the masterList is contained within one of these
    // these vector dictionaries.
    int iterCount;    // Used for...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here