I started playing board games when I was young, and I still loving them. Now, with my new additions of skills in Computer Science, I am able to build these board games by code. I am also interested in creating bots that can navigate the game itself and solve it.
The rule of Mastermind
If you already know it, you can skip for the next section This is the game called Mastermind, a type of logical game between 2 players. One player comes up with a sequence of colors (in this program a sequence of digits, from 1 to 9), and the other player tries to solve it by making guesses. After each guess, the player with the code provides information about how closely matched the guess is compared with the actual code (element-wise comparison).
There are three types of indicators: Green, which is an exact match of a digit (value and position in the code sequence); Yellow, which shows that digit exists in the code sequence; and Red, which shows that digit is not in the code sequence at all.
What it does
The game has two parts. One part is for you, the player, to guess the number randomly generated by the system. The other part is the AI, in which you provide the test and see how many steps the AI takes to solve it.
For the first part, the program randomly generates a sequence of 4 digits from 1 to 9. You then take turns to guess the digits. Whenever you press Enter, 1 turn counts. The results box shows how closely matched your answer is with the code sequence. The rules for the colors can be found in The rule of Mastermind above, in the second paragraph. Theoretically you have only 8 turns maximum to solve the digit.
For the second part, the program asks you for a sequence. Then an AI attempts to solve that sequence using the knowledge it learns after each guess. Its guesses are shown in the box on the right of the program, and the final guess should match with the code you provide in the first place.
How I built it
This project is very simple. The external modules needed to import for Python to run this program are only Numpy and Pygame.
The project has three main classes (or files). The first class is the Mastermind() class, which is the game objevt. The second class is the MastermindAI() class, which is the AI object used to solve the game given the Mastermind() class for it. Finally, the MainGUI() connects all the objects together to create the GUI for this project.
The concept behind the AI is also very simple. The first guess we make a random guess, and sees what the feedback is. The AI stores its knowledge in some variables. The self.known is a dictionary that maps the index of the sequence to its correct answer. The self.existed() is a set that records the possible values in the sequence. The self.cannot is a dictionary of sets that maps the index of the sequence to a set of values it cannot have. And the self.notexisted() is a set that contains the values the sequence is sure to not have. After each guess, these four variables are updated, and they are very robust that they can solve the sequence in usually 5 or 6 steps.
The motivation of the design of this AI is from the Minesweeper problem set, in the Knowledge lecture by CS50 Introduction to AI course on EdX.
Challenges I ran into
There are some obvious bugs in the program, like the digits get overriden if the user does not press Delete before, or the past guesses board does not have a scrolling function if there are more than nine guesses. I do not have time to cover everything, because I am still a beginner in code, and I work alone on this project.
Accomplishments that I'm proud of
Of course my proudest is the work being able to run! This project is far from the much more complicated programs or websites or AI systems out there. But it is beginner friendly, yet it covers many important Python concepts, like event handling, inheritance, management of objects and data structures, ...
What I learned
This project allows me to practice using Pygame one more time, as well as giving me the time constraints to finish the project.
What's next for Mastermind game
There are several ways to expand this game, such as allowing a user to define the range of digits (or numbers) for each element in the sequence, and the length of the sequence itself. Running the code for many randomly generated sequences, and providing plots to analyze the AI's performance is also very interesting. I can also try improving the bot by building optimalization code for it. Then finally, to publish it, I can build a simple website from Flask, then use the networking module in Python to build a Mastermind website that allows the players to log in and play each other.