Skip to content
Physics and Astronomy
Home Our Teaching Resources C programming mastermind.html
Back to top

Mastermind

"Mastermind" is a code-breaking game involving coloured pegs inserted into rows of four holes. For our purposes we may imagine the pegs to be Red, Orange, Yellow, Green, Blue and Purple (or Indigo or Violet!).

Initially the codemaker puts one peg into each of each of the row of four holes that is hidden from the other player, for example:


RGBB

The other player, the codebreaker has to work out the pattern by successively guessing what it is. For each guess, if they guess the correct colour in the correct location they are awarded a black peg, if they guess the correct colour but in the wrong location they are awarded a white peg. All they are told is the total number of pegs (not which hole they refer to) and no peg may count twice. For example:


PegsScore
TargetRGBBBW
GuessOGGR11

In this example the left-hand Green peg in the guess scores a black as it is in the correct location and the Red guessed peg scores a white as it is in the wrong location. The right-hand Green peg does not count as the Green peg in the target has already been counted for the black peg. The Orange peg also does not count as there is no Orange peg in the target.

Your task

Your task is to wtrite a program to play the part of the codesetter. It must create a target pattern and allow the codebreaker to enter guesses such as "ROYG" (single letters are fine). It should then print out the guess and the score roughly in the manner of the "Guess" row above. For the second and subsequent guess it should print out a summary of all of the guesses, one per row.

When the code breaker guesses the target it should print out the number of guesses taken.

                                                                                                                                                                                                                                                                       

Validate   Link-check © Copyright & disclaimer Privacy & cookies Share
Back to top