After looking at the Knights Tour puzzle which was based on the Krishna Lalith version, I was intrigued by another one of her projects which is her Mastermind Game. All the implementations I have seen in the past are where the computer picks a random number and the player must guess it. However in Krishna’s version, it is the computer that must guess the players number.
I decided to make my own version to run on an Arduino UNO. You can choose to have the computer guess your number or try and guess a random number that the computer generates. I used a similar 4x4 keyboard layout and replaced the 4 digit 7 segment display with a 1602 LCD display.
VideoSoftwareI couldn’t find a suitable algorithm even after scanning the Internet. Krishna’s version creates an array of all possible outcomes and basically uses brute force to try every possible outcome eliminating future guesses as it learns whether digits are present or not. My variant is basically the same. Rather than have an array of all possible outcomes, it stores the user’s results along with its guess in a history array and any derived rule in a rules array. The rules array gets applied to future guesses and the history array is used to determine new rules based on the user’s previous responses.
The software operates in three modes based on the following #define
statements:
#define AUTO 10000
#define SINGLE 9651
#define RELEASE 0
:
#define COMPILE_MODE RELEASE
The #define COMPILE_MODE
can be AUTO
, SINGLE
or RELEASE
.
AUTO mode
In this mode, the software will test every permutation of the numbers that the player can choose. It will determine which number took the most guesses. The output is displayed on the Arduino IDE console.
SINGLE mode
After determining which number took the most guesses, it is placed in the #define SINGLE xxxx
compiler constant and the COMPILE_MODE
set to SINGLE
. The output on the Arduino IDE console shows the guesses made and the rules it creates. There are 4 rules
- KNOWN - Known digit in known position
- SOMEWHERE - Known digit in wrong position and known not to be in a specific position
- ANYWHERE - Known digit in wrong position but don’t know anything else.
- NEVER - Digit definitely not present.
By looking at the history of guesses, you can tweak the algorithm and try the AUTO mode again to see if there is an improvement or if there is any sequence of numbers that it can’t solve.
RELEASE mode
This is the final game mode that allows user interaction.
Notable points of interestThe function computerGuessPlayer
is where the computer will try and guess the player’s number. It takes two arguments, both being call-back functions.
pInputFunction
- Function invoked to get the response to the guess
pOutputFunction
- Function invoked to show what guess is being made
This allows the AUTO & SINGLE modes to test different values to guess and the RELEASE mode to use the keyboard and display.
The rules array only contains unique rules and is sorted so that KNOWN rules are tested before SOMEWHERE and ANYWHERE rules.
Building the gameI designed a PCB to hold the switches and display and 3D printed a suitable case. The ATMega328 chip is mounted on the back of the board. I used a 28 pin DIL variant. The IC socket has its pins flattened out and soldered to the board like a SMD device. There is also a 5V SMD regulator and a 16 MHz SMD resonator.
I have included the Eagle files in case you want to get the board commercially made or do as I did and make it yourself. I used the Toner method.
After 3D printing the case, drill out the mount holes with a 2.5mm drill and create a thread with a 3mm tap. Use 6mm M3 screws to hold the board in place.
Making the switchesI have included an Eagle board layout which you can print using a laser printer onto a transparent label.
Cut the labels
Assemble the switch
Finally assembly the PCB and fix it to the front panel
Wire up the battery and switch
Use a FTDI programmer to program the ATMega328. If the ATMega328 isn’t pre-programmed with an Arduino UNO bootloader, you need to add one first.
This was an interesting programming exercise. The algorithm can be greatly improved. It only looks back one or two moves in the history table. The hardware could do with a speaker as well.
Comments