2024-01-05 22:30:58 -05:00
|
|
|
"""
|
|
|
|
Author: Brandon Rozek
|
2024-01-05 23:42:44 -05:00
|
|
|
Client for the WordGuess pubnix game.
|
2024-01-05 22:30:58 -05:00
|
|
|
"""
|
|
|
|
from datetime import datetime
|
|
|
|
from pubnix import (
|
|
|
|
run_simple_client,
|
|
|
|
send_message,
|
|
|
|
receive_message
|
|
|
|
)
|
|
|
|
from wordguess import WordGuess
|
|
|
|
|
2024-01-05 23:42:44 -05:00
|
|
|
## Messages
|
|
|
|
|
|
|
|
STARTUP_MESSAGE = lambda nc, td: f"""
|
|
|
|
Welcome to WordGuess!
|
|
|
|
|
|
|
|
The goal is to guess the word of the day.
|
|
|
|
This word is {nc} characters long.
|
|
|
|
|
|
|
|
A * character means a letter was guessed correctly,
|
|
|
|
but in the incorrect position.
|
|
|
|
|
|
|
|
Today is {td}.
|
|
|
|
"""
|
|
|
|
|
2024-01-05 22:30:58 -05:00
|
|
|
WIN_TEXT = lambda score: f"""
|
|
|
|
Congratulations! You solved the word of the day.
|
|
|
|
Come back tomorrow! Your score: {score}
|
|
|
|
"""
|
|
|
|
|
|
|
|
GUESSES_REMAINING = lambda gr: f"You have {gr} guesses remaining."
|
|
|
|
|
|
|
|
LOSE_TEXT = """
|
|
|
|
You ran out of guesses for the day. Come back tomorrow!
|
|
|
|
"""
|
|
|
|
|
2024-01-05 23:42:44 -05:00
|
|
|
## Game Client
|
2024-01-05 22:30:58 -05:00
|
|
|
|
|
|
|
class WordGuessClient:
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def start_game(self, client, _):
|
2024-01-05 23:42:44 -05:00
|
|
|
|
|
|
|
# In case the user already played today, we want to get
|
|
|
|
# information from the server on the current state
|
2024-01-05 22:30:58 -05:00
|
|
|
message = receive_message(client, WordGuess.GameStartMessage)
|
|
|
|
guesses_remaining = message.guesses_remaining
|
|
|
|
is_winner = message.is_winner
|
|
|
|
today = datetime.today().date()
|
|
|
|
|
2024-01-05 23:42:44 -05:00
|
|
|
print(STARTUP_MESSAGE(message.num_characters, today))
|
|
|
|
|
2024-01-05 22:30:58 -05:00
|
|
|
if is_winner:
|
|
|
|
print(WIN_TEXT(guesses_remaining))
|
|
|
|
elif guesses_remaining > 0:
|
|
|
|
print(f"You have {guesses_remaining} guesses remaining")
|
|
|
|
print("\nTo quit, press CTRL-C.")
|
|
|
|
|
2024-01-05 23:42:44 -05:00
|
|
|
# Core loop if the user has more guesses remaining
|
2024-01-05 22:30:58 -05:00
|
|
|
try:
|
|
|
|
while not is_winner and guesses_remaining > 0:
|
|
|
|
guess = input("Guess: ")
|
|
|
|
send_message(client, WordGuess.GuessMessage(guess))
|
|
|
|
|
2024-01-05 23:42:44 -05:00
|
|
|
# Get response from server based on the word provided
|
2024-01-05 22:30:58 -05:00
|
|
|
message = receive_message(client, WordGuess.GuessResponseMessage)
|
|
|
|
if not message.valid:
|
|
|
|
print("Not a valid guess, try again.")
|
|
|
|
continue
|
2024-01-05 23:42:44 -05:00
|
|
|
|
2024-01-05 22:30:58 -05:00
|
|
|
guesses_remaining = message.guesses_remaining
|
|
|
|
is_winner = message.winner
|
2024-01-05 23:42:44 -05:00
|
|
|
|
|
|
|
# Display hints
|
2024-01-05 22:30:58 -05:00
|
|
|
print(message.hint)
|
|
|
|
print(GUESSES_REMAINING(guesses_remaining))
|
|
|
|
print("Letters Guessed:", sorted(message.letters_guessed))
|
2024-01-05 23:42:44 -05:00
|
|
|
|
2024-01-05 22:30:58 -05:00
|
|
|
if is_winner:
|
|
|
|
print(WIN_TEXT(guesses_remaining))
|
2024-01-05 23:42:44 -05:00
|
|
|
|
|
|
|
# Ran out of guesses, present lose text
|
2024-01-05 22:30:58 -05:00
|
|
|
if not is_winner:
|
|
|
|
print(LOSE_TEXT)
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
w = WordGuessClient()
|
|
|
|
run_simple_client(WordGuess.ADDRESS, w.start_game)
|