76 lines
2.0 KiB
Python
76 lines
2.0 KiB
Python
from exceptions import WordleInvalidGuess, WordleUnknownWord
|
|
from wordle import Wordle
|
|
from wordle import CORRECT, IN_WORD, NOT_IN_WORD
|
|
from util import green, yellow, grey, empty_guess
|
|
|
|
def print_guess(guess, result):
|
|
string = ""
|
|
guess = guess.upper()
|
|
for c, r in zip(guess, result):
|
|
if r == CORRECT:
|
|
string += green(f" {c} ") + " "
|
|
elif r == IN_WORD:
|
|
string += yellow(f" {c} ") + " "
|
|
elif r == NOT_IN_WORD:
|
|
string += grey(f" {c} ") + " "
|
|
print(string, end="")
|
|
|
|
def print_character_state(game, characters):
|
|
string = ""
|
|
states = game.get_characters()
|
|
for c in characters:
|
|
if states[c] == CORRECT:
|
|
string += green(f" {c} ") + " "
|
|
elif states[c] == IN_WORD:
|
|
string += yellow(f" {c} ") + " "
|
|
elif states[c] == NOT_IN_WORD:
|
|
string += grey(f" {c} ") + " "
|
|
elif states[c] == None:
|
|
string += f" {c} "
|
|
print(string, end="")
|
|
|
|
def print_state(game):
|
|
guesses = game.guesses + [empty_guess for j in range(game.max_guesses - len(game.guesses)) ]
|
|
|
|
# keyboard layout
|
|
keyboard = ("qwertzuiop", "asdfghjkl", "yxcvbnm")
|
|
|
|
# guesses
|
|
for index, g in enumerate(guesses):
|
|
guess, result = g
|
|
print_guess(guess, result)
|
|
|
|
# print keyboard
|
|
print(" ", end="")
|
|
if index < 3:
|
|
print_character_state(game, keyboard[index])
|
|
print()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
game = Wordle()
|
|
|
|
print("Game has started:")
|
|
|
|
while not game.has_ended():
|
|
|
|
print_state(game)
|
|
|
|
while True:
|
|
guess = input("Please enter your guess: ")
|
|
try:
|
|
result = game.guess(guess)
|
|
break
|
|
except WordleInvalidGuess:
|
|
print("Invalid guess.", end=" ")
|
|
except WordleUnknownWord:
|
|
print("Unknown word.", end=" ")
|
|
|
|
print_state(game)
|
|
|
|
if game.has_won():
|
|
print("You win!")
|
|
else:
|
|
print(f"You lost. The word was {game._word.upper()} ...")
|