-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman_driver.py
88 lines (63 loc) · 2.96 KB
/
hangman_driver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""
Hangman
Goal: The most optimized solution for predicting a word. Given only the length of the word and the knowledge of a dictionary.
"""
# Ignoring warnings
import warnings
warnings.filterwarnings("ignore")
# Importing all dependencies and other helpful moduels
from helper_modules.word_chooser import get_a_word
from helper_modules.utility_functions import preprocess
from helper_modules.predict_evaluate import predict_a_letter, evaluate_prediction
def print_board(board, guessed_letter, missed):
"""
A function to print the current state of the board
:param board: Defines the state of the board
:param guessed_letter: Predicted letter by the program
:param missed: A set of words that were wrongly predicted by the program
"""
print("\n\nguess:", guessed_letter, end="\n")
for element in board:
print(element, sep=' ', end=" ")
print("missed:", end='')
for element in missed:
print(element, sep=',', end=" ")
def play_game(board, right_word, dictionary, verbose=True):
"""
The `main` function which drives the logic of the program
:param board: Defines the state of the board
:param right_word: The actual word to predict
:param dictionary: Updated list of possible set of words, one of which might be the answer
:param verbose: A flag vale to print/ not print the status of board at every level
return (<0/1>, <int>)
"""
# Picking the words which are of the given length for faster results
len_word = len(board)
updated_dictionary = dictionary[len_word]
missed = set()
rightly_guessed = set([''])
if verbose:
print("\n\nActual word", right_word)
print_board(board, "", "")
# Keep guessing until there are no blanks in the board
while ('_' in board):
# Predict a letter, evaluate the letter, update the dictionary and missed set
guessed_letter = predict_a_letter(rightly_guessed, updated_dictionary)
board, updated_dictionary, missed, rightly_guessed = evaluate_prediction(board, updated_dictionary, missed, rightly_guessed, right_word, guessed_letter)
if verbose:
print_board(board, guessed_letter, missed)
# If missed letters are more than 6, terminate the prediction system
if len(missed) > 6:
total_guesses = len(right_word) + len(missed)
return (0, total_guesses)
# Total guesses taken by the program to predict a given word
total_guesses = len(right_word) + len(missed)
return (1, total_guesses)
# Driver Program
if __name__ == '__main__':
# Generating a random word
board, right_word = get_a_word()
# Preprocessing the words.txt file to sort according to the length and form a dictionary, for fastest access.
dictionary = preprocess('words.txt')
# Starting the prediction program
play_game(board, right_word, dictionary)