-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordy_chat.py
128 lines (96 loc) · 3.97 KB
/
wordy_chat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
'''
This part handles the game state management, with each user having their own active game.
'''
from game_store import get_info_for_user, set_info_for_user
from wordle_logic import evaluate_guess, generate_new_word
from wordy_types import ActiveGame, EndResult, LetterState, UserInfo
def begin_game(player: UserInfo, lang: str) -> ActiveGame:
"""
Begin a game for a user.
"""
if player.current_game:
raise ValueError("User already has an active game")
# Select a word
answer = generate_new_word(lang)
# Create and store new game state
new_game = ActiveGame(answer=answer, lang=lang)
return new_game
def enter_guess(guess: str, game: ActiveGame) -> EndResult:
"""
Enter a guess for a user's game and reports back if the game ends.
>>> game=ActiveGame(lang="en", answer="abcd")
>>> enter_guess("aaaa", game) == EndResult.PLAYING
True
>>> render_result(game.results[-1])
'🟩⬛⬛⬛'
>>> game=ActiveGame(lang="en", answer="abca")
>>> enter_guess("aaaz", game) == EndResult.PLAYING
True
>>> render_result(game.results[-1])
'🟩🟨⬛⬛'
>>> game=ActiveGame(lang="en", answer="abca")
>>> enter_guess("aaab", game) == EndResult.PLAYING
True
>>> render_result(game.results[-1])
'🟩🟨⬛🟨'
"""
if game.state != EndResult.PLAYING:
return game.state
# Evaluate guess
result = tuple(evaluate_guess(guess, game.answer))
# Update game state
game.board_state.append(guess)
game.results.append(result)
# Check if game is over
if result == (LetterState.CORRECT,)*len(game.answer):
game.state = EndResult.WIN
elif len(game.board_state) > len(game.answer):
game.state = EndResult.LOSE
return game.state
def get_emotes_for_colorblind(colorblind: bool) -> tuple[str, str, str]:
'''
Get the emotes for to use, based on whether colorblind mode is on or off.
Returns a tuple of the emotes for absent, present, and correct.
'''
if colorblind:
return '⬛', '🟦', '🟧'
else:
return '⬛', '🟨', '🟩'
def render_result(result: tuple[LetterState], colorblind: bool = False) -> str:
"""
Render a result to a string.
>>> render_result((LetterState.ABSENT, LetterState.PRESENT, LetterState.CORRECT))
'⬛🟨🟩'
>>> render_result((LetterState.ABSENT, LetterState.PRESENT, LetterState.CORRECT), True)
'⬛🟦🟧'
>>> render_result((LetterState.ABSENT,)*4)
'⬛⬛⬛⬛'
"""
absent, present, correct = get_emotes_for_colorblind(colorblind)
return "".join(
absent if state == LetterState.ABSENT else
present if state == LetterState.PRESENT else correct
for state in result
)
if __name__ == "__main__":
from game_store import write_to_disk
# Quick chat emulator to test the game logic
def handle_input(guess: str, user_id: int):
player = get_info_for_user(user_id)
if not player.current_game or player.current_game.state != EndResult.PLAYING:
print("Starting new game!")
player.current_game = begin_game(player, 'en')
enter_guess(guess, player.current_game)
for result,word in zip(player.current_game.results, player.current_game.board_state):
print(f"{render_result(result)} {word}")
if player.current_game.state == EndResult.WIN:
print(f"Congratulations! Completed in {len(player.current_game.board_state)} guesses!")
elif player.current_game.state == EndResult.LOSE:
print(f"Sorry, you lost. The answer was {player.current_game.answer}")
if player.current_game.state != EndResult.PLAYING:
player.current_game = None
set_info_for_user(user_id, player)
write_to_disk()
while True:
guess = input("Guess: ")
handle_input(guess, 0xdeadbeef)