Please note that in the following discussion, the terms "secret code" and "answer" essentially mean the same thing, and so do the terms "hints" and "marks".


This programme has inherited the game rules of the classical Mastermind game, which was invented by an Israeli telecommunications expert, Mordecai Meirowitz. In this game, the Codemaker thinks of a colour string (the secret code) using any combination of colours in the Colour Panel that conforms to the particular game setting chosen. The Codebreaker will try to duplicate the exact colours and positions of the secret code. Each time the Codebreaker makes a guess, the Codemaker must give information ("hints") by displaying black and white balls (or boxes). A black ball (box) is displayed for every colour ball (box) which is the same colour and in exactly the same position as one of the colour balls (boxes) in the secret code. A white ball (box) is displayed for each colour ball (box) which is matched in colour but not position. When the Codebreaker duplicates the secret code, he wins the game. For example, if the code is "1231" and the Codebreaker's guess is "1134" (here the colours are represented by numbers), then the hints will be two black balls (boxes) and one white ball (box).


After you start the programme, you will see a Title Page. After a while (or after you click the mouse), the programme will go to the Background Screen. What follows is a detailed introduction to the various features of this screen.

Status Bar

At the bottom of the screen is the Status Bar, which is used for displaying useful messages to prompt the user to take some actions. When you are at a loss for what to do at any moment, read the message in the status bar.


