Initial commit

This commit is contained in:
Daniel Tsvetkov 2022-03-15 22:14:59 +01:00
commit a9b21ec33f
3 changed files with 108 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.idea
__pycache__
venv

40
guess.py Normal file
View File

@ -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()

65
play.py Normal file
View File

@ -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()