Mastermind Project

In EDUvision Season 3 Episode 5 we demoed a Mastermind project that can be used in an escape room setting. Mastermind is a code-breaking game for two players or one player against the computer. The game was invented in 1970 by Mordecai Meirowitz.

The idea of the game

  1. In the Mastermind, the “code maker” player chooses a pattern of four different colored LEDs. In our case, it is decided in the code.

  2. The “codebreaker” player tries to guess this pattern, both which colors are used and in what order. Each guess is made by adding four colors to the row.

  3. The Arduino board then gives feedback to the codebreaker using the lights next to the guessed pattern:

    • A red light indicates that there is one color that is right and on its right place, but we don’t know which one.
    • A white light indicates that there is one color that is right but in the wrong place, but we don’t know which one.
    • No light indicates that one of the colors is not included in the pattern.
  4. After the feedback, the codebreaker continues and adds a new guess. Remember to repeat all the colors that were correct. If the codebreaker figures out the pattern before or by the last row they win. Otherwise, the code maker wins.

Materials needed

  • Arduino MKR WiFi 1010 (or almost any of the Arduino boards)
  • OLED screen (optional)
  • Rotary encoder
  • Neopixel matri or LEDs
  • Breadboard
  • Micro USB cable
  • 3D-Printer filament and 3D-Printer (optional)
  • Cardboard

Soldering a rotary encoder

Code

This project utilizes a custom library which enables us to shorthand code in the .ino file. For the code to run properly you need a NeoPixel matrix and a rotary encoder, the OLED display is optional. The complete code including libraries can be found on GitHub:

Example sketch

#include "Mastermino.h"
Mastermino mastermino;

//Include the tabs
//#include "matrix.h"
#include "input.h"

#include "bitmaps.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// Code for OLED display
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT);

#define LOGO_HEIGHT   16
#define LOGO_WIDTH    16

void setup() {
  Serial.begin(9600);
  //while (!Serial);

  Serial.println("INIT");
  matrix.begin();
  matrix.clear();
  matrix.show();

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.drawBitmap(0, 0, logo, 128, 64 , 1);
  display.display();
  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);
  delay(3500);
  display.clearDisplay();
  display.setCursor(0, 15);
  display.print("Press the knob to     start");
  display.display();
  //while(1);
  while (digitalRead(7))delay(100);
  display.clearDisplay();
  display.setCursor(0, 15);
  display.println("Rotate the knob to");
  display.println("select a color");
  display.display();

  blinkMatrix(COLOR_CYAN);
  mastermino.start();

  //The code maker sets the color pattern here. Colors are set by defining "COLOR_GREEN" for example.
  mastermino.masterCode.dot[0].color = COLOR_;
  mastermino.masterCode.dot[1].color = COLOR_;
  mastermino.masterCode.dot[2].color = COLOR_;
  mastermino.masterCode.dot[3].color = COLOR_;
  display.clearDisplay();
  display.setCursor(0, 18);
  display.print("Playing");
  display.display();
}

void loop() {
  int status = mastermino.checkWin();
  if (status == PLAYER_PLAYING) {
    encoderUpdate();
  } else  {
    mastermino.finishGuess();
    mastermino.getChecks();

    updateMatrix();

    if (status == PLAYER_LOST) {
      blinkMatrix(COLOR_RED);
      display.clearDisplay();
      display.setCursor(0, 18);
      display.println("YOU LOST!");
      display.display();
    } else {
      blinkMatrix(COLOR_GREEN);
      display.clearDisplay();
      display.setCursor(0, 18);
      display.print("The code is:");
      display.println(""); // Set your desired secret code to be displayed here.
      display.display();
    }

    delay(1500);
    display.println();
    display.print("Press to RESTART");
    display.display();
    while (digitalRead(7));
    //Restart the game
    mastermino = Mastermino();
    setup();
  }
}

Test your project

Upload the code to your board. The display will guide you: “Press the knob to start a new game”. You can also choose to play the game without a display. In the beginning of the game, after pressing the knob you will see all the lights blinking four times.

Now the display should say: ”Playing”. Turn the knob to decide the color of the first light. Once you found the preferred color press the knob to pick that color. Then choose the color of the second light and so forth until you have picked a color for all the four lights on that row. After that the computer gives you feedback: A red light indicates that there is one color that is right and on its right place, but we don’t know which one. A white light indicates that there is one color that is right but in the wrong place, but we don’t know which one. A turned off light indicates that one of the colors is not included in the pattern.

If you get the pattern right the lights will blink green and the display will say: “You won”. If you don’t guess the pattern before the last row the lights will blink red and the display tells you “You lost”. You can start a new game by pressing the knob.

Watch the presentation of the project.