Hack a Day: MasterMind Challenge
As a representation of the Medellin campus and the only team, we wanted to face this challenge using C language. To remember our first steps in software development at Holberton School. C is fun!
- David Arias Fuentes
- Valentina Jaramilo Ramirez
Do you remember that old game of two players where one wrote down a short sequence of numbers or colors, and the other player had to try and guess it? Well, it was a bit more than that, but the idea might have a ring to it.
What is a Mastermind game?
The old game I’m referring to is the Mastermind game. The Mastermind game is a simple code-breaking board game for two players (called the codemaker and the codebreaker), and it was invented in 1970 by Mordecai Meirowitz.
The game consists in that the codemaker secretly creates a code consisting of four colors or numbers from a specific set, such as red, yellow, green, blue, purple, and orange for one or from 1 to 9 for the other. Then, the codebreaker tries to guess the exact position of the sequence in the secret code. For each guess that the codebreaker makes, the codemaker gives feedback with white and black pegs that will work as hints or clues for the codebreaker. For example, the number of black hits will indicate the number of colors or numbers that are right and are in the correct position, and the number of white clues will indicate the number of colors or numbers that are correct but are in the wrong place.
How to write a Mastermind in C?
Now that we know what a Mastermind game is, let’s get our hands into the matter.
To begin, determine the players of the game: the computer as the codemaker and the user as the codebreaker. For now, the codebreaker will have only eight chances to guess the right secret code, if the codebreaker uses all the guess opportunities, then the user will lose, and a message will appear: “try again!”. The user can choose to keep playing or end the game at any moment, using CTRL + D.
When executing the file, the game will automatically start, giving a welcome and a short set of instructions for the user. After a hit of the “enter” key.
The game will start, and a function to set up the “board” of the game will execute.
At this same point, the secret code of the codemaker will be given by a function that will generate a set of four unique numbers. The user will have to guess this secret code by typing in the command line of the terminal a sequence of numbers from 1 to 6.
The guess of the codebreaker will be taken from the command line, but the guess only will be valid if there are exactly four numbers. In case there isn’t, a message requiring the correct input will be showed. On the other hand, a valid input will be processed with a comparison between the codebreaker guess and the secret code.
Inside this comparison process, we were careful to avoid memory leaks in the program. The numbers that are found to be the correct value and into the right position will increase a black asserts counter. Meanwhile, the numbers with the appropriate value but not in the correct position will increase the white asserts counter. Also, keep in mind to have a counter for the number of valid guess chances for the user. In this particular case, the user has only eight opportunities.
The black asserts and the white asserts are the ones to give the hints to the user by filling the rows in the board game with X’s for black asserts, and O’s for white asserts.
Finally, and as mentioned before, the user will win if guess correctly the secret code, and a congrats message will show.
The user will lose when there are no more chances to guess, and a message of “try again!” will display along with the reveal of the secret code value. There the user can choose to keep playing by hitting the enter key or exit the game by pressing CTRL + D.
That’s was how you write a Mastermind game in C language. Thank you so much!