mastermind.frink

Download or view mastermind.frink in plain text format


/** This program plays the game Mastermind.  Its algorithm is very simple:

   1.) Make a list of all possible plays.

   2.) Choose one play at random and play it.

   3.) Using the feedback your opponent gives you, eliminate all of the
       plays from the list that can't match that feedback.  Repeat until done.

   This is intended to be used interactively, but if you just want to watch it
   play, it will automatically pick a random secret pattern for you and
   play for you.  Just hit the "enter" key every time and the computer will
   provide the correct defaults for you.

   For another implementation using the Deducer framework in Deducer.frink,
   see mastermindDeducer.frink .
*/


// The possible colors.  You can change these to whatever you want, including
// full names.  You can also use more or fewer colors and everything will
// just work right.
colors=["G","B","Y","P","O","R"]

// You can change from the normal 4-peg game to more or fewer pegs.
numPegs = 4

// Create an array with all possible plays.
opts = new array
multifor x = makeArray[[numPegs], colors]
   opts.push[x]

//println[formatTable[opts]]

// Pick a target play at random.
target = random[opts]    
println["Suggest:" + target]       

// This is a function that will be passed to "select".  It just returns true
// for each move that matches the specified result pegs.
sfunc =  {|move, data|
          [target, result] = data
          rank[move, target] == result
         }

// The main loop.
do
{
   println["Possible solutions remaining: " + length[opts]]
   move = opts.removeRandom[]         // Choose a move at random
   // move = integerDigits[eval[input["Enter move."]]] // Let the user choose
   [black,white] = rank[move,target]  // Get default results for user.
   result = eval[input["Move is $move",[["Black: ", black], ["White: ", white]]]]

   // Now just go through the remaining possible solutions and return only
   // the ones that could have matching results.
   opts = array[select[opts,
                       sfunc,
                       [move, result]]]

} while result@0 != numPegs and length[opts] > 1

if result@0 == numPegs    // All black?  We guessed right last time!
   println["Guessed the solution correctly!"]
else                      // Otherwise, we know what the solution will be.
   println["Solution is " + opts@0]



/** This function determines how good a move was at matching the specified
    target.  It returns the number of black and white pegs as the array
    [black, white]
*/

rank[move, target] :=
{
   black = 0
   white = 0
   targetCopy = target.shallowCopy[]
   
   // First, count total number of matches in any position.  As a match is
   // found in any position, remove it from the list so it's not counted twice.
   for mpiece = move
      if targetCopy.removeValue[mpiece]  // Returns true if a piece was removed
         white = white + 1

   // Now count pieces in the correct positions.  For each one found, remove
   // one from the "white" count and add one to the "black" count.
   for i = 0 to length[target]-1
      if move@i == target@i
      {
         white = white - 1
         black = black + 1
      }

   return [black,white]
}



Download or view mastermind.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 19945 days, 0 hours, 0 minutes ago.