-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.py
86 lines (61 loc) · 2.29 KB
/
hangman.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
import nltk
import random
import playground as pg
nltk.download("wordnet")
from nltk.corpus import wordnet
from more_itertools import locate
#creating word database for the Hangman
def word_database():
with open("/usr/share/dict/words", "r") as words_database:
words = words_database.read().split()
#keeping only nouns longer than 2 characters without punctuation mark
nouns = list(filter(lambda word: wordnet.synsets(word, pos=wordnet.NOUN) and len(word) > 2 and '\'' not in word, words))
return nouns
#printing current state of guessing
def print_current_state(pg):
for letter in pg.guess_state:
if letter == ' ':
print('_ ', end='')
else:
print(f"{letter} " , end='')
print()
#validating user input
def get_and_validate_guess(already_tried):
while True:
guess = input("Enter your guess: ").upper()
is_valid_guess = len(guess) == 1 and ord(guess) >= ord('A') and ord(guess) <= ord('Z')
if guess in already_tried:
print(f"{guess} has been already tried, try again")
continue
if is_valid_guess:
already_tried.add(guess)
return guess
print("Invalid letter, try again")
#evaluating checked guess
def evaluate_guess(pg, word, guess):
located = list(locate(word, lambda x: x == guess))
if not located:
pg.incorrect_guess()
return
for i in located:
pg.guess_state[i] = guess
#the whole Hangman game
def play():
words = word_database()
selected_word = words[random.randrange(0, len(words))].upper()
playground = pg.Playground(selected_word)
is_guessed = False
already_tried = set()
while playground.incorrect < 7:
print(''.join(playground.hangman))
print_current_state(playground)
guess = get_and_validate_guess(already_tried)
evaluate_guess(playground, selected_word, guess)
if ''.join(playground.guess_state) == selected_word:
is_guessed = True
print(f"You guessed the correct word: {selected_word}!")
return
print(''.join(playground.hangman))
print(f"You wasted all of your attempts\nGiven word was {selected_word}")
if __name__ == "__main__":
play()