2019-03-08 22:04:28 +01:00
|
|
|
# hangman.py
|
|
|
|
|
|
|
|
import random
|
|
|
|
|
|
|
|
def get_secret_word(word_file="/usr/share/dict/words"):
|
|
|
|
with open(word_file) as f:
|
|
|
|
good_words = []
|
|
|
|
for i in f:
|
|
|
|
i = i.strip()
|
2019-03-08 22:15:57 +01:00
|
|
|
if len(i) <= 6:
|
2019-03-08 22:04:28 +01:00
|
|
|
continue
|
2019-03-08 22:15:57 +01:00
|
|
|
if not i.isalpha():
|
2019-03-08 22:04:28 +01:00
|
|
|
continue
|
2019-03-08 22:15:57 +01:00
|
|
|
if i[0].isupper():
|
2019-03-08 22:04:28 +01:00
|
|
|
continue
|
|
|
|
good_words.append(i)
|
|
|
|
return random.choice(good_words)
|
|
|
|
|
|
|
|
def get_masked_word(word_file):
|
2019-03-08 22:38:44 +01:00
|
|
|
mask_word="*" * len(word_file)
|
2019-03-08 22:04:28 +01:00
|
|
|
return mask_word
|
|
|
|
|
|
|
|
def type_guess_word(word_file, guess_word, guessed_line):
|
|
|
|
if guess_word in word_file:
|
|
|
|
x = 0
|
|
|
|
for i in word_file:
|
|
|
|
if i == guess_word:
|
|
|
|
guessed_line = guessed_line [0:x] + guess_word + guessed_line[x+1:]
|
|
|
|
x = x+1
|
|
|
|
else:
|
2019-03-08 22:38:44 +01:00
|
|
|
print("\n\033[1;31;40m Pudlo\n\033[1")
|
2019-03-08 22:04:28 +01:00
|
|
|
return guessed_line
|
|
|
|
|
|
|
|
def user_input(input=input):
|
2019-03-08 22:38:44 +01:00
|
|
|
letter = input(" Podaje litere:\n\033[1")
|
2019-03-08 22:40:25 +01:00
|
|
|
return letter
|
2019-03-08 22:04:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
def n_main():
|
|
|
|
print ("Welcome.")
|
|
|
|
s_word =(get_secret_word())
|
|
|
|
guessed_line=(get_masked_word(s_word))
|
|
|
|
print(get_masked_word(s_word))
|
|
|
|
a =True
|
|
|
|
tries=10
|
|
|
|
guess_word_list = []
|
|
|
|
while a:
|
|
|
|
if guessed_line == s_word:
|
2019-03-08 22:38:44 +01:00
|
|
|
print("\n\033[1;33;40m Gratulacje. Wygrales!\033[1")
|
2019-03-08 22:04:28 +01:00
|
|
|
break
|
2019-03-08 22:38:44 +01:00
|
|
|
print("\n\033[1;31;40m Pozostalo prob: {}\033[1".format(tries))
|
2019-03-08 22:04:28 +01:00
|
|
|
guess_word = user_input()
|
|
|
|
if guess_word in guess_word_list:
|
2019-03-08 22:38:44 +01:00
|
|
|
print("\n\033[1;31;40m Podano wczesniej.\033[1")
|
2019-03-08 22:04:28 +01:00
|
|
|
continue
|
|
|
|
if guess_word.isdigit():
|
2019-03-08 22:38:44 +01:00
|
|
|
print("\n\033[1;31;40m Cyfry sa niedozwolone!\033[1")
|
2019-03-08 22:04:28 +01:00
|
|
|
continue
|
|
|
|
if len(guess_word) != 1:
|
2019-03-08 22:38:44 +01:00
|
|
|
print("\n\033[1;31;40m Dopusczalne tylko pojedyncze znaki.\n\033[1")
|
2019-03-08 22:04:28 +01:00
|
|
|
continue
|
|
|
|
guess_word_list.append(guess_word)
|
|
|
|
guessed_line = type_guess_word(s_word, guess_word,guessed_line)
|
|
|
|
print (guessed_line)
|
2019-03-08 22:38:44 +01:00
|
|
|
print("\n\033[1;32;40m Podane litery: {}\033[1".format(guess_word_list))
|
2019-03-08 22:04:28 +01:00
|
|
|
tries = tries-1
|
|
|
|
if (tries < 1):
|
|
|
|
a= False
|
2019-03-08 22:38:44 +01:00
|
|
|
print("\n\033[1;30;47m Nieodgadniete slowo: {} \n\n\033[1".format(s_word))
|
2019-03-08 22:04:28 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
n_main()
|