hangman/play.py
2022-03-15 22:14:59 +01:00

66 lines
1.5 KiB
Python

bg_alphabet = "абвгдежзийклмнопрстуфхцчшщъьюя"
BLANK = '_'
MOVE_SEP = '.................................'
WIN_MESSAGE = "WIN"
LOSS_MESSAGE = "LOSE"
HANG_STAGES = [[],
["|"],
["Г"],
["Го"],
["Го", " |"],
["Го", "/|"],
["Го", "/|\\"],
["Го", "/|\\", "/"],
["Го", "/|\\", "/ \\"],
]
def generate_hint(word, guesses):
is_win = True
reveals = []
for pos, character in enumerate(word):
if pos == 0 or pos == len(word) - 1:
reveals += character
elif character in guesses:
reveals += character
else:
is_win = False
reveals += BLANK
return ' '.join(reveals).upper(), is_win
def print_state(state):
for line in HANG_STAGES[state]:
print(line)
def main():
word = input("word >")
guesses = set()
state = 0
while True:
hint, is_win = generate_hint(word, guesses)
print(hint)
print_state(state)
if is_win:
print(WIN_MESSAGE)
break
while True:
guess = input("guess >")
if guess in bg_alphabet:
break
if guess not in word and guess not in guesses:
state += 1
if state > len(HANG_STAGES):
print(LOSS_MESSAGE)
break
guesses.add(guess)
print(MOVE_SEP)
if __name__ == '__main__':
main()