Building Your Own Mastermind Game

A two-way approach to creating your own remake of the hit 70’s game

Image for post
Image for post

Gameplay

Mastermind is a two-sided code solving game played by a codemaker and a codebreaker. Using a series of pegs separated out into 8 colors, the codemaker chooses a pattern and conceals it in their gameplay area. The codebreaker then begins trying to break the code by inserting various pegs for each turn, hoping to break it within 10 chances. In turn, the codemaker responds with an output of smaller white or red pegs telling the codebreaker how close they are to winning. This is done like so: On the side of each turn, there’s space for 4 of these smaller pegs. In a basic ascending order, the red pegs confirm the correct placement of the larger pegs and the white ones confirm the right color, but in the wrong placement. There are various versions of the game that expands the gameplay and awards points for certain actions, but the basic gameplay is the same.

For a thorough breakdown of the gameplay behind Mastermind, please see this article here:

Console Version

For this project, python was implemented for quick development and use within both the console version and the GUI version. Firstly, for ease of readability, the console version was created using numbers instead of colors. For the base gameplay, game logic was created to import into the files. Each round a code is generated and the game can begin. Checks are made to determine if there are any possible errors and user generated input is in the proper format. Once the input is determined to be valid, the algorithm compares it with the master code and outputs the the response of how many items are correct and how many are in the right place. Once the code is cracked, the player is congratulated and challenged to play again. If the code isn’t cracked within 10 tries, the user is consoled and challenged to play again.

Image for post
Image for post
If statements to output console version’s messages

GUI Version

In the GUI version, things are just a little bit different. Because of the nature of things, this version is based in a grid and uses colors and a button to play. To play this version, utilize the command ./MasterMind.py to run the file from the directory inside your terminal. If you’re utilizing a virtual machine, you must have display port forwarding enabled to view the GUI and play the game. Upon the initialization of a new game, a code is created just like in the console version. The user then clicks in their desired colors in the current round and submits their guess via a button. Game logic is used to return whether or not the user is getting closer to cracking the code and whether they run out of turns or win, they are prompted to play again.

Image for post
Image for post
Function to reset the GUI version

Project Repository

Written by

Cosmetologist to Coder — Holberton School New Haven

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store