Sunday, February 13, 2022

Wordle with Numbers

Not Wordle the game with number as in integers (that game has already been made), but Wordle in Numbers the spreadsheet app!

This is one of those trendy projects ala Flappy Bird that will probably get outdated the moment it's published but it's still a nice introduction to spreadsheets, conditional formatting, and logic.

To recap the rules of Wordle:

  • Guess a 5-letter word.
  • White squares indicate unused guesses.
  • Dark grey indicates that letter is not present in the secret word.
  • Yellow indicates the letter is present but in an incorrect position.
  • Green indicates the letter is present in the correct position.
  • You have six guesses to guess the secret word.
Create a new Numbers spreadsheet. Type in a secret word in Row 1. Each letter should be in its own column:

The secret word is on row 1. Rows 2, 3, 4, 5, 6, 7 will contain our guesses. We can resize our playing area to make square cells and colour the borders by selecting our playing area: cell A2 to E7:

We can give rules to cells in spreadsheets. These rules can change data or the formatting of cells. We're going to have Numbers compare our guesses to the secret word and colour the cell if it's right or wrong.
Let's start with unused guesses. Unused guesses should be white. So, select our playing area of cells A2:E7 and click Format > Cell > Conditional Highlighting.
Add a rule that says Cell is blank and Color Fill it white:

Correct letter guesses that are also in the correct column position are coloured green. Highlight the playing area again (cell A2 to E7) and open Conditional Formatting again (Format > Cell > Conditional Highlighting) and add a rule that says Text is then click the cell reference button the Cell Reference button. Click on the number 1 in the first row to select the entire row:
Choose a green fill and make sure "Preserve Row" is selected. The rule should look like this, note the dollar signs that preserved the absolute reference to the cell:

Correct letters but in a wrong position are coloured yellow. So make sure the playing area is selected again and add a rule that says Text is and click the cell reference button the Cell Reference button. Select cell A1 to check against the first letter of the secret word and choose a yellow fill. Make sure "Preserve Row" and "Preserve Column" are selected:

Create five more rules that reference the next cells: B1, C1, D1 and E1. Make sure to enable "Preserve Row" and "Preserve Column" on each rule so it references the correct row and column with absolute references (the dollar sign $).

Now we can create a rule to show a dark grey if the letter guess is incorrect. With the playing area selected create another rules that says Cell is not blank and choose a custom colour fill of a dark grey colour:

Time to test! Enter a word in your playing area, one letter per cell. The cell colour should change to yellow if you have a correct letter. If it doesn’t work, time to debug or fix your code!
Common mistakes:
  • Not applying the rules to all cells in your playing area
  • Not including the "Preserve Row" or "Preserve Column" (the $ signs) in your formula rule, e.g. $A$1
  • Extra spaces or punctuation
If the game is working then it's time to hide the secret word. Do this by right-clicking Row 1 and selecting Hide row

There are a few issues with hiding the secret word this way. Unscrupulous players can unhide the row and cheat very easily. (nevermind that they can cheat by having endless guesses, and they can even check letter by letter and not submit a whole word like real Wordle!). Also, the secret word needs to be changed manually each time you want to play. It would be nice to create a word bank of many words that get randomly selected for a game. That way, the game maker and player would have no knowledge of the word before playing and more than one game can happen. This kind of game can be created using multiple worksheets, and if I'll try to publish those steps in a separate post.