Download or view pegGame.frink in plain text format
// This Frink program analyzes the peg-jumping board game like the ones found
// at Cracker Barrel.
class PegGame
{
// This encodes the locations we can [jumpTo, over] from a given peg.
class var jumpTable = [[[3,1],[5,2]], // 0
[[6,3],[8,4]], // 1
[[7,4],[9,5]], // 2
[[0,1],[5,4],[10,6],[12,7]], // 3
[[11,7],[13,8]], // 4
[[0,2],[3,4],[12,8],[14,9]], // 5
[[1,3],[8,7]], // 6
[[2,4],[9,8]], // 7
[[1,4],[6,7]], // 8
[[2,5],[7,8]], // 9
[[3,6],[12,11]], // 10
[[4,7],[13,12]], // 11
[[3,7],[5,8],[10,11],[14,13]], // 12
[[4,8],[11,12]], // 13
[[5,9],[12,13]]] // 14
// Sets up a board for analysis
class analyzeBoard[pegs, trackMoves = false] :=
{
results = makeArray[[11],0]
if trackMoves == true
moves = new array
else
moves = undef
analyzeBoardRecursive[pegs, results, moves]
sum = sum[results]
for i = 1 to 10
{
r = results@i
if (r != 0)
println["$i\t$r\t" + padLeft[format[r/sum*100,1,4],7," "]]
}
}
class analyzeBoardRecursive[pegs, results, currentMove] :=
{
//println[pegs]
movesFound = 0
pegsLeft = 0
for from = 0 to 14
{
if (pegs@from == true)
{
pegsLeft = pegsLeft + 1
jumps = jumpTable@from
for [jumpTo, jumpOver] = jumps
if pegs@jumpTo == false and pegs@jumpOver == true
{
newBoard = pegs.shallowCopy[]
newBoard@from = false
newBoard@jumpTo = true
newBoard@jumpOver = false
movesFound = movesFound + 1
if currentMove != undef
{
newMoves = currentMove.shallowCopy[]
newMoves.push["$from-$jumpTo"]
}
analyzeBoardRecursive[newBoard, results, newMoves]
}
}
}
if (movesFound == 0)
{
//println[pegsLeft]
if pegsLeft == 1
{
print["remain:$pegsLeft\t"]
if (currentMove != undef)
print[join[" ",currentMove]]
println["\t$pegs"]
}
results@pegsLeft = results@pegsLeft + 1
}
}
}
pegs = makeArray[[15],true]
// Remove one peg
pegs@0 = false
println["\n(0 initially empty):"]
PegGame.analyzeBoard[pegs, true]
pegs = makeArray[[15],true]
// Remove one peg
pegs@1 = false
println["\n(1 initially empty):"]
PegGame.analyzeBoard[pegs, true]
pegs = makeArray[[15],true]
// Remove one peg
pegs@4 = false
println["\n(3 initially empty):"]
PegGame.analyzeBoard[pegs, true]
pegs = makeArray[[15],true]
// Remove one peg
pegs@3 = false
println["\n(4 initially empty):"]
PegGame.analyzeBoard[pegs, true]
Download or view pegGame.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 20145 days, 21 hours, 6 minutes ago.