From a9b21ec33f6c24de118740c87a69504d2023f8fe Mon Sep 17 00:00:00 2001 From: Daniel Tsvetkov Date: Tue, 15 Mar 2022 22:14:59 +0100 Subject: [PATCH] Initial commit --- .gitignore | 3 +++ guess.py | 40 +++++++++++++++++++++++++++++++++ play.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 .gitignore create mode 100644 guess.py create mode 100644 play.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8c526ef --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +__pycache__ +venv \ No newline at end of file diff --git a/guess.py b/guess.py new file mode 100644 index 0000000..d1f2660 --- /dev/null +++ b/guess.py @@ -0,0 +1,40 @@ +import os +from collections import Counter + +from play import MOVE_SEP, BLANK + + +def main(): + hint = input("hint >") + no_letters = input('no >') + hint = hint.lower().replace(' ', '') + regex = '' + for char in hint: + regex += '[{}]'.format(char) + regex = regex.replace('[_]', '[[:alpha:]]') + command = "grep -Ei '^{}$' /usr/share/dict/bulgarian".format(regex) + if no_letters: + command += " | grep -Eiv '{}'".format('|'.join(no_letters)) + print(MOVE_SEP) + print(command) + print(MOVE_SEP) + words = os.popen(command).read().split('\n')[:-1] + print("{} words".format(len(words))) + print(MOVE_SEP) + characters = Counter() + for word in words: + word = word.lower() + word_chars = set(word) + characters += Counter(list(word_chars)) + for char in hint: + if char not in [BLANK]: + del characters[char] + if len(words) < 100: + for word in words: + print(word) + print(MOVE_SEP) + print(characters.most_common()) + + +if __name__ == '__main__': + main() diff --git a/play.py b/play.py new file mode 100644 index 0000000..97e5863 --- /dev/null +++ b/play.py @@ -0,0 +1,65 @@ +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()