In one of Uncle Bob’s video, he talked about this game called “Master Mind” when he was teaching Single Responsibility Principle(SRP). After googled the game, turns out that it is actually a quite famous board game. For more information about the game, please look here -> wiki.
The game can be played by two people. One comes up with a code, the other one tries to guess. The one who comes up with the code has to score the guesser’s guess based on some rules. The rules are the following:
- For each exact match(the right char on the right position), give a
- For each half match(the right char on the wrong position), give a
For example, if the code is
ACBB and the guess is
BCAF, then the score would be
+ is for the
C, and two
- are for
After explaining how to play this game, Uncle Bob gives a architecture that looks like this.
This design shows that there are 3 actors in this program: Customer, Game Designer and Strategist. According to the Single Responsibility Principle, there will be one module for each actor. And the Game Interface module and Strategy module are dependent on Game Play module.
Here shows a more detailed design.
Let’s use these as a reference and create some empty classes and interfaces.
Since the center of the program is the game engine, let’s implement enough
GameEngine so that we can pass the test
And it’s not very hard to pass the test of
Then the test of
Now, let’s write tests for the
If all the guesses are invalid, the
nextGuess should return null.
If there is only one guess is valid,
nextGuess should return that one and no more.
Next, test how the
Guesser generate guesses.
Tests for the
Finally, tests for
source code version 8