Pig the dice game/Player: Difference between revisions

Content added Content deleted
Line 2,016: Line 2,016:
strategy::Pair
strategy::Pair
end
end

randomchoicetostop(player, group) = rand(Bool)
randomchoicetostop(player, group) = rand(Bool)
variablerandtostop(player, group) = any(x -> x.score > player.score, group) ? rand() < 0.1 : rand(Bool)
variablerandtostop(player, group) = any(x -> x.score > player.score, group) ? rand() < 0.1 : rand(Bool)
overtwentystop(player, group) = player.ante > 20
overtwentystop(player, group) = player.ante > 20
over20unlesslosingstop(player, group) = player.ante > 20 && all(x -> x.score < 80, group)
over20unlesslosingstop(player, group) = player.ante > 20 && all(x -> x.score < 80, group)

const strategies = ("random choice to stop" => randomchoicetostop, "variable rand to stop" => variablerandtostop,
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)
"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 players = [Player(0, 0, 0, 0, s) for s in strategies]
const dice = collect(1:6)
const dice = collect(1:6)

function turn(player)
function turn(player, verbose=false)
playernum = findfirst(p -> p == player, players)
scorewin(p) = for p in players if p == player p.wins += 1 else p.losses += 1 end; p.score = 0 end
scorewin(p) = for p in players if p == player p.wins += 1 else p.losses += 1 end; p.score = 0 end
player.ante = 0
player.ante = 0
while (r = rand(dice)) != 1
while (r = rand(dice)) != 1
player.ante += r
player.ante += r
verbose && println("Player $playernum rolls a $r.")
if player.score + player.ante >= 100
if player.score + player.ante >= 100
scorewin(player)
scorewin(player)
verbose && println("Player $playernum wins.\n")
return false
return false
elseif player.strategy[2](player, players)
elseif player.strategy[2](player, players)
verbose && println("Player $playernum holds and has a new score of $(player.score).")
player.score += player.ante
player.score += player.ante
break
break
end
end
end
end
verbose && println("Player $playernum rolls a 1, so turn is over.")
true
true
end
end

function rungames(N)
function rungames(N)
for i in 1:N
for i in 1:N
verbose = (i == 3) ? true : false # do verbose if it's game number 3
curplayer = rand(collect(1:length(players)))
curplayer = rand(collect(1:length(players)))
while turn(players[curplayer])
while turn(players[curplayer], verbose)
curplayer += 1
curplayer += 1
if curplayer > length(players)
if curplayer > length(players)
Line 2,060: Line 2,066:
end
end
end
end

rungames(1000000)
rungames(1000000)
</lang>{{out}}
</lang>{{out}}
<pre>
<pre>
Player 2 rolls a 6.
Player 2 holds and has a new score of 0.
Player 2 rolls a 1, so turn is over.
Player 3 rolls a 1, so turn is over.
Player 4 rolls a 5.
Player 4 rolls a 1, so turn is over.
Player 1 rolls a 2.
Player 1 rolls a 3.
Player 1 rolls a 4.
Player 1 rolls a 4.
Player 1 rolls a 6.
Player 1 holds and has a new score of 0.
Player 1 rolls a 1, so turn is over.
Player 2 rolls a 4.
Player 2 rolls a 3.
Player 2 rolls a 2.
Player 2 rolls a 5.
Player 2 rolls a 2.
Player 2 rolls a 2.
Player 2 rolls a 6.
Player 2 rolls a 4.
Player 2 rolls a 4.
Player 2 holds and has a new score of 6.
Player 2 rolls a 1, so turn is over.
Player 3 rolls a 1, so turn is over.
Player 4 rolls a 2.
Player 4 rolls a 2.
Player 4 rolls a 4.
Player 4 rolls a 1, so turn is over.
Player 1 rolls a 4.
Player 1 rolls a 4.
Player 1 rolls a 5.
Player 1 rolls a 3.
Player 1 holds and has a new score of 19.
Player 1 rolls a 1, so turn is over.
Player 2 rolls a 4.
Player 2 rolls a 5.
Player 2 rolls a 2.
Player 2 rolls a 4.
Player 2 holds and has a new score of 38.
Player 2 rolls a 1, so turn is over.
Player 3 rolls a 2.
Player 3 rolls a 4.
Player 3 rolls a 1, so turn is over.
Player 4 rolls a 6.
Player 4 rolls a 6.
Player 4 rolls a 4.
Player 4 rolls a 3.
Player 4 rolls a 5.
Player 4 holds and has a new score of 0.
Player 4 rolls a 1, so turn is over.
Player 1 rolls a 3.
Player 1 rolls a 3.
Player 1 holds and has a new score of 35.
Player 1 rolls a 1, so turn is over.
Player 2 rolls a 5.
Player 2 holds and has a new score of 53.
Player 2 rolls a 1, so turn is over.
Player 3 rolls a 6.
Player 3 rolls a 3.
Player 3 rolls a 5.
Player 3 rolls a 1, so turn is over.
Player 4 rolls a 6.
Player 4 rolls a 6.
Player 4 rolls a 3.
Player 4 rolls a 3.
Player 4 rolls a 3.
Player 4 holds and has a new score of 24.
Player 4 rolls a 1, so turn is over.
Player 1 rolls a 4.
Player 1 holds and has a new score of 41.
Player 1 rolls a 1, so turn is over.
Player 2 rolls a 6.
Player 2 holds and has a new score of 58.
Player 2 rolls a 1, so turn is over.
Player 3 rolls a 5.
Player 3 rolls a 4.
Player 3 rolls a 5.
Player 3 rolls a 2.
Player 3 rolls a 4.
Player 3 rolls a 1, so turn is over.
Player 4 rolls a 6.
Player 4 rolls a 5.
Player 4 rolls a 4.
Player 4 rolls a 3.
Player 4 rolls a 3.
Player 4 holds and has a new score of 45.
Player 4 rolls a 1, so turn is over.
Player 1 rolls a 4.
Player 1 rolls a 2.
Player 1 holds and has a new score of 45.
Player 1 rolls a 1, so turn is over.
Player 2 rolls a 2.
Player 2 rolls a 5.
Player 2 rolls a 5.
Player 2 rolls a 4.
Player 2 rolls a 2.
Player 2 rolls a 4.
Player 2 rolls a 6.
Player 2 rolls a 2.
Player 2 rolls a 6.
Player 2 wins.

Strategy % of wins (N = 1000000)
Strategy % of wins (N = 1000000)
------------------------------------------------------------
------------------------------------------------------------
roll to 20 then if not losing stop 45.7
roll to 20 then if not losing stop 45.8
roll to 20 36.9
roll to 20 36.8
variable rand to stop 16.0
variable rand to stop 15.9
random choice to stop 1.4
random choice to stop 1.4
</pre>
</pre>