-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordle_logic.py
62 lines (51 loc) · 1.96 KB
/
wordle_logic.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
'''
This file contains the logic for playing a game of Wordle.
'''
import random
from typing import Iterator
from dictionary import get_solution_words_for
from wordy_types import LetterState
def evaluate_guess(guess: str, answer: str) -> Iterator[LetterState]:
'''
Evaluate a guess against an answer.
>>> list(evaluate_guess("a", "a")) == [LetterState.CORRECT]
True
>>> list(evaluate_guess("b", "a")) == [LetterState.ABSENT]
True
>>> list(evaluate_guess("aa", "ab")) == [LetterState.CORRECT, LetterState.ABSENT]
True
>>> list(evaluate_guess("aa", "ba")) == [LetterState.ABSENT, LetterState.CORRECT]
True
>>> list(evaluate_guess("ac", "ba")) == [LetterState.PRESENT, LetterState.ABSENT]
True
>>> list(evaluate_guess("wordle", "wordle")) == [LetterState.CORRECT]*6
True
'''
if len(guess) != len(answer):
raise ValueError("Guess and answer must be of same length")
# Count letters in the guess which aren't exactly correct
answer_counts = {}
for guess_letter, answer_letter in zip(guess, answer):
if guess_letter != answer_letter:
answer_counts[answer_letter] = answer_counts.get(answer_letter, 0) + 1
for guess_letter, answer_letter in zip(guess, answer):
# Letter matches
if guess_letter == answer_letter:
yield LetterState.CORRECT
continue
# Letter isn't used at all
if answer_counts.get(guess_letter, 0) <= 0:
yield LetterState.ABSENT
continue
# So the letter is used, but in the wrong place
# Reduce the count of the letter so we don't
# report it too many times
answer_counts[guess_letter] -= 1
yield LetterState.PRESENT
def generate_new_word(lang: str):
'''
Pick a random word as a new game solution.
'''
words = get_solution_words_for(lang)
word = random.choice(words)
return word