Coding Wordle

Christmas is a strange time of the year, often you feel like you’ve gone from rushing around in chaos to sitting down with little to do. My Christmas is often like this. This year my sister was doing an online puzzle called Wordle but having completed it I asked – is that it? To which she replied – “oh you can only do one a day”. This led me to look at creating a Wordle program which would allow her to play as many games as she wants.

What Is Wordle

Wordle is a game hosted on the New York Times website (Wordle – The New York Times (nytimes.com)). The game requires you to guess a 5 letter word in six attempts.

When you guess a word (it has to be a legitimate word not just a string of text), each letter is evaluated and there are 3 possible outcomes for a letter.

  1. The letter is not contained in the word we are trying to guess (highlighted as grey)
  2. The letter is contained in the word we are trying to guess but not in the position we have put it in our guess (highlighted in yellow)
  3. The letter is contained in the same position in the word we are trying to guess that we have it in our guess (highlighted in green)

This is very simple once you have seen the game. Below we have an example

Example Game

This is the output of a game I recently played.

  • Guess 1: SOARE – I know the first letter now is S, and the word contains the letters R and E. I also know O and A are not contained in the word.
  • Guess 2: SLIER – no new letters in the word are revealed – but I know it doesn’t contain L or I
  • Guess 3: SUPER – No new letters in the word are revealed – but I know it doesn’t contain U or P
  • Guess 4: SHYER- No new letters in the word are revealed – but I know it doesn’t contain H or Y
  • Guess 5: STEER – I now know the 3rd letter is an E and no T is contained in the word
  • Guess 6: SNEER – This is the correct word I WON

Had guess 6 not being correct I would have lost the game.

What Do We Need to Code

So thinking about how the game works what exactly do we need to have to code this?

Firstly we will need a list of words to choose from – all of them need to be 5 letters long and somewhat “guessable”

Secondly we need a bigger list of words for the user to pick from – we want users to be able to guess obscure words, but its a bit harsh to have obscure words as the word to guess, I think the game is not fun if you are unaware of what the winning word is.

The game itself (once an answer is randomly selected) repeats the following pattern;

  1. Receive input from a user
  2. Evaluate the input – what letters should be grey, yellow or green
  3. Communicate the result with the user
  4. Go back to step 3 until the word is guessed or 6 attempts have passed.

Coding Wordle

I don’t want to have to code a GUI for this, if I were to make this properly I would go to this effort, but for a blog post / to give my sister something to do at Christmas it isn’t worth the effort. However with no GUI there does seem to be a problem with how we can display the Grey/Yellow/Green outcomes of letters.

Luckily there is a library that can help us. We can use the Rich library (rich) in Python to allow us to colour texts and background in a console app.

Selecting An Answer

I obtained some dictionaries containing 5 letter English language words, I filtered one to be a subset of another, with it containing more common words.

I can then use Pandas to import these words and make all the words upper case.

so at this point I have data frames containing the words in which the i canswer will be selected and those values which are appropriate guesses.

I can write a small function to pick a random word which will be the answer to the game.

I check that each word is 5 characters long (in case there is some issue with the data) and then pick a random word from the collection.

Obtaining User Input

This section is quite simple – I will simply prompt the user to enter their guess and then have some validation to check the following;

  1. The word entered is actually a word (it is in the larger of the two dictionaries)
  2. The word entered is 5 characters long
  3. The entered hasn’t previously been entered by a user

This is shown below;

Notice how it continues to ask the user for an answer until these conditions are met.

For example;

Evaluating Guesses

This is where the magic will have to happen. we will need a way of comparing what was entered to the randomly selected answer and highlight for each each letter entered;

  1. if the letter doesnt exist in the word
  2. if the letter exists in the same position in the word
  3. if the letter exists in the word but not in the position entered

The function below achieves this – returning a tuple containing the formatted text of the word and an array of squares. This array of squares will draw a grid at the end of the game showing the progress of guesses in a colour filled grid.

The function is shown below

We loop through each letter in the guess and firstly evaluate if the 1st letter in the guess and answer is equal (green). The second check looks if the letter is in the answer (yellow) and if it is neither of these outcomes, then the letter simply doesnt exist in the word (grey).

Squares are constants that represent the 3 coloured squares (grey, green, yellow).

and the correct/contains/wrong functions simply handle appropriate string formatting in the reply to the console.

So if the answer to be guessed was QUEEN and I guessed QUIET. this function would return the following;

the second element of the tuple (pattern) would be used at the end of the game to show the progress of all guesses, until the game ends it is appended to a collection.

Communicating Results

As shown in the previous functions we have an array of letters with highlighting. We can communicate this back to the user via the console, and check if the game has ended.

where allGuesses is the array representing each evaluation of the guess combined into one collection. so example. If the answer was Queen and i guessed Quips and Quits

the first element in the array would be.

and the second would be.

So on each answer you can see the previous answers you have submitted with their formatting;

The Game Loop

I have created a method called game which controls the logic flow highlighted earlier. A boolean value endOfGame is set to false initially and is set to true once the conditions have being met to end the game.

Victory or Defeat is then evaluated.

and regardless of the outcome the pattern array of arrays is printed to show the guessing pattern. For example if I lost the example I have used so far I would see the following;

Below is an example of a victory;

Conclusion

Now we have a Wordle game which you can play as many times as you like. I however am particularly bad at “word” games (if you read these blog posts regularly you have probably already guessed that words are not my forte) and this one is no exception.

What might be interesting for a future post is to write some code to help me play Wordle – a programme that suggest possible words that are valid, I think its possible to write some code that would allow to have a very high chance of victory. Keep your eyes peeled for this in the future.

Code is in GitHub (A Wordle Game in Python (github.com))