Mastermind (tm) - Pool reduction and Minimax algorithms
Posted: Mon May 11, 2026 11:47 am
I spotted an original Mastermind set in a charity shop and wondered if I could write a program to solve. Then the idea of LEDs and such came on me - but the set had gone 
So for those who don't remember it - Invicta's Mastermind had one player set a secret code (4 pegs each any of 6 colours) - then the second player attempts to crack the code by playing 'turns' - which the 1st player marks with white pegs for right peg in wrong position and black pegs for right peg, right place. It sold something like 77million sets!
It begat Wordle (& Quordle) as well as Word Mastermind and Deluxe (with 5 pegs) etc.
So here is a program that generates a secret code - and then plays itself at Mastermind. It uses two algorithms - pool reduction (which is simpler, & faster, but may take more rows to solve the puzzle) and Donald Knuth's Minimax algorithm - which is guaranteed to solve a puzzle in 5 or less turns. However - the pool reduction would probably be my choice as it gives a less 'clinical' game. It could (and I may) - be easily modified to 'play' a human opponent - setting a code and scoring or solving a user code. See 'The Art of Computer Programming' by Don Knuth.
I used an esp32 - the minimax code is fairly compute intensive but doesn't use much RAM apart from one optimisation - I intended to run on an Arduino (or ATTiny!) - but there is a lookup table of all the possible codes (for 4 x 6 colours there are 1296). I'd hit WDT issues on the minimax using my original approach of converting an int (0..1295) into the code using base 6 maths.
I also wrote a small bitmap handler - I think I'd pull this into a component - which is integrated into the code here. The bitmap allows a runtime size selection (using calloc - though malloc may be better) - I wondered about changing the number of pegs/colours for fun
Output is currently to UART - I originally thought touch screen - then moved to WS2812s.
Note - pool reduction uses a 'random' guess and sometimes luck is on it's side - minimax requires the first guess to have two pairs of colour and I use a small (6) selection. It could be RGRG or RGGR for to make play look more 'human'
Martin
So for those who don't remember it - Invicta's Mastermind had one player set a secret code (4 pegs each any of 6 colours) - then the second player attempts to crack the code by playing 'turns' - which the 1st player marks with white pegs for right peg in wrong position and black pegs for right peg, right place. It sold something like 77million sets!
It begat Wordle (& Quordle) as well as Word Mastermind and Deluxe (with 5 pegs) etc.
So here is a program that generates a secret code - and then plays itself at Mastermind. It uses two algorithms - pool reduction (which is simpler, & faster, but may take more rows to solve the puzzle) and Donald Knuth's Minimax algorithm - which is guaranteed to solve a puzzle in 5 or less turns. However - the pool reduction would probably be my choice as it gives a less 'clinical' game. It could (and I may) - be easily modified to 'play' a human opponent - setting a code and scoring or solving a user code. See 'The Art of Computer Programming' by Don Knuth.
I used an esp32 - the minimax code is fairly compute intensive but doesn't use much RAM apart from one optimisation - I intended to run on an Arduino (or ATTiny!) - but there is a lookup table of all the possible codes (for 4 x 6 colours there are 1296). I'd hit WDT issues on the minimax using my original approach of converting an int (0..1295) into the code using base 6 maths.
I also wrote a small bitmap handler - I think I'd pull this into a component - which is integrated into the code here. The bitmap allows a runtime size selection (using calloc - though malloc may be better) - I wondered about changing the number of pegs/colours for fun
Output is currently to UART - I originally thought touch screen - then moved to WS2812s.
Note - pool reduction uses a 'random' guess and sometimes luck is on it's side - minimax requires the first guess to have two pairs of colour and I use a small (6) selection. It could be RGRG or RGGR for to make play look more 'human'
Martin