function word_scores_filtered = filter_words(word_scores,words_guessed,results)
% remove words_guessed since those can't be the answer
word_scores_filtered = word_scores;
word_scores_filtered(matches(word_scores_filtered.words,words_guessed),:) = [];
% filter to words that have correct letters in correct positions (green letters)
[rlp,clp] = find(results==2);
letter = extract(words_guessed(rlp(ii)),clp(ii));
% keep only words that have the correct letters in the correct locations
word_scores_filtered = word_scores_filtered(extract(word_scores_filtered.words,clp(ii))==letter,:);
% filter to words that also contain correct letters in other positions (yellow letters)
[rl,cl] = find(results==1);
letter = extract(words_guessed(rl(jj)),cl(jj));
% remove words with letter in same location
word_scores_filtered(extract(word_scores_filtered.words,cl(jj))==letter,:) = [];
% remove words that don't contain letter
word_scores_filtered(~contains(word_scores_filtered.words,letter),:) = [];
% filter to words that also contain no incorrect letters (grey letters)
[ri,ci] = find(results==0);
letter = extract(words_guessed(ri(kk)),ci(kk));
% remove words that contain incorrect letter
word_scores_filtered(contains(word_scores_filtered.words,letter),:) = [];
function results = wordle_feedback(answer, guess)
for ii = 1:5 % for each letter in our guess
letter = extract(guess,ii); % extract that letter
if extract(answer,ii) == letter
% if answer has the letter in the same position
elseif contains(answer,letter)
% if answer has that letter in another position
% if answer does not contain that letter
function [word_to_guess,win,guesses] = play_wordle(word_scores, word_to_guess)
top_words = sortrows(word_scores,2,"descend"); % ensure scores are sorted
for ii = 1:max_guesses % for each of our guesses
% filter our total vocabulary to candidate guesses using progressively different strategies
if ii == 1 % for our first guess, filter down to words with five unique letters and take top score
top_words_filtered = top_words(top_words.num_letters==5,:);
elseif ii <= 3 % if we're generating our second or third guess
% filter out ineligible words and require five unique letters if possible
top_words_filtered = filter_words(top_words(top_words.num_letters==min_uniq,:),guesses(1:ii-1),results(1:ii-1,:));
% if filtering to five unique letters removes all words, allow more repeated letters
while height(top_words_filtered) == 0 && min_uniq > min(word_scores.num_letters)
top_words_filtered = filter_words(top_words(top_words.num_letters==min_uniq,:),guesses(1:ii-1),results(1:ii-1,:));
else % after third guess, set no restrictions on repeated letters
top_words_filtered = filter_words(top_words,guesses(1:ii-1),results(1:ii-1,:));
% generate our guess (if we have any)
if height(top_words_filtered) == 0 % if there are no eligible words in our vocabulary
win = 0; % we don't know the word and we've lost
return % make no more guesses
else % otherwise generate a new guess and get the results
guesses(1,ii) = top_words_filtered.words(1);
results(ii,:) = wordle_feedback(word_to_guess,guesses(1,ii));
% evaluate if we've won, lost, or should keep playing
if guesses(1,ii) == word_to_guess % if our guess is correct
win = 1; % set the win flag
return % make no more guesses
elseif ii == max_guesses % if we've already used all our guesses and they're all wrong
win = 0; % we've lost and the loop will end
else % otherwise we're still playing
% citation: Jim Goodall, 2020. Stack Overflow, available at: https://stackoverflow.com/a/60181033
function [clean_s] = removediacritics(s)
%REMOVEDIACRITICS Removes diacritics from text.
% This function removes many common diacritics from strings, such as
% â - the circumflex accent
% ü - the diaeresis, or trema, or umlaut
% ø - the slash, or solidus, or virgule
s = regexprep(s,'(?:Á|À|Â|Ã|Ä|Å)','A');
s = regexprep(s,'(?:Æ)','AE');
s = regexprep(s,'(?:ß)','ss');
s = regexprep(s,'(?:Ç)','C');
s = regexprep(s,'(?:Ð)','D');
s = regexprep(s,'(?:É|È|Ê|Ë)','E');
s = regexprep(s,'(?:Í|Ì|Î|Ï)','I');
s = regexprep(s,'(?:Ñ)','N');
s = regexprep(s,'(?:Ó|Ò|Ô|Ö|Õ|Ø)','O');
s = regexprep(s,'(?:Œ)','OE');
s = regexprep(s,'(?:Ú|Ù|Û|Ü)','U');
s = regexprep(s,'(?:Ý|Ÿ)','Y');
s = regexprep(s,'(?:á|à|â|ä|ã|å)','a');
s = regexprep(s,'(?:æ)','ae');
s = regexprep(s,'(?:ç)','c');
s = regexprep(s,'(?:ð)','d');
s = regexprep(s,'(?:é|è|ê|ë)','e');
s = regexprep(s,'(?:í|ì|î|ï)','i');
s = regexprep(s,'(?:ñ)','n');
s = regexprep(s,'(?:ó|ò|ô|ö|õ|ø)','o');
s = regexprep(s,'(?:œ)','oe');
s = regexprep(s,'(?:ú|ù|ü|û)','u');
s = regexprep(s,'(?:ý|ÿ)','y');
Comments
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.