Master Mind, Part 1

November 17, 2009

Master Mind is a two-player game of deductive logic. One player, the setter, selects a four-symbol code, and the other player, the solver, tries to identify the code by trying test patterns, probes, to which the setter responds with the number of black hits, indicating the number of positions where the code symbol and probe symbol are identical, and the number of white hits, where a probe has the right symbol in the wrong position. Setter and solver change roles after each puzzle is solved, for a pre-defined number of rounds, and the winner is the player who has solved the puzzles with the least number of probes. With six symbols and four pegs there are 64 = 1296 possible codes. The physical game uses colored pegs for the symbols; we will use digits instead. Variants of the game increase the number of symbols and/or the length of the code; a variant with five pegs using eight colors is marketed under the name Super Master Mind. Here is a sample game:

1 1 2 2    B
1 3 4 4    W
3 5 2 6    BWW
1 4 6 2    BW
3 6 3 2    BBBB

The solver first probes with the pattern 1 1 2 2, which has a single black hit. The second probe, 1 3 4 4, receives a single white hit. The third probe, 3 5 2 6, earns a single black hit and two white hits. The fourth probe, 1 4 6 2, receives one black hit and one white hit. The fifth probe, 3 6 3 2, solves the puzzle with four black hits.

Your task is to write a program that performs the role of the setter, selecting a random code, prompting for probes, and scoring each probe until the human solver who is playing the game solves the puzzle. In the next exercise you will be asked to write a solver. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

4 Responses to “Master Mind, Part 1”

  1. CodePlea said

    Here’s my first ever TCL program: http://codepad.org/mzkVDiY6

    It was an interesting exercise.

  2. […] Remco Niemeijer …aaaand we’re back. Sorry for the delay, life can get really busy. In yesterday’s Programming Praxis problem we have to create a program that will answer guesses for the game Master Mind. Let’s get to […]

  3. Remco Niemeijer said

    My Haskell solution (see http://bonsaicode.wordpress.com/2009/11/17/programming-praxis-master-mind-part-1/ for a version with comments):

    import Data.List
    import System.Random
    
    match :: [Int] -> [Int] -> String
    match code guess = concat $ zipWith replicate [bs, ws, ds] "BW."
        where bs = length . filter id $ zipWith (==) code guess
              ds = length (foldr delete code guess)
              ws = length code - bs - ds
    
    go :: [Int] -> IO ()
    go code = do hits <- fmap (match code) readLn
                 if all (== 'B') hits then putStrLn "You Win!"
                    else putStr (hits ++ "\nTry again: ") >> go code
    
    play :: Maybe [Int] -> IO ()
    play code = do putStr "Enter your guess as a list: "
                   rnd <- fmap (take 4 . randomRs (1, 8)) getStdGen
                   go $ maybe rnd id code
    
  4. […] from: Master Mind, Part 1 « Programming Praxis By admin | category: mind | tags: carrey, clementine, girlfriend, mind, other-player, […]

Leave a comment