-
Notifications
You must be signed in to change notification settings - Fork 2
/
game.py
140 lines (115 loc) · 4.48 KB
/
game.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
import os
def _verify_endgame(word, check_word_in_used_letters, num_lifes):
word_only_alpha = [c.lower() for c in word if c.isalpha()]
if check_word_in_used_letters == len(word_only_alpha):
print('> Parabéns você venceu')
return True
if num_lifes == 0:
print('> Acabaram suas chances')
return True
else:
return False
def _print_word_status(word, num_lifes, used_letters):
if num_lifes == 6:
print(' ______ ')
print(' / | ')
print(' | ')
print(' | ')
print(' | ')
print(' | ')
print(' ')
elif num_lifes == 5:
print(' ______ ')
print(' / | ')
print(' | O ')
print(' | ')
print(' | ')
print(' | ')
print(' ')
elif num_lifes == 4:
print(' ______ ')
print(' / | ')
print(' | O ')
print(' | I ')
print(' | I ')
print(' | ')
print(' ')
elif num_lifes == 3:
print(' ______ ')
print(' / | ')
print(' | O ')
print(' | / I ')
print(' | I ')
print(' | ')
print(' ')
elif num_lifes == 2:
print(' ______ ')
print(' / | ')
print(' | O ')
print(' | / I \ ')
print(' | I ')
print(' | ')
print(' ')
elif num_lifes == 1:
print(' ______ ')
print(' / | ')
print(' | O ')
print(' | / I \ ')
print(' | I ')
print(' | / ')
print(' ')
elif num_lifes == 0:
print(' ______ ')
print(' / | ')
print(' | O ')
print(' | / I \ ')
print(' | I ')
print(' | / \ ')
print(' ')
print('# Palavra secreta: ', end='')
for i in word:
if i in used_letters:
print(f'{i}', end='')
elif not i.isalpha():
print(f'{i}', end='')
else:
print(' _', end='')
print('')
print(f'# Você tem {num_lifes} chances')
print('# Letras ja digitadas: ', end='')
for i in used_letters:
print(i, end=', ')
print('\n')
def _get_letter(used_letters):
while True:
last_letter = input('Digite uma letra: ').upper()[0]
if last_letter in used_letters:
print('> A letra já foi selecionada\n')
elif not last_letter.isalpha():
print('> Caracter invalido\n')
else:
os.system('cls' if os.name == 'nt' else 'clear')
return last_letter
def play(word):
# Function that has the logic of the game
# Checks if the letter typed matches the word
# And gives feedback to the player according to the result
num_lifes = 6
used_letters = []
_print_word_status(word, num_lifes, used_letters)
while True:
flag_correct_letter_attempt = False
check_word_in_used_letters = 0
last_letter = _get_letter(used_letters)
used_letters.append(last_letter)
for i in word:
if i == last_letter:
flag_correct_letter_attempt = True
if i in used_letters:
check_word_in_used_letters += 1
if not flag_correct_letter_attempt:
num_lifes -= 1
print('> Você errou a letra')
if _verify_endgame(word, check_word_in_used_letters, num_lifes): break
_print_word_status(word, num_lifes, used_letters)
_print_word_status(word, num_lifes, used_letters)