Question: Hello I need help formatting this code in Python: Hangman Rules Hangman is a word guessing game. A word is selected and a hint is

Hello I need help formatting this code in Python:
Hangman Rules
Hangman is a word guessing game. A word is selected and a hint is constructed from the word by replacing all non-guessed letters with hyphens. At each turn the player guesses a letter. If that letter is present in the word, the letters that match it in the word are revealed. If not, then the number of incorrect guesses increases.
A player may only guess a single letter at a time, and can not repeat a guess.
The game ends by one of two conditions. First, when the number of incorrect guesses is at least 5, the player loses. Second, when the player has guessed all of the letters in the word, in which case the player wins.
The twist
While you will be writing a program to play Hangman, we want to make it cheat as much as possible, while still presenting only correct information. The basic idea is the program maintains a list of possible words. Whenever the players guesses a letter, the word list is sifted through and reduced to leave as many potential words.
To do this reduction, the words are partitioned based upon what hint they would reveal if it were the solution. The maximum sized partite set is then selected to be the new set of words. In the event of a tie, the hint that reveals the fewest letters should be preferred. If there is still a tie, then select the randomly.
The code
Your code must be carefully tested.
Your hangman code needs to have the following functions:
def mask_word(word, guessed):
"""Returns word with all letters not in guessed replaced with hyphens
Args:
word (str): the word to mask
guessed (set): the guessed letters
Returns:
str: the masked word
"""
def partition(words, guessed):
"""Generates the partitions of the set words based upon guessed
Args:
words (set): the word set
guessed (set): the guessed letters
Returns:
dict: The partitions
"""
def max_partition(partitions):
"""Returns the hint for the largest partite set
The maximum partite set is selected by selecting the partite set with
1. The maximum size partite set
2. If more than one maximum, prefer the hint with fewer revealed letters
3. If there is still a tie, select randomly
Args:
partitions (dict): partitions from partition function
Returns:
str: hint for the largest partite set
"""
You should use any additional functions you feel help the program and its design.
The general flow of your program should be:
Read in the dictionary file and filter for a specific word length
Prompt the user for a guess
Partition the word list based upon the guessed letters so far, revealing the mask that has the most ambiguity.
Prompt the user for another guess and repeat.
Displaying Details
When your program is running, it will need to optionally print out the full details of of the decisions being made behind the scenes. To permit the program to still appear as if it were seriously playing, the details are only shown if the user inputs a negative word length when prompted at the start of the game. In other words, if the input word length is positive, then the game plays normally. If the input word length is negative, the absolute value of the input is used for the word length and full details are printed as the game is played.
Test Cases
Testing your code is important! Some example cases you may use include:
Masking:
If you have the word zymurgy and the letters guessed are "rstne", its masked form would be ----r--. If the letters guessed were "ym", its masked form would be -ym---y.
Partitioning:
If you start with the word list {abcd, abce, abdg}, have already guessed ab, leading to the revealed ab-- and then guess c, the resulting partitions should be abc- : {abcd, abce} and ab-- : {abdg}. The decision should then be to reveal abc- and make the word list become {abcd, abce}.
If you had the partitions abc-: {abcd, abce}, ab--: {abdg, abdf}}, then the partition selected should be ab--.
You should have additional test cases.
Helpful Suggestions:
random.choice is useful to randomly select an element from a list
A simple way to determine if a guessed letter was correct is to compare the displayed hint to the hint determined to be the next. If they are the same, then the guess was "incorrect", even if it reduced the potential words

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!