The goal of this assignment is to use tkinter to produce a c
The goal of this assignment is to use tkinter to produce a class hangman that allows the user to play a simple game of hangman. It will loosely follow the strategy we used for the Calculator in class. Follow these guidelines:
Write a (module level, not in the class) function mask that takes two string arguments, the first a word the second containing exceptions. The function mask returns a string in which every character in the word has been replaced by ‘?’ except for the characters in exceptions. For example:
>>> mask(\'APPLE\',\'AE\')
\'A???E\'
>>> mask(\'APPLE\',\'\')
\'?????\'
>>> mask(\'APPLE\',\'PLEASE\')
\'APPLE\'
>>>
The interface (see images below):
hangman should inherit from Frame.
Hangman Label & Entry displays the current state of the word
Right Label & Entry – letters guessed correctly so far (no repeats listed)
Wrong Label & Entry – letters guessed incorrectly so far (repeats not listed)
26 buttons, one for each letter of the alphabet
Gameplay. The player clicks on a letter button. Three things can happen:
the letter is already in either the list of right or wrong letters. In this case nothing happens.
the letter is in the word (but not in either list). Then that letter is added to the list of right letters, the word is remasked and the masked word (use mask) is displayed next to Hangman (in effect revealing the new letter). If the word is finished a showinfo box states “You Win!”
the letter is not in the word (but not in either list). Then that letter is added to the list of wrong letters. If that list contains 6 or more letters, a showinfo box states “You Lose!”
Implementation details:
hangman subclasses Frame
you may assume that the user will not edit the Entry’s (this can be prevented but requires more than what we know now).
in addition to creating the interface, and calling Frame’s__init__, __init__ should take the given word, make it uppercase, and assign it to self.word
each button should set command=cmd, where cmd is a local function accepting one argument that defaults to the same letter on the button label (same thing we did with the calculator). cmd calls another method click to implement the gameplay above.
Here is an example of how the game works. First, start the game:
>>> root = Tk()
 >>> hangman(\"APPLE\",root).pack() # will close when “X” cliced
Solution
/*
 Write a (module level, not in the class) function mask that takes two string arguments, the first a word the second containing exceptions. The function mask returns a string in which every character in the word has been replaced by ‘?’ except for the characters in exceptions. For example:
 >>> mask(\'APPLE\',\'AE\')
 \'A???E\'
 >>> mask(\'APPLE\',\'\')
 \'?????\'
 >>> mask(\'APPLE\',\'PLEASE\')
 \'APPLE\'
 >>>
 */
 string mask(string word,string exceptions)
 {
 /*
 creating boolean array for checking character of 1st string is there in 2nd string
 as characters may be lowercase or uppercase ; So created the array of size of 123 as ascii value of \'z\' is 122
 */
 bool check_char[123]={0};
//set the locations of array check_char[] depending on ascii value of exceptions string.
 for(unsigned i=0; i<exceptions.length(); i++)
 check_char[(int)exceptions[i]] = 1;
/*
 checking for every character location in arracy check_char[] is set or reset
 If it\'s set then character is present otherwise not present and replace the character in string \'word\' with \'?\'
 */
 for(unsigned i=0; i<word.length(); i++)
 {
 //If location is not set,means character of string word is not present in exceptions and replacing it with \'?\'
 if(!check_char[(int)word[i]])
 word[i] = \'?\';
 }
 return word;
}


