How to resolve the algorithm Pig the dice game/Player step by step in the Julia programming language
How to resolve the algorithm Pig the dice game/Player step by step in the Julia programming language
Table of Contents
Problem Statement
Create a dice simulator and scorer of Pig the dice game and add to it the ability to play the game to at least one strategy.
As a stretch goal:
The game of Pig is a multiplayer game played with a single six-sided die. The object of the game is to reach 100 points or more. Play is taken in turns. On each person's turn that person has the option of either
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Pig the dice game/Player step by step in the Julia programming language
This Julia code simulates a game where multiple players take turns rolling dice and accumulating points. The game ends when a player reaches 100 points, or when they roll a 1. Each player has a specific strategy for deciding when to stop rolling and accumulate their points.
The code defines a Player
struct to represent each player, with attributes for their score, ante (current roll), wins, losses, and strategy. It also defines a dictionary of strategies, each with a name and a function that takes a player and a group of players as arguments and returns a boolean indicating whether the player should stop rolling.
The turn
function simulates a single turn for the specified player. It rolls a die repeatedly until the player either rolls a 1 or decides to stop based on their strategy. If the player's score reaches or exceeds 100, they win and their score is reset to 0. If the player rolls a 1, their turn ends without accumulating any points.
The rungames
function runs multiple games (specified by the parameter N
) and collects statistics on the performance of each strategy. It prints a table showing the strategy name and the percentage of wins for each strategy.
Here's a step-by-step explanation of the code:
-
Define the
Player
struct to represent each player. -
Define a dictionary of strategies, where each strategy is associated with a name and a function that takes a player and a group of players as arguments and returns a boolean indicating whether the player should stop rolling.
-
Define the
turn
function to simulate a single turn for the specified player. -
Define the
rungames
function to run multiple games and collect statistics on the performance of each strategy. -
Run the
rungames
function withN = 1000000
to simulate 1,000,000 games. -
Print a table showing the strategy name and the percentage of wins for each strategy.
Source code in the julia programming language
mutable struct Player
score::Int
ante::Int
wins::Int
losses::Int
strategy::Pair{String, Function}
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)
scorewin() = 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
verbose && println("Player $playernum rolls a $r.")
if player.score + player.ante >= 100
scorewin()
verbose && println("Player $playernum wins.\n")
return false
elseif player.strategy[2](player, players)
player.score += player.ante
verbose && println("Player $playernum holds and has a new score of $(player.score).")
return true
end
end
verbose && println("Player $playernum rolls a 1, so turn is over.")
true
end
function rungames(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)))
while turn(players[curplayer], verbose)
curplayer = curplayer >= length(players) ? 1 : curplayer + 1
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)
You may also check:How to resolve the algorithm Largest int from concatenated ints step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Create a file step by step in the Visual Objects programming language
You may also check:How to resolve the algorithm Conditional structures step by step in the EMal programming language
You may also check:How to resolve the algorithm Pythagorean triples step by step in the Visual Basic programming language
You may also check:How to resolve the algorithm HTTPS/Authenticated step by step in the Go programming language