-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJogoDeForca.py
43 lines (39 loc) · 1.36 KB
/
JogoDeForca.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
import random
class JogoDeForca:
def __init__(self, palavra=None):
if palavra is None:
self.palavra = None
import requests
url = 'https://www.ime.usp.br/~pf/dicios/br-sem-acentos.txt'
r = requests.get(url, allow_redirects=True)
if r.status_code==200:
self.content = str(r.content.decode()).split('\n')
else:
print("Erro: ", r.status_code)
else:
self.palavra = palavra
def novo_jogo(self, vidas=5):
self.vidas = vidas
if self.palavra is None:
self.palavra = random.choice(self.content)
return len(self.palavra)
def tentar_letra(self, letra):
if self.vidas > 0:
if letra in self.palavra:
return [idx for idx in range(len(self.palavra)) if self.palavra[idx]==letra]
else:
self.vidas -= 1
if self.vidas == 0:
print("Fim de jogo!")
return False
else:
return []
def tentar_palavra(self, palavra):
if self.vidas > 0:
if self.palavra == palavra:
print ("Ganhou!")
return True
else:
self.vidas = 0
print("Fim de jogo!")
return False