Write an interactive program that plays a game of hangman. Store the word to be
ID: 3691185 • Letter: W
Question
Write an interactive program that plays a game of hangman. Store the word to be guessed in successive elements of an array of individual characters called word. The player must guess the letters belonging to word. The program should terminate when either all letters have been guessed correctly (the player wins) or a specified number of incorrect guesses have been made (the computer wins). Hint: Use another array, guessed, to keep track of the solution so far. Initialize all elements of guessed to the '*' symbol. Each time a letter in word is guessed, replace the corresponding '*' in guessed with that letter.
Create the file input.in . Place one word with no more than 10 uppercase
characters with a ' ' (new line). That will be the word to be guessed.
c. Write the function proto-type for scan_word(), a function that opens the file
input.in, scanning in one character at a time to the array word[] until
' ' is encountered. Return the length of word[] (not counting the ' ').
Initialize character array guess[] to 10 '*'’s, as the book suggests.
Write the function proto-type for find_lett(), a function that returns the first
index of the (guessed) letter in the array word[] of length n. If the letter is not
found in the array word[], return an index of -1.
In main() you will need a loop to:
i. display guess[] and the number of misses (initialized to 0)
ii. ask the user for a letter – keep asking until a letter is entered
iii. convert the letter to uppercase if it is lowercase
iv. check if the letter appears in word[]
1. if it appears in word[], replace the corresponding '*' in
guess[]
2. repeat the step above until find_lett() returns -1 so that all of
the repeated letters are “found.”
v. if the letter does not appear in word[], increment the miss counter.
vi. if the miss counter reaches 7, break out of the loop and tell the user they
lost! Alternately, if none of the letters in guess[] are still '*', break
out of the loop and tell the user they won! At the end, display word[].
Explanation / Answer
#here is the code in python which implements hangman
import random
wordlist = ['hangman', 'dinner', 'computer', 'america', 'olympics', 'football', 'minecraft', 'jacket', 'cabbage', 'electricity', 'dog',
'pasta', 'japan', 'water', 'programming', 'anaconda', 'onehunga', 'name', 'windows', 'curtains', 'bieber', 'kirito',
'montenegro', 'wheel', 'civilization', 'physics', 'bluebird' 'table', 'ACDC', 'guardian yam', 'mario', 'parachute', 'agario', 'obama',
'youtube', 'putin', 'dairy', 'christianity', 'club penguin', 'oskahlavistah', 'coins', 'agitating', 'jumping', 'eating',
'your mom', 'executive', 'car', 'jade', 'abraham', 'sand', 'silver', 'uranium', 'oscar is gay', 'bioshock', 'fizzle', 'moonman', 'watermelon',
'WAHAHAHAHAHA', 'steve jobs', 'extreme', 'weeaboo jones', 'hot damn']
class Guess:
def __init__(self):
self.SECRET = random.choice(wordlist)
self.GUESSES_ALLOWED = int(raw_input("The secret's word has %s letter, you how many times can you be mistaken?" % len(self.SECRET)))
self.WRONG = []
self.GUESSED = []
def make_a_guess(self):
while self.GUESSES_ALLOWED:
current_guess = raw_input('Guess a letter!') # TODO: to check for double chars
self.print_mask()
if current_guess in self.GUESSED:
print "You;ve already guessed that! Try again! "
self.make_a_guess()
self.print_mask()
elif current_guess in self.WRONG:
print "You;ve already guessed that! Try again! "
self.make_a_guess()
self.print_mask()
elif current_guess not in self.SECRET:
self.GUESSES_ALLOWED -= 1
print "WRONG! Guesses left: %s " % self.GUESSES_ALLOWED
self.WRONG.append(current_guess)
self.print_mask()
elif current_guess in self.SECRET:
print "CORRECT! Guesses left: %s " % self.GUESSES_ALLOWED
self.GUESSED.append(current_guess)
self.print_mask()
if set(self.GUESSED) == set(self.SECRET):
print "You guessed the word!"
repeat = raw_input("Play again? type y or n and press Enter")
if 'y' in repeat:
a = Guess()
a.make_a_guess()
elif 'n' in repeat:
sys.exit(0)
else:
print "You got '%s' guesses left, you lost!" % self.GUESSES_ALLOWED
repeat = raw_input("Play again? type y or n and press Enter")
if 'y' in repeat:
a = Guess()
a.make_a_guess()
elif 'n' in repeat:
sys.exit(0)
def print_mask(self):
guessed_indexes = []
for letter in self.GUESSED:
indexes_for_one_letter = [i for i, x in enumerate(self.SECRET) if x == letter]
guessed_indexes += indexes_for_one_letter
MASK_TO_PRINT = []
for i, letter in enumerate(self.SECRET):
if i in guessed_indexes:
MASK_TO_PRINT.append(letter)
else:
MASK_TO_PRINT.append("_")
print " ".join(MASK_TO_PRINT)
if __name__ == '__main__':
game = Guess()
game.make_a_guess()
#this is another function display used to give the ouput
def display(word, guessed):
word = ' '.join([ch if ch in guessed else '_' for ch in word])
print(word)
display('computer', set('stop'))
display('cabbage', list('bag'))
display('banana', 'abcn')
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.