How to Develop a Wordle Game using TDD in 25 Minutes

How to Develop a Wordle Game using TDD in 25 Minutes

Developing a complete Wordle Game is very easy with TDD

TL;DR: Implementing a working Wordle in a few minutes

Everybody is playing Wordle these days...

And I love TDD.

So, let's get moving...

TL;DR: With just a few steps we can build a robust Wordle.

Defining a word

The minimum information amount in Wordle is a word.

We can argue that letter is smaller, but we think all needed letter protocol is already defined (we might be wrong).

A word is not a char(5).

A word is not an array.

A word is not a string.

This is a common mistake and a bijection violation.

A word and a string have different responsibilities, though they might intersect.

Mixing (accidental) implementation details with (essential) behavior is a widespread mistake.

So we need to define what is a word.

A word in Wordle is a valid 5-letter word.

Let's start with our happy path:

<?php

namespace Wordle;

use PHPUnit\Framework\TestCase;

final class WordTest extends TestCase {
    public function test01ValidWordLettersAreValid() {
        $wordleWord = new Word('valid');
        $this->assertEquals(['v', 'a', 'l', 'i', 'd'], $wordleWord->letters());
    }
}

We assert that prompting for letters in 'valid' returns an array of the letters.

Notice

  • Word class is not defined yet.
  • We don't care about letter sorting. That would be a premature optimization and gold plating scenario.
  • We start with a simple example. No duplicated.
  • We don't mess with word validation yet (the word might be XXXXX).
  • We can start with a simpler test just validation word is created. This would violate the test structure that always requires an assertion.
  • Expected value should always be the first in the assertion.

We get an error:

Error : Class "Wordle\Word" not found

This is good in TDD, We are exploring our domain.

Creating a Word

We need to create a Word with the constructor and the letters() function.

<?php

Namespace Wordle;

final class Word {

    function __construct(string $letters) {
    }

    function letters(): array {
        return ['v', 'a', 'l', 'i', 'd'];
    }
}

Notice

  • We don't need anything to do with the constructor parameter.
  • We hardcode letters function since this is the simplest possible solution up to now. -- Fake it till we make it.
  • Classes are final to avoid subclassification.

We run all the tests (just 1) and we are OK.

OK (1 test, 1 assertion)


Few Letters

Let's write another test:

<?php 

  public function test02FewWordLettersShouldRaiseException() {
        $this->expectException(\Exception::class);
        new Word('vali');
  }

Notice

  • PHPUnit exception raising is not very good. We just declare an exception will be raised.

Test fails...

Failed asserting that exception of type "Wordle\Exception" is thrown.

Changing the current implementation

We need to change our implementation in order to make test02 pass (and also test01)

<?php  

function __construct(string $letters) {
        if (strlen($letters) < 5)
            throw new \Exception('Too few letters. Should be 5');
}

Notice

  • We just check for a few letters. not for too many since we don't have yet a covering test.
  • TDD requires full coverage. Adding another check without a test is a technique violation.
  • We just raise a generic Exception. Creating special exceptions is a code smell that pollutes namespaces. (unless we catch it, but this is not happening right now).

Checking Too Many Letters

Let's check for too many

<?php 
  public function test03TooManyWordLettersShouldRaiseException() {
        $this->expectException(\Exception::class);
        new Word('toolong');
  }

The test fails as expected. Let's correct it.

Failed asserting that exception of type "Exception" is thrown.

<?php

function __construct(string $letters) {
        if (strlen($letters) < 5)
            throw new \Exception('Too few letters. Should be 5');
        if (strlen($letters) > 5)
            throw new \Exception('Too many letters. Should be 5');
}

And all tests passed.

OK (3 tests, 3 assertions)


Refactor (or not)

We can now make an (optional) refactor and change the function to assert a range instead of two boundaries. We decide to leave it this way since it is more declarative.

We can also add a test checking for zero words following the Zombie methodology. Let's do it.

<?php

  public function test04EmptyLettersShouldRaiseException() {
        $this->expectException(\Exception::class);
        $wordleWord = new Word('');
  }

it is no surprise test passes since we already have a test covering this scenario. As this test adds no value we should remove it.


Valid Letter

Let's check now what is a valid letter:

<?php

  public function test05InvalidLettersShouldRaiseException() {
        $this->expectException(\Exception::class);
        $wordleWord = new Word('vali*');
  }

... and the test is broken since no assertion is raised.

Failed asserting that exception of type "Exception" is thrown.

We need to correct the code...

