Darkness 2001-09-14 00:00:00 UTC
Twilight 2001-09-15 00:00:00 UTC
Daylight 2001-09-16 00:00:00 UTC
Finish 2001-09-21 00:00:00 UTC

Mastermind - Rules

Arrow_down  Download Sample Code

Small mastermind image

The Challenge

This contest is a variation on the classic Mastermind® game. Each puzzle is an unknown sequence of colored pegs and the object is to guess which pegs they are. On each round of the game, you make a guess at the puzzle and receive feedback in the form of black and white pegs to let you know how many of your pegs are correct. To get a feel for the game, click here for a java implementations that you can play on the web.

Playing the Contest

Download and unzip these files to get started: mastermind.zip. Most notable of these files is "solver", the file that contains the actual contest entry that you write, and "runcontest", that you can run to test your entry. More details on these functions follow.

A row vector of positive integers represents the sequence of pegs, e.g. [4 2 4 7 3]. Each puzzle can have any number of pegs (length of vector) and any number of possible colors (maximum integer). Your submission returns its best guess for this sequence. It earns a black peg for each peg that is exactly right and a white peg for each peg that is in the solution, but in the wrong position (for scoring purposes, a black peg is worth twice as much as a white peg). For example:

[4 2 4 7 3]
[4 3 4 2 4]
Black pegs: 2 (for the second and third pegs in the guess)
White pegs: 2 (for the second and fourth pegs in the guess, which match the fifth and second pegs of the sulution but are in the wrong place).

Before returning its final answer, your submission can make a limited number of guesses to narrow down the possibilities. This corresponds to a row in the board game. It makes the guess by calling scoreme, which has this signature:

[black, white, numCallsMade]=scoreme(guess,puzzleID);

Your entry can only call scoreme me a limited number of times, and this limit is not the same for each puzzle.

Your submission needs to have the following signature:


It receives:

the number of pegs in the puzzle, i.e. the length of the vector.
the number of colors, i.e. the maximum integer in the vector.
the maximum number times your entry can call scoreme.

And must return:

your submission's best guess.

Each entry must complete the entire test suite in less than two minutes, or it will fail. The faster it completes the suite, the lower (better) the score.

Developing your entry

Let's take a closer look at the example included in the zipfile mentioned at the beginning of this page. If you download and uncompress the zipfile mentioned earlier, you will have the files used below.

function finalAnswer=solver(numPegs,numColors,guessLimit,puzzleID)
% sample entry

while numCallsMade < guessLimit
  [black, white, numCallsMade]=scoreme(guess,puzzleID);
  if score > bestScore
    if black == numPegs
        % perfect score, so return

A few points to keep in mind about this function and your contest entries:

  • The function has the proper signature
    function finalAnswer=solver(numPegs,numColors,guessLimit,puzzleID)
  • The function makes calls to the scoreme function to determine how good its current guess is, passing the guess and puzzleID to the scoreme function.
  • The function exits after it has completed guessing, and returns only the best guess that it found.

Internally, we keep track of how many times you've called scoreme to make sure that you don't use too many guesses. But we're only interested in your final answer, and that's what your function should return.

To test this funciton using the testsuite we've included in the zipfile, at the MATLAB prompt you can all the "runcontest" function:

 >> [results, message] = runcontest

The first column of results will contain your "pegscore", that's 2*numBlackPegs + numWhitePegs, and the second column will give you an estimate of the runtime. message will contain a string describing the overall statistics of your submission.

It's also useful to visualize what you entry is doing; you can do this by passing runsuite an input of 1:

 >> [results, message] = runcontest(1)

After you entry has completed each test in the testsuite, a graphical display of the Mastermind board is shown, with the correct answer at the top and your guesses listed below (your first guess is at the bottom).


Here are the factors, in order of importance, that affect your score:

  1. Number of black pegs
  2. Number of white pegs pegs
  3. Runtime of your code
  4. Number of times you've called scoreme

This list indicates relative weighting; it's possible that very strong performance in a less important area can make up for slightly weaker performance in a more important area.

Good luck!

Collaboration and editing existing entries

Once an entry has been submitted, it cannot be changed. However, any entry can be viewed, edited, and resubmitted as a new entry. You are free to view and modify any entries in the queue. The contest server maintains a history for each modified entry. If your modification of an existing entry improves its score, then you are the "author" for the purpose of determining the winners of this contest. We encourage you to examine and optimize existing entries.

We also encourage you to discuss your solutions and strategies with others. We have provided a ink to an online chat room so that you can chat about the contest and see who else is watching. You can also post to a comp.soft-sys.matlab thread that we've started from our newsreader.

Fine Print

The allowable functions are those contained in the basic MATLAB package available in $MATLAB/toolbox/matlab, where $MATLAB is the root MATLAB directory. Functions from other toolboxes will not be available. Entries will be tested against MATLAB version 6.1 (R12.1).

The following are prohibited:

    Java commands or object creation
    eval, feval, etc.
    Shell escape such as !, dos, unix
    Handle Graphics commands
    ActiveX commands
    File I/O commands
    Debugging commands
    Printing commands
    Simulink commands
    Benchmark commands such as tic, toc, and flops


Check our FAQ for answers to frequently asked questions about the contest.

Mastermind is a registered trademark of Pressman Toy Corporation.

About named visibility periods

Contests are divided into segments where some or all of the scores and code may be hidden for some users. Here are the segments for this contest:

  • Darkness - You can't see the code or scores for any of the entries.
  • Twilight - You can see scores but no code.
  • Daylight - You can see scores and code for all entries.
  • Finish - Contest end time.