david@drawbycode.com
Mastermind
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:

    1. Take the first colour (e.g. red) and fill all pegs in first row with it
    2. 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)
    3. If result was empty then do step 1 with the next colour, otherwise go to step 4
    4. Put that number of that colour into the next row (e.g. place 2 reds)
    5. Fill the rest of spaces in that row with the next colour
    6. If the number blacks and whites in the result increased then we know that the increase is however many of this new colour
    7. If the number of blacks and whites increased then we know there is none of this new colour
    8. 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
    9. 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
    10. This means we've found which colour and how many of each colour is in the real pattern
    11. Make random patterns of this collection of colours until we find the right one.

    Playing the game manually
    The bot successfully winning a game