From this menu, you can choose any one of the 1,424 different game settings. There are 9 list boxes in this menu, 7 of which ("Who guesses?, "Number of Colours", "Start from", "Length of Code", "Hints contain error(s)", "Order Type", "Colour repeated?") determine the game setting. The remaining two ("Display Mode", "Input Method") determine the display mode and input method of the game. The following is a description of the options available in each of these list boxes. However, please note that this programme has a VBSrcipt, a JavaScript and an ActionScript version. Not all the following features are supported by all three versions.

Who guesses?

There are 3 options in this list box: "You guess", "Computer guesses" and "Watch computer play". If the player chooses the first option, he / she will act as the Codebreaker and will have to break a code generated by the computer. If the player chooses the second option, he / she will act as the Codemaker. The computer will then become the Codebreaker to guess a code made by the player based on the hints given by the player. If the player chooses the third option, the computer will act as both the Codemaker and the Codebreaker (the computer will be "honest") and the player can then watch how the computer plays the game.

Number of Colours

This determines the number of different colours that the secret code may be drawn from, ranging from 5 to 10 colours. The specific colours that may actually be used will be shown in the COLOUR PANEL.

Start from

There are two options in this list box: Trial 1 and Middle. If the former option is chosen, the game will start right from the beginning, just like the usual Mastermind game. If the latter option is chosen, the game will start in the intermediate phase when some initial guesses have already been made. Under this option, if you are playing a You guess game or a Watch computer play game, the computer will generate a number of initial guesses together with the hints. You or the computer will then have to continue guessing based on the initial guesses and hints. On the other hand, if you are playing a Computer guesses game, you will then be prompted to input the trial number at which you want the computer to start guessing (between 2 and 9) as well as the initial guesses together with the hints. Under both cases, the initial guesses and the guesses made subsequently will be shown differently (by different brightness in the ActionScript and VBScript versions and by special symbols in the JavaScript version) in the RECORD Area.

Length of Code

This determines the length of the secret code. The exact length of code falls into two types: specified or unspecified. The former type ranges from "3" to "8" while the latter type ranges from "Unspecified (3 - 4)" to "Unspecified (3 - 8)". Unspecified code length means the secret code may contain blank spaces. But please notice blank spaces are not considered as one type of colours and are not counted when giving marks. For example, if the code is "bb345" and the Codebreaker's guess is "b304b", then the hints will be one black ball (box) and one white ball (box).

Hints contain error(s)?

There are two options in this list box: No error and May contain error(s). If you choose the former, all hints provided by the computer contain no errors, just like the usual game. If you choose the latter, some of the hints may contain error(s) and the game proceeds differently depending on whether you are playing the You guess game or the Watch computer play game.

In the former case, an Error button will appear after the first guess has been made. If you detect that the hints provided are inconsistent, you can press the Error button. The computer will then determine whether the hints given have really led to inconsistencies. If so, all the errors will be corrected and you can then continue guessing until you break the code. If the hints do not lead to inconsistencies or if you detect the inconsistencies too late, you will lose the game. Please notice this game setting is quite demanding of the player: not only do you need to detect the inconsistencies (if any), but you have to detect them "at the right time".

In the latter case, the computer will guess as usual. When it detects an error, a message box will spring up informing you and requesting you to press the OK button. After you press the button, the computer will correct all the error(s) automatically and then continue guessing based on the corrected hints.

Please notice the Computer guesses option and May contain error(s) option are a pair of "excluded combinations". That means you are not allowed to choose these options at the same time. In case you make error(s) when giving hints knowingly or unknowingly, the computer will sooner or later detect the inconsistencies among the hints and will not continue guessing. Amazed at the powerful A.I. of my programme?

Order Type

There are two Order Types: "Fixed order" and "Random order". Under the former game setting, the colours must be arranged according to the predefined order as shown in the Colour Panel, with the blank spaces placed at the end, while under the latter game setting, the colours (including blank spaces) may be arranged in any order.

Colour repeated?

This determines the number of repetitions that a particular colour may be repeated in the code and may range from "No repetition", "Each colour at most n times" to "May be repeated any times", with "n" being an integer between 2 and 7.

Display Mode

There are two display modes: "Colour" and "Number". (The ActionScript version supports one more display mode: "Both", which is a combination of the previous two.) Under the "Colour" mode, the computer will display all guesses and codes (answers) using colour balls (boxes). On the other hand, under the "Number" mode, each colour is represented by a digit between 0 and 9, while the blank space is represented by the letter "b". The correspondence between colours and digits is shown in the Colour Panel. This feature is not supported by the JavaScript version.

You may change the Display Mode at any stage of the game, or when the computer prompts you to change the mode at the end of a game.

Input Method

There are two input methods: "Mouse" and "Keyboard". Under the "Mouse" method, the player inputs his / her guesses / hints by using the mouse (dragging, double-clicking, clicking). Under the "Keyboard" method, the player inputs his / her guesses / hints by keying in numbers, using numbers and "b" to represent colours and blank spaces if appropriate. This feature is not supported by the JavaScript version.

Please note that this option only applies to the You guess and Computer guesses games. You may change the Input Method at any stage of the game.

Start Game

After choosing a game setting from the menu, you can start playing the game by pressing this button.

Start Demo

In addition to playing games, this programme also contains a demo function. After pressing this button, you may see how the computer plays the game with itself (without cheating!) endlessly under randomly chosen game settings. If you leave the programme idle at the Game Setting Menu, the demo function will be activated automatically in 5 minutes, displaying a certain kind of "screen saver" effect.

On/Off Sound Effects

This is a toggle command which enables you to switch on / off the background music as well as the various sound effects. This feature is only supported by the ActionScript Version.

On/Off Animation

This is a toggle command which enables you to switch on / off the background animation. This feature is only supported by the ActionScript Version.


This screen appears after you press the Start Game button or the Start Demo button. In addition to the Status Bar, On/Off Sound Effect Button, and On/Off Animation Button which also appear in this screen, there are some new features. What follows is an introduction to the features in this screen.


The lower half of the screen is the RECORD Area where the previous guesses and hints are shown for your reference.

Game Setting Frame

The upper right part of the screen is the Game Setting Frame which is a table showing the game setting as well as the Game Status of the particular game for your reference.

Colour Panel

The Colour Panel is located in the lower half of the upper left part of the screen. This panel shows you the specific colours that the secret code may be drawn from. The digit corresponding to each colour is also shown on each colour ball (box).

At the end of a game or during the whole course of a Watch computer play game or when Demo is on, the Colour Panel will appear as an Answer Frame or Possible Answer Frame showing the answer or possible answer of that particular game for your inspection.

Input / Message Area

The Input / Message Area is located in the upper half of the upper left part of the screen. This is the area where you input the information as requested by the computer or the computer shows you some information. The outlook of this area is different depending on the types and stages of the game. At times when the computer shows you the marks of a guess or when the computer requests you to input marks for its guess, the input area will occupy the whole upper half of the screen (and thus cover the Game Setting Frame and the Colour Panel .

Demo Speed Control Scrollbar / Radio Buttons

This appears in the Input / Message Area during a Demo session. It allows you to adjust the speed of the demo function. It appears as a scrollbar in the ActionScript and VB versions and appears as a set of radio buttons in the VBScript and JavaScript versions.

End Game Button

This command enables you to end the game at any stage of the game except when the computer is making a guess or a message box has appeared. Please note that if you are playing under the "You guess" setting and have already made at least one guess, the correct answer of that particular game will be shown in the Answer Frame after you choose this command.

End Demo Button

This command enables you to end the Demo session and return to the Background Screen.

Time Spent Frame

In a "You guess" or "Computer guesses" game, this frame provides the information of the time elapsed since the commencement of the game so that the player is aware of the time left. When the time limit is drawing near (i.e. 55 minutes after the commencement of the game), a warning message will appear to remind the player.


These refer to the various stages of a game. Here is a summary of the various Game Statuses that you may encounter.


This Game Status occurs most often and refers to the normal stage when the Codebreaker is still trying to break the code.


This only occurs in a Computer guesses game when the computer prompts you to think of a string for it to guess or when the computer prompts you to input the initial guesses and hints under the middle game setting.


This occurs when the Codebreaker breaks the code.

Error Detected

This occurs when the Codebreaker correctly detects an inconsistency among the given hints at the right time (i.e. not too early or late). If it is a Computer guesses game, the computer will not continue the game. Otherwise, the error(s) will be corrected and the Codebreaker may then continue playing.

Continue Playing

This occurs after an error has been detected and corrected and the Codebreaker continues playing the game.

End Game

This occurs when you end the game in an intermediate stage of the game.


In a You guess game, you are allowed to guess for a maximum of 18 times (including any initial guesses) within one hour. Failing to break the code within the maximum number of trials or the time limit will lead to Gameover. (There is no time limit for the VBScript and JavaScript versions.)

Hint(s) consistent

This only occurs in a You guess game under a May contain error(s) setting when you incorrectly determine that there is an error among the given hints but the given hints are in fact consistent. When this occurs, a possible answer (i.e. a string that is consistent with the given hints) will be shown in the Possible Answer Frame.

Late Detection of Error

This only occurs in a You guess game under a May contain error(s) setting when you detect the error too late. The computer will then tell you where the error(s) occur and when you should have detected the error(s).


Simulated Logical Reasoning

Mastermind is a purely logic game, which means no matter how complicated the game setting is, the secret code can always be broken just by logical reasoning. The code-breaking method employed in this programme is thus nothing more than a simulation of the logical reasoning a human employs in playing this game, i.e. after making the guesses, the programme makes assumptions about the code based on the given hints and adjusts the assumptions as the game proceeds and more hints are given, until the code is broken. If the given hints contain error, it can also detect it sooner or later. For example, suppose the first guess is "0123" which receives the marking of one black ball (box) and one white ball (box) (hereinafter denoted as [1,1]). Then the programme assumes that the code contains the digits "0" and "1" with "0" at the first position. It then takes "0415" as the second guess, which receives the marking of [0, 1]. Then it can be concluded that the above assumptions are wrong, and so the programme makes a new assumption that the code includes "0" and "2" with "2" at the third position. So it takes "6027" as the third guess, and so on.

The only difference between a human and the computer is only that the computer is concentrated and patient enough to follow the logical reasoning strictly without omissions or errors. According to my observation, even for the hardest game setting, the programme can break the code or detect any error within 16 trials. Apart from simulated logical reasoning, there are at least two other approaches that a code-breaking programme may take - the Naive Approach and the Optimal Strategies. I will next briefly discuss these two alternatives and explain why I have not adopted these two approaches in my programme.

Naive Approach

Under this approach, the programme searches from among all possible answers for the first one that is consistent with the given hints. The search either follows a predetermined order of the possible answers or generates a possible answer randomly and rejects all answers that are inconsistent with the given hints, until it finds the first consistent answer. If the search has reached the last answer in the sequence without finding a consistent answer, then it means the given hints are inconsistent. For example, suppose the first guess is "0000" with the marking [0,0], which means the code does not contain the digit "0", then during the search the programme will reject all answers that contain "0", until it comes to an answer that contains no "0", such as "1111", which then becomes the second guess.

While this approach is very simple and works well with the classical Mastermind game setting (No. of colours: 6; Length of code: 4) (hereinafter denoted as "the 6-4 setting") which has a total of 6 x 6 x 6 x 6 = 1,296 possible codes, it is absolutely inefficient (if not technically infeasible) for a more general game setting, such as a "10-Unspecified (3 - 8) setting", which has a total of 214,356,000 possible codes.

Optimal Strategies

Ever since the invention of the Mastermind game, there have been researches for optimal strategies that minimize the number of trials required for breaking a code. For example, Toby Nelson has provided in his webpage Investigations into the Master Mind Board Game a "Strategy Table" for breaking Mastermind code under the classical game setting (No. of colours: 6; Start from: Trial 1; Length of code: 4; Hints contain error(s)?: No error; Order Type: Random order; Colour repeated?: May be repeated any times). Using this Strategy Table, one can break any classical Mastermind code in at most 5 trials. Moreover, according to a research done by Kenji Koyama and Tony Lai (see An Optimal Mastermind Strategy from Journal of Recreational Mathematics, 1993), the best strategy uses an average of 4.340 trials to break the code (with one case needing 6 trials). In comparison, according to my observation, the average number of trials required to break the code under the same game setting is 4.97.

However, there is an important prerequisite for these optimal strategies, i.e. these strategies are based on a complete analysis of all possible codes and possible guesses in each trial and then select the guess that leads to quickest breaking of the code. After such an analysis, a Strategy Table can be drawn up. The programmer can then incorporate the whole Strategy Table into a computer programmer for breaking Mastermind code. A computer algorithm using this method does not simulate the process of logical reasoning, but it really "memorizes" a preprocessed Strategy Table.

Let me illustrate this with a very simple game setting - the "4-2 setting", which has a total of 4 x 4 = 16 possible codes. Here the 4 colours are represented by the digits 0, 1, 2, 3. Before the first guess is made, all the 16 codes are equally possible. Let us now examine what happens after the first guess is made and marked. For the purpose of analysis, the first guess falls into only 2 different types: "00" and "01". (All other guesses essentially lead to the same analysis, as one may check.) After the first guess is marked, the number of possible answers is greatly reduced. In the following I set out the possible answers left after the first guess is marked in a table:

First GuessMarkingPossible AnswersNo. of Possible Answers
11 12 13 21 22 23 31 32 33
01 02 03 10 20 30
22 23 32 33
20 30 12 13
00 02 03 11 21 31

We then compare the "worst case" for each of the two possible first guesses. From the above table, we can see that for the first guess of "00", if the marking is [0,0], there are 9 possible answers. This is the "worst case" for this first guess. For the first guess of "01", the worst case is 6 possible answers when the marking is [1,0]. Comparing the two worst cases, we can conclude that the first guess of "01" is preferable to "00". Thus the optimal strategy for the "4-2 setting" should include the instruction "The first guess is '01'". Having considered the first guess, we can then proceed to the second guess and do the same analysis as above. For example, if the marking for "01" is [1,0], then according to the above table, there are essentially only 2 different second guesses: "00" or "02". We can then list out all the possible markings together with the possible anwers corresponding to these markings and do the "worst case" comparison again. That's enough for this example.

The pitfall of this approach is similar to the Naive Approach. For example, for a "10-Unspecified (3 - 8) setting" with a total of 214,356,000 possible codes, this would mean tremendous amount of preprocessing time and tremendous amount of computer memory for just one game setting, not to say 1,424 different game settings.


The Naive Approach and the Optimal Strategies are similar to each other in that both rely on a certain kind of "global search" from among all possible codes. In comparison, the approach of my programme is based on initial assumptions and adjustment of assumptions about the code. When it adjusts the assumptions, it does not need to consider all possible codes. While the "global search" approaches are simpler for a programmer and works well under the classical "6-4 setting", it is very inefficient for the general setting. For this reason, my programme has adopted a more complicated and non-optimal yet successful code-breaking strategy.

Go to Mastermind Link Page.