I have recently released a simple terminal based Mastermind game: Solvemind. Its source is available on Github. In this game you can guess the computer's random secret code and the computer will answer with a measure of how close you are. Step by step you can you can deduce the secret code to win the game.

Additionally this implementation enables you to analyze the current situation. You can list all possible secret codes, i.e. all codes which are consistent with the responses the computer has given so far. You can also compute what next guess is (probably) optimal, in that it minimizes the worst-case count of possible secret codes after the response to that guess. This is equivalent to rating of guesses according to the Min-Max-Rule with a depth of 1 and then using the heuristic of minimizing the number of possible secret codes.

This system of recursively analyzing future moves to a finite depth and then using a heuristic as an artificial base case, is for example how chess AIs work. A "really" optimal solution would be to compute all possible future turns / guesses and responses recursively and rate them using the Min-Max-Rule. This is not feasible because the time to compute grows exponentially with the depth. In the case of Mastermind a depth of 1 is already taking quite some time for non-trivial game states on my machine, depending on the exact situation.

Solvemind is written in C and uses nothing but the standard C library.

## Rules

You try to guess the secret code of the computer. This code consists of four
slots, each filled with one "colour" (here the letters `a`

through `h`

). Once
the game is started you simply enter your guess, e.g. `aafb`

and hit the enter
key.

The response consists of two numbers: fit and misplaced. "fit" shows how many
of your letters a exactly matching the letter of the secret code in the same
slot. "misplaced" shows how many of your letters that are not exactly matching
the letter in the same spot could be rearranged so that they would "fit".
*Fun fact*: This distance measure is symmetrical.

## Commands

In Solvemind you can enter a command instead of a code to do a special action not directly tied to the game itself.

`:new`

Start a new game`:rev`

Reveal the secret code`:pos`

List all possible secret codes`:best`

Calculate the best next guess, see above. (Can take some time!)`:turns`

Print all guesses and responses of this game`:pop`

Undo the last guess. Interesting for playing with`:pos`

and`:best`

`:exit`

Exit the game