<?php

  function __construct(string $letters) {
       if (str_contains($letters,'*')) {
          throw new \Exception('word contain invalid letters');
  }

And all tests pass since we are clearly hardcoding.

OK (5 tests, 5 assertions)

Notice

  • We hardcode the asterisc to be the only invalid character (as far as we know)
  • We can place the checking code before or after the previous validations. -- Until we have an invalid case (with invalid characters and invalid length) we cannot assume the expected behavior

More Invalid

Let's add more invalid letters and correct the code.

<?php

  public function test06PointShouldRaiseException() {
        $this->expectException(\Exception::class);
        $wordleWord = new Word('v.lid');
  }

  //Solution

  function __construct(string $letters) {
        if (str_contains($letters, '.'))
            throw new \Exception('word contain invalid letters');
    ///....
  }

Notice

  • We didn't write a more generic function (yet) since we cannot correct tests and refactor at the same time (the technique forbids us).

Refactor

All tests are ok. We can refactor. We replace the last two sentences

<?php

  function __construct(string $letters) {
        if (!\preg_match('/^[a-z]+$/i', $letters)) {
            throw new \Exception('word contain invalid letters');
      }
    //..

Notice

  • We can refactor only if we don't change the tests at the same time.
  • The assertion checks only for uppercase letters. Since we are dealing with these examples up to now.
  • We defer design decisions as much as possible.
  • We defined a regular expression based on English Letters. We are pretty sure it won't accept Spanish (ñ), German(ë), etc.

As a checkpoint, we have only five letter words from now on.

Lets assert on letters() function. We left it hard coded. TDD Opens many paths. We need to keep track of all of them until we open new ones.

Comparing Words

We need to compare words

<?php  

  public function test07TwoWordsAreNotTheSame() {
        $firstWord = new Word('valid');
        $secondWord = new Word('happy');
        $this->assertNotEquals($firstWord, $secondWord);
  }

  public function test08TwoWordsAreTheSame() {
        $firstWord = new Word('valid');
        $secondWord = new Word('valid');
        $this->assertEquals($firstWord, $secondWord);
    }

And test fails. Let's use the parameter we are sending to them.

<?php

final class Word {

    private $setters;
    function __construct(string $letters) {
        if (!\preg_match('/^[a-z]+$/i', $letters)) {
            throw new \Exception('word contain invalid letters');
        }
        if (strlen($letters) < 5) {
            throw new \Exception('Too few letters. Should be 5');
        }
        if (strlen($letters) > 5) {
            throw new \Exception('Too many letters. Should be 5');
        }
        $this->letters = $letters;
    }

    function letters(): array {
        return ['v', 'a', 'l', 'i', 'd'];
    }
}

Notice

  • We store the letters and this is enough for object comparison (it might depend on the language).
  • letters() function is still hardcoded

Tests are OK

OK (8 tests, 8 assertions)


More Words

We add a different word for letters comparison

<?php

public function test09LettersForGrassWord() {
        $grassWord = new Word('grass');
        $this->assertEquals(['g', 'r', 'a', 's', 's'], $grassWord->letters());
    }

And test fails.

Failed asserting that two arrays are equal.

It is very important to check for equality/inequality instead of assertTrue() since many IDEs open a comparison tool based on the objects. This is another reason to use IDEs and never text editors.

Let's change the letters() function since we've been faking it.

<?php 

  function letters(): array {
        return str_split($this->letters);
  }

English Dictionary

Our words are in a bijection with English Wordle words. or not?

<?php

  public function test10XXXXIsnotAValidWord() {
        $this->expectException(\Exception::class);
        $wordleWord = new Word('xxxxx');
  }

This test fails. We are not catching invalid English 5-letter words.

We need to make a decision. According to our bijection, there's an external dictionary asserting valid words.

We can validate with the dictionary upon word creation. But we want the dictionary to store valid wordle words. No strings.

It is an egg-chicken problem.

We decide to deal with invalid words in the dictionary and not the Wordle word.


Dictionary Tests

We create new tests on our dictionary.

<?php

namespace Wordle;

use PHPUnit\Framework\TestCase;

final class DictionaryTest extends TestCase {
    public function test01EmptyDictionaryHasNoWords() {
        $dictionary = new Dictionary([]);
        $this->assertEquals(0, $dictionary->wordsCount());
    }
}

The test fails since we have not defined our Dictionary. We do it:

<?php

Namespace Wordle;

final class Dictionary {

    function __construct(array $words) {
        $this->words = $words;
    }

    function wordsCount(): int {
        return 0;
    }
}

Notice

  • We don't do anything with the words yet.
  • We hardcode the number of words.
  • We faked it yet again.

Count

We add another case for count 1 if the dictionary has one word.

<?php
  public function test02SingleDictionaryReturns1AsCount() {
        $words = [new Word('happy')];
        $dictionary = new Dictionary($words);
        $this->assertEquals(1, $dictionary->wordsCount());
   }

The test fails as expected

Failed asserting that 0 matches expected 1.

We correct it.

<?php

final class Dictionary {

    private $words;
    function __construct(array $words) {
        $this->words = $words;
    }

    function wordsCount(): int {
        return count($this->words);
    }
}

Notice

  • Dictionary is immutable
  • No Setters or getters

Including Words

We start with inclusion and get an error.

Error : Call to undefined method Wordle\Dictionary::includesWord()

So we fake it.

<?php
public function test03DictionaryDoesNotIncludeWord() {
        $words = [new Word('happy')];
        $dictionary = new Dictionary($words);
        $this->assertFalse($dictionary->includesWord(new Word('sadly')));
    }


//the solution
function includesWord(): bool {
        return false;
    }

Positive Case

We add a positive case. And we need to correct the function instead of hardcoding it.

<?php 

public function test04DictionaryIncludesWord() {
        $words = [new Word('happy')];
        $dictionary = new Dictionary($words);
        $this->assertTrue($dictionary->includesWord(new Word('happy')));
    }

function includesWord(Word $subjectToSearch): bool {
        return in_array($subjectToSearch, $this->words);
    }

We have the dictionary working.

Wordle Game

Let's create the game.

<?php

final class GameTest extends TestCase {
    public function test01EmptyGameHasNoWinner() {
        $game = new Game();
        $this->assertFalse($game->hasWon());
    }
}

Test fails. We need to create the class and the function.

Creating Game Objects

<?php

Namespace Wordle;

final class Game {

    function __construct() {
    }

    function hasWon(): bool{
        return false;
    }

}

Words tried

We implement words tried.

And the simplest solution.

Hardcoding as always.

<?php

public function test02EmptyGameHasNoWinner() {
        $game = new Game();
        $this->assertEquals([], $game->wordsTried());
    }

//and the model

function wordsTried(): array {
        return [];
    }

Let's try some words. We get

Error : Call to undefined method Wordle\Game::addtry()

We define it.

<?php

public function test03TryOneWordAndRecordIt() {
        $game = new Game();
        $game->addtry(new Word('loser'));
        $this->assertEquals([new Word('loser')], $game->wordsTried());
    }

//The solution

final class Game {

    private $wordsTried;
    function __construct() {
        $this->wordsTried = [];
    }

    function addTry(Word $trial) {
        return $this->wordsTried[] = $trial;
    }

    function wordsTried(): array {
        return $this->wordsTried;
    }
}

Notice

  • We store the trials locally and add the trial and also change wordsTried() real implementation.

Has Lost

We can implement hasLost() if it misses 5 trials. With the simplest implementation as usual.

<?php

public function test04TryOneWordAndDontLooseYet() {
        $game = new Game();
        $game->addtry(new Word('loser'));
        $this->assertFalse($game->hasLost());
    }

//the solution
 function hasLost(): bool {
        return false;
    }

As always. We stop faking it and decide to make it.

Failed asserting that false is true.

So we change it as below.

<?php
  public function test05TryFiveWordsLoses() {
        $game = new Game();
        $game->addtry(new Word('loser'));
        $game->addtry(new Word('loser'));
        $game->addtry(new Word('loser'));
        $game->addtry(new Word('loser'));
        $this->assertFalse($game->hasLost());
        $game->addtry(new Word('loser'));
        $this->assertTrue($game->hasLost());
    }
'''

//The code
function hasLost(): bool {
        return count($this->wordsTried) > 4;
    }

Integrating the Dictionary

We have most of the mechanics. Let's add the dictionary and play invalid.

<?php

    public function test06TryToPlayInvalid() {
        $words = [new Word('happy')];
        $dictionary = new Dictionary($words);
        $game = new Game($dictionary);
        $this->expectException(\Exception::class);
        $game->addtry(new Word('xxxx'));
    }

We need to pass the dictionary to fix the tests

<?php

final class Game {

    private $wordsTried;
    private $dictionary;
    function __construct(Dictionary $validWords) {
        $this->dictionary = $validWords;
        $this->wordsTried = [];
    }

    function addTry(Word $trial) {
        if (!$this->dictionary->includesWord($trial)){
            throw new \Exception('Word is not included ' . $trial);
        }
        return $this->wordsTried[] = $trial;
    }
}

Fixed.


Play to Win

Now, we play to win

<?php
  public function test07GuessesWord() {
        $words = [new Word('happy')];
        $dictionary = new Dictionary($words);
        $winnerWord = new Word('happy');
        $game = new Game($dictionary, $winnerWord);
        $this->assertFalse($game->hasWon());
        $game->addtry(new Word('happy'));
        $this->assertTrue($game->hasWon());
    }

We need to correct hasWon().

<?php

  private $winnerWord;
    function __construct(Dictionary $validWords, Word $winnerWord) {
        $this->dictionary = $validWords;
        $this->wordsTried = [];
        $this->winnerWord = $winnerWord;
    }

    function hasWon(): bool {
        return in_array($this->winnerWord, $this->wordsTried);
    }

Notice

  • We use no flags to check if someone has won. We can directly check it.
  • We make an addParameter refactor with this new element to previous game definitions.

Winner Word

We added winnerWord. We need to assert this word is in the dictionary.

<?php 
 public function test08WinnerWordNotInDictionary() {
        $words = [new Word('happy')];
        $dictionary = new Dictionary($words);
        $winnerWord = new Word('heros');
        $this->expectException(\Exception::class);
        $game = new Game($dictionary, $winnerWord);
    }

//and add the check...

function __construct(Dictionary $validWords, Word $winnerWord) {
        if (!$validWords->includesWord($winnerWord)){
            throw new \Exception('Winner word must be in the dictionary');
        }

OK (8 tests, 10 assertions)


We have all the mechanics.

Letter Positions

Let's add the letter's positions. We can do it in Word class.

<?php
public function test10NoMatch() {
        $firstWord = new Word('trees');
        $secondWord = new Word('valid');
        $this->assertEquals([], $firstWord->matchesPositionWith($secondWord));
    }


//This method in Word class

 function matchesPositionWith(Word $anotherWord) : array {
        return [];
    }

Test passes

OK (10 tests, 10 assertions)


Match

Let's match

<?php
  public function test11MatchesFirstLetter() {
        $firstWord = new Word('trees');
        $secondWord = new Word('table');
        $this->assertEquals([1], $firstWord->matchesPositionWith($secondWord));
    }

Fails.

We need to define it better

<?php
function matchesPositionWith(Word $anotherWord) : array {
        $positions = [];
        for ($currentPosition = 0; $currentPosition < count($this->letters()); $currentPosition++){
            if ($this->letters()[$currentPosition] == $anotherWord->letters()[$currentPosition]) {
                $positions[] = $currentPosition + 1; //Humans start counting on 1
                //We can implement this better in several other languages
            }
        }
        return $positions;
    }

We keep running all the tests all the time

OK (23 tests, 25 assertions)


We can add a safety test to be more declarative

<?php

public function test12MatchesAllLetters() {
        $firstWord = new Word('trees');
        $secondWord = new Word('trees');
        $this->assertEquals([1, 2, 3 , 4 ,5], $firstWord->matchesPositionWith($secondWord));
    }

Incorrect Positions

Now we need the final steps. Matching in incorrect positions. and always the simplest solution...

<?php
public function test13MatchesIncorrectPositions() {
        $firstWord = new Word('trees');
        $secondWord = new Word('drama');
        $this->assertEquals([2], $firstWord->matchesPositionWith($secondWord));
        $this->assertEquals([], $firstWord->matchesIncorrectPositionWith($secondWord));
    }

//the easy solution
function matchesIncorrectPositionWith(Word $anotherWord) : array {
        return [];
    }

Notice

  • By adding these safe, zero cases we miss many usual bugs.

A more spicy test case. Let's go for the implementation

<?php
public function test14MatchesIncorrectPositionsWithMatch() {
        $firstWord = new Word('alarm');
        $secondWord = new Word('drama');
        $this->assertEquals([3], $firstWord->matchesPositionWith($secondWord));
        $this->assertEquals([1, 4, 5], $firstWord->matchesIncorrectPositionWith($secondWord));
        //A*ARM vs *RAMA
        $this->assertEquals([3], $secondWord->matchesPositionWith($firstWord));
        $this->assertEquals([2, 4, 5], $secondWord->matchesIncorrectPositionWith($firstWord));
    }

// The complicated solution

function matchesIncorrectPositionWith(Word $anotherWord) : array {
        $positions = [];
        //count($this->letters() is always 5 but we don't want to add a magic number here
        for ($currentPosition = 0; $currentPosition < count($this->letters()); $currentPosition++){
            if (in_array($this->letters()[$currentPosition], $anotherWord->letters())) {
                $positions[] = $currentPosition + 1; //Humans start counting on 1
                //We can implement this better in several other languages
            }
        }
        return array_values(array_diff($positions, $this->matchesPositionWith($anotherWord)));
    }

We remove from the occurrences the exact matches.

OK (26 tests, 32 assertions)


That's it. We have implemented a very small model with all meaningful rules.

Future Steps

A good model should endure requirements changes.

In the following articles, we will make these enhancements also using TDD:

  • Manage different languages and characters.

  • Import the words from a text file.

  • Add a Visual Engine and host it.

  • Implement an Absurdle

  • Develop a machine-learning algorithm to minimize wordle moves.

Repository

You can have all the code (and make pull requests on GitHub)

Conclusion

TDD is an iterative methodology. If you find some missing functionality you can write me on Twitter and we will add it (after a failing test case, of course).

Hope you like this article and enjoy playing Wordle!

More articles are on the way!