Jump to content

Pig the dice game/Player: Difference between revisions

m (→‎{{header|Perl}}: un-comment print statement)
Line 2,006:
98 97 96 95 93 92 91 90 89 87 86 85 84 82 81 80 78 77 75</lang>
This is a table of decision points. First row represents sum of our current uncommitted rolls. Second row represents the maximum permanent score where you should roll again with that number of uncommitted points, if we are using this estimation mechanism to choose our actions. Note that the first four columns here should have some obvious validity -- for example, if we have 96 permanent points and we have rolled 4 uncommitted points, we have won the game and we gain nothing from rerolling... Note also that this decision mechanism says we should never reroll if we have at least 20 uncommitted points.
 
 
=={{header|Julia}}==
<lang julia>mutable struct Player
score::Int
ante::Int
wins::Int
losses::Int
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)
scorewin(p) = for p in players if p == player p.wins += 1 else p.losses += 1 end; p.score = 0 end
player.ante = 0
while (r = rand(dice)) != 1
player.ante += r
if player.score + player.ante >= 100
scorewin(player)
return false
elseif player.strategy[2](player, players)
player.score += player.ante
break
end
end
true
end
 
function rungames(N)
turns = zeros(Int, length(players))
for i in 1:N
curplayer = rand(collect(1:length(players)))
while turn(players[curplayer])
turns[curplayer] += 1
curplayer += 1
if curplayer > length(players)
curplayer = 1
end
end
end
results = sort([(p.wins/(p.wins + p.losses), p.strategy[1]) for p in players], rev=true)
println(" Strategy % of wins (N = $N)")
println("------------------------------------------------------------")
for pair in results
println(lpad(pair[2], 34), lpad(round(pair[1] * 100, digits=1), 18), " ")
end
end
 
rungames(1000000)
</lang>{{out}}
<pre>
Strategy % of wins (N = 1000000)
------------------------------------------------------------
roll to 20 then if not losing stop 45.7
roll to 20 36.9
variable rand to stop 16.0
random choice to stop 1.4
</pre>
 
=={{header|M2000 Interpreter}}==
4,106

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.