-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHangMan.py
168 lines (138 loc) · 4.3 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
HANGMAN_ASCII_ART = r"""
_ _
| | | |
| |__| | __ _ _ __ __ _ _ __ ___ __ _ _ __
| __ |/ _` | '_ \ / _` | '_ ` _ \ / _` | '_ \
| | | | (_| | | | | (_| | | | | | | (_| | | | |
|_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|
__/ |
|___/"""
MAX_TRIES = 6
name = input('Please enter your name : ')
HANGMAN_PHOTOS = {
0: """
x-------x""",
1: """
x-------x
|
|
|
|
|""",
2: """
x-------x
| |
| 0
|
|
|""",
3: """
x-------x
| |
| 0
| |
|
|""",
4: """
x-------x
| |
| 0
| /|\
|
|""",
5: """
x-------x
| |
| 0
| /|\
| /
|""",
6: """
x-------x
| |
| 0
| /|\
| / \
|"""}
def open_screen():
print(f'{HANGMAN_ASCII_ART}\nHello sir {name},your number of tries : {MAX_TRIES}')
print('Shay Aviv\'s design: bold, creative, balanced.')
def choose_word(file_path, index):
with open(file_path, 'r') as words:
words_data = words.read()
words_list = words_data.split(' ')
if index > len(words_list):
index = 1
chosen_word = words_list[index - 1].replace('\n', '')
return chosen_word
def check_valid_input(letter_guessed, old_letters_guessed):
if len(letter_guessed) != 1:
print("That is not a one letter, you little cheater!")
return False
elif not letter_guessed.isalpha():
print("Think you are a smartie? That's not an English letter.")
return False
elif letter_guessed.lower() in old_letters_guessed:
print("You already guessed that letter.")
return False
else:
return True
def try_update_letter_guessed(letter_guessed, old_letters_guessed, secret_word):
global num_of_tries
if not check_valid_input(letter_guessed, old_letters_guessed):
old_letters_guessed.sort()
print(" -> ".join(old_letters_guessed))
else:
if letter_guessed.lower() not in secret_word:
old_letters_guessed.append(letter_guessed.lower())
num_of_tries += 1
print(':(')
print(print_status(num_of_tries))
print(show_hidden_word(secret_word, old_letters_guessed))
else:
old_letters_guessed.append(letter_guessed.lower())
print(show_hidden_word(secret_word, old_letters_guessed))
def show_hidden_word(secret_word, old_letters_guessed):
correct_guess = ['']
for letter in secret_word:
if letter in old_letters_guessed:
correct_guess.append(letter + " ")
else:
correct_guess.append("_ ")
result = ''.join(correct_guess)
return result
def check_win(secret_word, old_letters_guessed):
if ''.join(show_hidden_word(secret_word, old_letters_guessed).split()) in secret_word:
print(f'Congratulations!\nLooks like master {name} is our new akinator !!!')
return True
return False
def word_len(secret_word):
return '_ ' * len(secret_word)
def print_status(num_of_tries):
return (HANGMAN_PHOTOS[num_of_tries])
num_of_tries = 0
def main():
open_screen()
global num_of_tries
secret_word = choose_word(input(r"Please enter file path: ").lower(), int(input(r"Please choose an index: ")))
old_letters_guessed = []
print(f'Lets start sir {name} !')
print(HANGMAN_PHOTOS[num_of_tries])
print(word_len(secret_word))
while num_of_tries < MAX_TRIES:
try_update_letter_guessed(input(f'{name} Please enter a letter: '), old_letters_guessed, secret_word)
print(f'You have {MAX_TRIES - num_of_tries} tries left.')
show_hidden_word(secret_word, old_letters_guessed)
game_status = check_win(secret_word, old_letters_guessed)
if game_status:
break
if not game_status:
print('GAME OVER!')
print(f'the word was {secret_word}')
nextgame = input('Do you want to keep playing? \nPress 1 to continue and 2 to end game ')
if nextgame == '1':
main()
else:
print(f'It was nice until it was over Mr.{name} ')
if __name__ == "__main__":
main()