-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.py
65 lines (55 loc) · 2.07 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
import os
import random
class Hangman:
def __init__(self):
self.words = []
def start(self, language):
self.load_words(language)
self.game_loop()
def load_words(self, language):
with open(f"dictionaries/{language}.txt", 'r') as wordfile:
self.words.extend([line.strip() for line in wordfile.readlines()])
def pick_a_word(self):
return self.words[random.randint(0, len(self.words) - 1)]
def game_loop(self):
game = True
lives = 9
while game:
word = self.pick_a_word()
guesses = set()
current_guess = []
for x in word:
current_guess.append("_")
while True:
print(f"Zgadujesz słowo: {' '.join(current_guess)}")
guess = input("podaj literke (bądź spacje) lub zgadnij całe słowo: ")
if guess == word:
print("Wygrana!")
lives += 2
break
elif guess in guesses:
lives -= 1
print("Juz to próbowałeś...")
elif len(guess) == 1 and guess in word:
guesses.add(guess)
current_guess = []
for x in word:
if any(x == y for y in guesses):
current_guess.append(x)
else:
current_guess.append("_")
else:
lives -= 1
print(f"nie trafione! zostało żyć: {lives}")
if lives == 0:
print(f"przegrana! słowo do zgadnięcia to: {word}")
game = False
break
def main():
available_languages = [x.split(".")[0] for x in os.listdir("dictionaries")]
language = input(f"Wybierz język z {available_languages}: ")
if language not in available_languages:
print("Niestety, nie mamy słownika dla tego języka. Papa!")
exit(1)
Hangman().start(language)
main()