evil_hangman.py (4087B)
1 """ 2 Evil hangman game. 3 Try to get the user to loose by creating "word trees" 4 where the game will choose the one revealing the least letters. 5 """ 6 7 DICTIONARY_PATH = "dictionary.txt" #path to dictionary 8 9 from eng_dict import * 10 11 def main(): 12 #initialize a new dictionary object 13 dict = eng_dictionary(DICTIONARY_PATH) 14 15 #generate a random starting word 16 starting_word = dict.getRandomWord() 17 word_length = len(starting_word) 18 19 #remove any words not matching the starting words length. 20 dict.trimDictionaryWordLength(word_length) 21 22 current_progress = wordToTemplate(starting_word) 23 24 #greet user 25 print("Computer has now generated a random word.\nStart guessing by typing a letter and then pressing enter.\n") 26 print(current_progress) 27 28 #enter gameloop 29 tries_left = 10 30 guessed_letters = set() 31 while tries_left > 0: 32 #get user input (letter) 33 guessed_letter = input("Guess a letter > ")[0] 34 35 #form word familis based on the guessed letter. 36 word_families = getWordFamilies(dict.dictionary, guessed_letter) 37 38 #filter out and replace current dictionary with the one with the most empty choices. 39 biggest_path = max(word_families, key = lambda x: len(set(word_families[x]))) 40 dict.setDict(word_families[biggest_path]) 41 42 #show progress and update variable "currentprogress" 43 current_progress = getProgress(biggest_path, current_progress) 44 45 #if no word was revealed remove one try 46 if guessed_letter not in biggest_path and guessed_letter not in guessed_letters: 47 tries_left -= 1 48 49 #mark current letter as guessed 50 guessed_letters.add(guessed_letter) 51 52 53 print("===============================\nProgress: ", current_progress, 54 "\nTries left: ", tries_left, "\n===============================") 55 56 #loose condition 57 if tries_left == 0: 58 for word in word_families[biggest_path]: 59 final_word = word 60 print("You have lost!\nThe correct word was: ", final_word) 61 62 #win condition 63 if "-" not in current_progress: 64 print("Congratulations, you have against all odds won!") 65 return 66 67 68 """ 69 Return a dictionary filled with the different word trees. 70 Tree will be a list containg words matching the provided template. 71 The key is the template. 72 Template will look like: ex. g--d 73 74 dict is the current dictionary of words that have not been removed yet. 75 guessed letter is out most recent guess. 76 """ 77 def getWordFamilies(dict, guessed_letter): 78 word_families = {} 79 80 for word in dict: 81 template = wordToTemplate(word, guessed_letter) 82 if template in word_families: 83 word_families[template].add(word) 84 else: 85 word_families[template] = {word} 86 87 """#for debugging 88 #for key in word_families: 89 #print(key, "\t", word_families[key])""" 90 91 return word_families 92 93 94 """ 95 Convert a word into a template. 96 if no "focus_letter" aka guessed letter is provided 97 the word will be converted into just dashes implying an "empty" 98 template. 99 """ 100 def wordToTemplate(word, focus_letter=None): 101 template = "" 102 103 for letter in word: 104 if letter == focus_letter: 105 template += focus_letter 106 else: 107 template += "-" 108 109 return template 110 111 112 """ 113 If the user gets a correct guess add that to the current progress. 114 for example. 115 our current guesses has provided us with the progress g--d 116 the user then guesses o, which for this example is correct. 117 g--d and -oo- will then be combined to form good. 118 """ 119 def getProgress(template, current_progress): 120 stringStream = "" 121 122 for i in range(len(template)): 123 if template[i] != "-": 124 stringStream += template[i] 125 else: 126 stringStream += current_progress[i] 127 128 return stringStream 129 130 131 132 if __name__ == "__main__": 133 main()