Rather than jumping head first into coding our Mastermind game in Atmel (see “Smoketown USA – The Big Picture” blog post for overview of the game), we thought it would be a better idea for us to get a version of the game working with just code written in C – using the Dev-C++ Bloodshed Software – and then move forward from there. We started by having the program generate four random numbers between 1 and 6. This would be stored in an array and act as the master code. Then the program would prompt the user to enter four numbers ranging from 1 to 6, and store that in a second array. We could then compare each individual element of the input array with all elements of the master array and then tell the user the necessary information about their guess.
Once we had a solid program that worked just from running it in the terminal, we then moved on to begin integrating it into Atmel with our other components. The first component we decided to tackle was setting up the information for the output pins to drive the LEDs. To do this, we used the IOPORT_CREATE_PIN( ) function in order to define a name for each output pin, as shown below:
Once each output pin was assigned an LED and a color, we could then move on to writing the logic that would turn on each output. To do this, we basically stored a truth table inside a 2D 15×15 array. Each row of the truth table is made up of five groups of three numbers, as shown in the image below. The first number of a group indicates red, the second number of a group indicates green, and the third indicates blue. The first group of three numbers in each row indicate how many of each color LED there: for example, in the first row it shows 4 red LEDs. The next four groups of three indicate the color that each of the four LEDs need to be. So in the first row, all four groups of three have a one in the first position to indicate that all four of the LEDs should be red.
The truth table was mainly used for comparing with the information gathered from the user’s input. Once we calculated the number of digits that were exact (correct digit and correct location) and the number of digits that were close (correct digit and incorrect location) we could then search through the truth table using a few for-loops and if-statements in order to set each of the output pins for the correct LED and color.