31
loading...
This website collects cookies to deliver better user experience
def play(secret_word):
words = get_all_words()
for i in range(0, 6):
guess = pick_word(words)
result = calculate_guess(secret_word, guess)
if all(result[i] == Result.CorrectSpot for i in range(0, len(result))):
print(f"I got it in {number_of_guesses} tries! The secret word was: {secret_word}")
break
words = filter_words(guess, result, words, secret_word)
words
. And then, iterate 6 times picking a random word from words
, calculating the result for this guess, and, if in the result all the letters are in the correct spot, then I know I won. Otherwise, I filter the words on the array with the information I got from the result.class Result(Enum):
CorrectSpot = 1
IncorrectSpot = 2
NoSpot = 3
def calculate_guess(secret_word, guess_word):
def match_latter(secret_word, guess_word, i):
if secret_word[i] == guess_word[i]:
return Result.CorrectSpot
elif guess_word[i] in secret_word:
return Result.IncorrectSpot
else:
return Result.NoSpot
return [match_latter(secret_word, guess_word, i) for i in range(0, len(secret_word))] if (len(guess_word) and len(secret_word)) else None
Result.CorrectSpot
if the letter in the specific position in the guess word is the same - in the same position - in the secret word. An Result.IncorrectSpot
if the letter is not in the same place in the guess word, but it's present in the word somewhere else. And Result.NoSpot
if the letter is not present at all in the secret word.def filter_words(guess_word, result, words):
# remove the guess_word from the list of words
words = [word for word in words if word != guess_word]
# filter by the letters we know aren't in the secret word
forbidden_letters = [guess_word[i] for i in range(0, len(guess_word)) if result[i] == Result.NoSpot]
words = [word for word in words if not any(letter in word for letter in forbidden_letters)]
# filter by the letters we know are in the secret word, in the correct spot
for i in range(0, len(guess_word)):
if result[i] == Result.CorrectSpot:
words = [word for word in words if word[i] == guess_word[i]]
# finally, filter by the letters we know should be in the word
existing_letters = [guess_word[i] for i in range(0, len(guess_word)) if result[i] != Result.NoSpot]
if existing_letters:
words = [word for word in words if any(letter in word for letter in existing_letters)]
return words
words
left. Then, I follow on using the information from the result, that is, removing words that have any letter that was flagged by Result.NoSpot
, removing words that do not have the letters in the specific locations identified by Result.CorrectSpot
, and finally, filtering out the words that don't have any of the letters that we know are in the secret word, tagged by either Result.CorrectSpot
or Result.IncorrectSpot
.words
array.def pick_word(words):
return random.choice(words) if words else None
def smartish_pick_word(words):
good_options = [word for word in words if len(set(word)) == len(word)]
return pick_word(good_options) if len(good_options) else pick_word(words)
def words_with_common_letters(i, words):
# filter words that have at least two of the 8 most common letters
most_common_letters = ['e', 't', 'a', 'o', 'i', 'n', 's', 'r']
good_options = [word for word in words if len(set(word)) == len(word)]
good_options = [word for word in good_options if (sum(1 if letter in most_common_letters else 0 for letter in word)) >= 2] if len(good_options) else []
return smartish_pick_word(i, good_options) if len(good_options) else smartish_pick_word(i, words)
def weigth_words(words):
good_options = [word for word in words if len(set(word)) == len(word)]
if len(good_options) == 0:
good_options = words
words_weigth = [(word, sum(most_common_letters[letter] for letter in word)) for word in good_options]
words_weigth = sorted(words_weigth, key=lambda x: x[1], reverse=True)
return words_weigth[0][0] if len(words_weigth) else words_with_common_letters(words)
def avoid_repetitions_at_first(i, tried_words, words):
if i < 3:
forbidden_letters = [word[i] for word in tried_words]
good_options = [word for word in words if not any(letter in word for letter in forbidden_letters)]
if len(good_options) == 0:
good_options = words
guess = weigth_words(i, good_options)
else:
guess = weigth_words(i, words)
return guess
Naive | Smart-ish | Smart with common letters | Weighting letters | Avoiding repetitions at first | Starting with "prism" | |
---|---|---|---|---|---|---|
Winning percentage | 49.7 | 66.7 | 66.9 | 75 | 76 | 83.4 |
Average tries | 4.1 | 4.1 | 4 | 3.7 | 3.8 | 3.8 |