63 lines
1.6 KiB
Python
63 lines
1.6 KiB
Python
#!/usr/bin/python
|
|
|
|
import wordle
|
|
|
|
|
|
def display_instructions():
|
|
"""Print out instuctions before starting play."""
|
|
|
|
print(
|
|
f"""Welcome to Wordle!
|
|
|
|
Wordle has a hidden word of five letters, and you get to
|
|
make up to six guesses to find out what it is.
|
|
|
|
After each guess, you get feedback on each letter of your guess.
|
|
* {wordle.display_result(wordle.LetterResult.CORRECT, "Correct!")}
|
|
* {wordle.display_result(wordle.LetterResult.MOVE, "Correct, in a different location.")}
|
|
* {wordle.display_result(wordle.LetterResult.INCORRECT, "Not in the word.")}
|
|
"""
|
|
)
|
|
|
|
|
|
def read_valid_guess(guess_number):
|
|
while True:
|
|
print(f"Guess {guess_number}: ", end="")
|
|
guess = input()
|
|
if guess in wordle.dictionary():
|
|
return guess
|
|
|
|
print(f"{guess} is not a valid Wordle word. Please try again.")
|
|
|
|
|
|
def display_results(guess, results):
|
|
print(" ", end="")
|
|
for i in range(wordle.WORDLE_LENGTH):
|
|
print(wordle.display_result(results[i], guess[i]), end="")
|
|
print("")
|
|
|
|
|
|
def play_game():
|
|
display_instructions()
|
|
|
|
target = wordle.select_word()
|
|
|
|
# To help with debugging
|
|
# print(f"The target is: {target}")
|
|
|
|
for guess_number in range(1, 6):
|
|
guess = read_valid_guess(guess_number)
|
|
results = wordle.grade_word(target, guess)
|
|
|
|
if wordle.success_result(results):
|
|
print(f"You won! It took {guess_number} guesses.")
|
|
return
|
|
|
|
display_results(guess, results)
|
|
|
|
print(f"Sorry, no guesses remaining. The word was: {target}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
play_game()
|