Virtual remake of the Mastermind board game and design/implementation of a solving algorithm.

2013-12-01

Python

MySQL

tkinter

Description

Mastermind is a two player code-breaking game. The task was to implement the game in Python, allowing two game modes: 'human vs computer' whereby the human is
the codebreaker and 'computer vs computer' where the computer performs an algorithm to break a generated code. I made use of the TkInter module for the GUI and implemented an
algorithm that I devised. My algorithm works as follows:

Take the first colour (e.g. red) and fill all pegs in first row with it

If result has any blacks then we have that many of that colour in the real pattern (e.g. 2 blacks in result means there are 2 reds in the code)

If result was empty then do step 1 with the next colour, otherwise go to step 4

Put that number of that colour into the next row (e.g. place 2 reds)

Fill the rest of spaces in that row with the next colour

If the number blacks and whites in the result increased then we know that the increase is however many of this new colour

If the number of blacks and whites increased then we know there is none of this new colour

If the number of blacks and whites doesn't increase from row to row then it means none of the new colour used is in the real pattern

Repeat the above steps with the next colours until we reach a point where the number of blacks and whites in the result = the total number of pegs

This means we've found which colour and how many of each colour is in the real pattern

Make random patterns of this collection of colours until we find the right one.