View or download BullsAndCows.frink in plain text format
/** This program plays the game "Bulls and Cows".
https://en.wikipedia.org/wiki/Bulls_and_Cows
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.
*/
// The possible digits. You can change these to whatever you want, including
// full names. You can also use more or fewer digits and everything will
// just work right.
numbers=array[1 to 9]
// You can change from the normal 4-digit game to more or fewer digits.
numDigits = 4
// Create an array with all possible plays.
opts = new array
for comb = numbers.combinations[numDigits]
for perm = comb.permute[]
opts.push[perm]
// 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 digits.
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
[bulls,cows] = rank[move,target] // Get default results for user.
result = eval[input["Move is $move",[["Bulls: ", bulls], ["Cows: ", cows]]]]
// 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 != numDigits and length[opts] > 1
if result@0 == numDigits // All bulls? 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 bulls and cows as the array
[bulls, cows]
*/
rank[move, target] :=
{
bulls = 0
cows = 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
cows = cows + 1
// Now count pieces in the correct positions. For each one found, remove
// one from the "cows" count and add one to the "bulls" count.
for i = 0 to length[target]-1
if move@i == target@i
{
cows = cows - 1
bulls = bulls + 1
}
return [bulls,cows]
}
View or download BullsAndCows.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 18863 days, 3 hours, 27 minutes ago.