Pig the dice game/Player: Difference between revisions

m
Line 2,016:
strategy::Pair
end
 
randomchoicetostop(player, group) = rand(Bool)
variablerandtostop(player, group) = any(x -> x.score > player.score, group) ? rand() < 0.1 : rand(Bool)
overtwentystop(player, group) = player.ante > 20
over20unlesslosingstop(player, group) = player.ante > 20 && all(x -> x.score < 80, group)
 
const strategies = ("random choice to stop" => randomchoicetostop, "variable rand to stop" => variablerandtostop,
"roll to 20" => overtwentystop, "roll to 20 then if not losing stop" => over20unlesslosingstop)
const players = [Player(0, 0, 0, 0, s) for s in strategies]
const dice = collect(1:6)
 
function turn(player, verbose=false)
playernum = findfirst(p -> p == player, players)
Line 2,039:
return false
elseif player.strategy[2](player, players)
verbose && println("Player $playernum holds and has a new score of $(player.score).")
player.score += player.ante
verbose && println("Player $playernum holds and has a new score of $(player.score).")
breakreturn true
end
end
Line 2,047:
true
end
 
function rungames(N)
for i in 1:N
Line 2,066:
end
end
 
rungames(1000000)
</lang>{{out}}
4,105

edits