-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.py
143 lines (119 loc) · 6.17 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
import random
import json
import os
figure = [
' -------------------\n | +----+ |\n | | | |\n | | |\n | | |\n | | |\n | | |\n | ========= |\n -------------------\n',
' -------------------\n | +----+ |\n | | | |\n | O | |\n | | |\n | | |\n | | |\n | ========= |\n -------------------\n',
' -------------------\n | +----+ |\n | | | |\n | O | |\n | | | |\n | | |\n | | |\n | ========= |\n -------------------\n',
' -------------------\n | +----+ |\n | | | |\n | O | |\n | /| | |\n | | |\n | | |\n | ========= |\n -------------------\n',
' -------------------\n | +----+ |\n | | | |\n | O | |\n | /|\\ | |\n | | |\n | | |\n | ========= |\n -------------------\n',
' -------------------\n | +----+ |\n | | | |\n | O | |\n | /|\\ | |\n | / | |\n | | |\n | ========= |\n -------------------\n',
' -------------------\n | +----+ |\n | | | |\n | O | |\n | /|\\ | |\n | / \\ | |\n | | |\n | ========= |\n -------------------\n',
' -------------------\n | +----+ |\n | | |\n | | |\n | | |\n | | |\n | | |\n | ========= |\n -------------------\n\n\n'
]
def move(direction, by):
if direction == "W":
return f"\033[{by}A"
elif direction == "S":
return f"\033[{by}B"
elif direction == "D":
return f"\033[{by}C"
elif direction == "A":
return f"\033[{by}D"
class Hangman:
"""
Console based simple hangman game
"""
initial = True
already_used = False
invalid_input = False
incorrect_input = False
def __init__(self, question) -> None:
self.failed_attempts = 0
self.word_guess_query = question[1]
self.word_to_guess = question[0].upper()
self.game_progress = list(u'\u2B1C' * len(self.word_to_guess))
def game_title(self) -> None:
cwidth = os.get_terminal_size().columns
print('\u001b[1;36m')
print(u'-+-+-+- CONSOLE BASED HANGMAN GAME USING PYTHON -+-+-+-'.center(cwidth))
print(''.center(cwidth,'_'), end="\n\n")
def display_question(self) -> None:
print(u"\n\u001b[1;35m"+f"🔸 {self.word_guess_query}\n")
def get_user_input(self):
return input(" » ").upper()
def is_invalid_letter(self, input_) -> bool:
return (len(input_) >= 1 and not input_.isalpha())
def find_indexes(self, letter):
return [i for i, char in enumerate(self.word_to_guess) if letter == char]
def update_progress(self, letter, indexes):
for index in indexes:
self.game_progress[index] = letter.upper()
def print_game_status(self):
error_info = lambda msg: print(move("W", 2) + "\033[K" + "🔹 "+' '.join(self.game_progress) + f'\t\t \u001b[1;31m({msg})\u001b[1;33m')
if Hangman.incorrect_input == True:
error_info('Incorrect input')
Hangman.incorrect_input = False
return
if Hangman.invalid_input == True:
error_info('Invalid input')
Hangman.invalid_input = False
return
if Hangman.already_used == True:
error_info('You already guessed it')
Hangman.already_used = False
return
if Hangman.initial:
print("\033[K" + "🔹 "+"\u001b[1;33m"+' '.join(self.game_progress))
Hangman.initial = False
else:
print(move("W",2) + "\033[K" + "🔹 "+"\u001b[1;33m"+' '.join(self.game_progress))
def play(self):
global isGameOver
print("\u001b[1;37m"+figure[-1][:-2] if exception_flage == False else "\u001b[1;37m"+figure[-1], end="\n\u001b[1;33m")
while self.failed_attempts < len(figure)-1:
self.print_game_status()
user_input = self.get_user_input()
if self.is_invalid_letter(user_input):
print(move("W", 2))
print("\033[K")
Hangman.invalid_input = True
continue
if user_input in self.game_progress:
print(move("W", 2))
print("\033[K")
Hangman.already_used = True
continue
if user_input in self.word_to_guess:
indexes = self.find_indexes(user_input)
self.update_progress(user_input, indexes)
if u'\u2B1C' not in self.game_progress:
self.print_game_status()
print('\n\n Correct! The word is: {0}'.format(self.word_to_guess))
break
else:
print(move("W", 12) + "\u001b[1;37m" + figure[self.failed_attempts] + move("S", 2), end="\n\u001b[1;33m")
self.failed_attempts += 1
Hangman.incorrect_input = True
if self.failed_attempts >= len(figure)-1:
print("\u001b[1;31m\nGame Over!\u001b[0m\n")
isGameOver = True
return
if __name__ == '__main__':
os.system('cls')
with open('data.json', 'r') as file:
quests = json.load(file)
isGameOver = False
exception_flage = False
while len(quests):
idx = random.choice(list(quests.keys()))
hangman = Hangman(quests[idx])
hangman.game_title()
hangman.display_question()
hangman.play()
quests.pop(idx)
exception_flage = True
if isGameOver:
break
input("\n\u001b[1;32mPress Enter to continue...")
os.system('cls')