Creating a Wordle AI with Python

Dağhan
3 min readFeb 8, 2022

What is Wordle?

Wordle is a web-based word game developed by Josh Wardle. Players have six attempts to guess a five-letter word; feedback is given for each guess, in the form of colored tiles, indicating when letters match or occupy the correct position. Yellowish color means that letter exists but it’s in the wrong place while green means that is in the right place. Wordle has a single daily solution, with all players attempting to guess the same word.

What is the best strategy?

Honestly, I have no idea. I always focus on the solution solver rather than the actual solution solver itself. So, this makes my main goal to write the best strategy maker which kinda seems like creating a chess bot. The upside of this is that I only will calculate 6 possible turns and 5 possible letters instead of countless possibilities of chess.

Getting Started

As always, I start with getting the English word database from NLTK.

After downloading the words, we’re gonna get rid of words that have more or fewer letters than 5.

Now, the word we’re looking for is one of the 10.422 words. Our chances of getting the word on the first try are ~%0.0096, chances of getting hit by lightning are ~%0.0065. That’s why we need a combination of a chance and brain.

Photo by Jesse Martini on Unsplash

First, I tried just randomly entering words just to see how frequently it actually finds the words. It was found in ~5404 tries, which is quite higher than the maximum number which is 6.

Next time, I tried guessing by respecting the colors using these two functions:

This is where it gets tricky because I had to create an empty dictionary that would hold the knowledge and pass it to the next try in the function where it also returns the remaining words. It managed the find the word in ~3.805 tries which were way too good than my expectations compared to the table of real player results below:

Being in the top ~%50 didn’t really satisfy me and I wanted to improve it more so I gathered this.

These are the frequency of occurrence of letters in that 10.422 words. This is a good point for a start right? I wanted to change the starting word from random to a predetermined one. Here are my top picks and try counts:

Then, I tried the community starting words:

It doesn’t seem to differ much between the starting words but I might also be missing something. Maybe implementing the letter order would help decrease the average try count but I didn’t find any database for that so I decided to end the experiment after trying today's Wordle puzzle.

It found the word “HUMOR” in 5 tries even though it’s a little bit unlucky that it didn’t find it in 4 tries…

Thank you for reading this, it was fun for me to build a program that could do something that I can’t. I posted the full code down below in case you wanna check it out. See you in the next project!

--

--

Dağhan

A software development student at Bogazici University. I love coding weird stuff as much as watching movies and TV series.