Mastermind (tm) - Pool reduction and Minimax algorithms

Use this section to discuss your embedded Flowcode projects.
mnfisher
Valued Contributor
Posts: 1958
http://meble-kuchenne.info.pl
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 158 times
Been thanked: 926 times

Flowcode v11 Mastermind (tm) - Pool reduction and Minimax algorithms

Post by mnfisher »

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.
mastermind.png
mastermind.png (159.71 KiB) Viewed 162 times

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
Attachments
mastermind.fcfx
(44.21 KiB) Downloaded 24 times

chipfryer27
Valued Contributor
Posts: 1972
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 433 times
Been thanked: 647 times

Re: Mastermind (tm) - Pool reduction and Minimax algorithms

Post by chipfryer27 »

Hi Martin

I was one of the 77 million that had that game <s>

One thing I can't remember about it was if the white (correct colour wrong position) and black (correct colour and position) pegs were related to the answer (i.e. peg position aligned) or if they were just informing that you had these results somewhere.

My mind is also thinking that this was a game on perhaps a SoC Mk14..?? It seems familiar, and if so would not have been resource heavy being a very limited 4-bit machine. Computer set the challenge and you then tried to work it out.

Regards

mnfisher
Valued Contributor
Posts: 1958
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 158 times
Been thanked: 926 times

Re: Mastermind (tm) - Pool reduction and Minimax algorithms

Post by mnfisher »

The score doesn't reflect the position of the pegs - so 2w + 2b doesn't tell you which are in the right place... So unlike Wordle on that regard.

A small MCU would need a random number generator (of some sort) and 16 bits to save the code. What was the UI? I'm thinking 4 buttons to cycle the colour for a column and a 'play' button?

mnfisher
Valued Contributor
Posts: 1958
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 158 times
Been thanked: 926 times

Re: Mastermind (tm) - Pool reduction and Minimax algorithms

Post by mnfisher »

I see there is a new TV quiz show coming based on Wordle.

Sounds like very thin fare?

Much prefer Quordle 😀

chipfryer27
Valued Contributor
Posts: 1972
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 433 times
Been thanked: 647 times

Re: Mastermind (tm) - Pool reduction and Minimax algorithms

Post by chipfryer27 »

Hi

The Mk14 had a rubber membrane Hex keypad and a 8-digit 7-segment display and ran machine code.

Memory (mine) seems to think it generated a 4 digit code that you then tried to figure out. Took forever to play as you had to type in the Hex program before you could play...

Regards

mnfisher
Valued Contributor
Posts: 1958
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 158 times
Been thanked: 926 times

Re: Mastermind (tm) - Pool reduction and Minimax algorithms

Post by mnfisher »

Could probaby find those - for a proper retro vibe?

Will hang fire on the hex entry though - everyone knows that DIP switches are the way to go :-)

chipfryer27
Valued Contributor
Posts: 1972
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 433 times
Been thanked: 647 times

Re: Mastermind (tm) - Pool reduction and Minimax algorithms

Post by chipfryer27 »

🙃🙃 - well I am upside down just now

mnfisher
Valued Contributor
Posts: 1958
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 158 times
Been thanked: 926 times

Re: Mastermind (tm) - Pool reduction and Minimax algorithms

Post by mnfisher »

Well that's just bats 🦇🦇

I quite like the idea of doing a Wordle solver for the esp32 too. I did one on PC - but a little standalone device might be fun... NY Times might sue?

The trouble with doing this - suddenly it's no fun to do the puzzle (it's like infinite lives in computer games)- but writing the program is a better challenge anyway...

chipfryer27
Valued Contributor
Posts: 1972
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 433 times
Been thanked: 647 times

Re: Mastermind (tm) - Pool reduction and Minimax algorithms

Post by chipfryer27 »

I dare to disagree...

I had plenty of fun with a HP PC running at 25MHz with a whopping 8MB of RAM (stop drooling) playing Doom with infinite lives / indestructabilty. Something just felt good slaughtering all the nasties whilst immune to their weapons 🙃

Regards

mnfisher
Valued Contributor
Posts: 1958
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 158 times
Been thanked: 926 times

Re: Mastermind (tm) - Pool reduction and Minimax algorithms

Post by mnfisher »

Maybe in some cases - but I always need invincibility rather than infinite lives - otherwise get stuck at a checkpoint. Hollow Knight anyone?

Got quite adept at Z80 code looking for 'patches' for Spectrum games (lives/protection etc) - but found I enjoyed that more than playing the actual thing (too hard is the usual complaint! Half Life 2 was a notable exception)

Modern games are just way too big to bother with but it might a valid job for AI?

Post Reply