Snake and ladder: Difference between revisions

Line 812:
}
}</lang>
 
=={{header|Julia}}==
The game is pure chance, with a 2% advantage to going before the next player. See statistics below.
<lang julia>const landingtoending = Dict(4 => 14, 9 => 31, 17 => 7, 20 => 38, 28 => 84, 40 => 59,
51 => 67, 54 => 34, 62 => 19, 63 => 81, 64 => 60, 71 => 91, 87 => 24, 93 => 73,
95 => 75, 99 => 78)
 
const sixesrollagain = true
 
function takeaturn(player, square, verbose=true)
while true
roll = rand(1:6)
verbose && print("Player $player on square $square rolls a $roll ")
if square + roll > 100
verbose && println(" but cannot move.")
else
square += roll
verbose && println(" and moves to square $square.")
if square == 100
return 100
end
next = get(landingtoending, square, square)
if square < next
verbose && println("Yay! landed on a ladder. Climb up to $next.")
if square == 100
return 100
end
square = next
elseif square > next
verbose && println("Oops! Landed on a snake chute. Slither down to $next.")
square = next
end
end
if roll < 6 || !sixesrollagain
return square
else
verbose && println("Rolled a 6, so roll again.")
end
end
end
 
function snakesandladdersgame(nplayers, verbose=true)
players = ones(Int, nplayers)
while true
for (player, position) in enumerate(players)
ns = takeaturn(player, position, verbose)
if ns == 100
verbose && println("Player $player wins!")
return player
end
players[player] = ns
verbose && println()
end
end
end
 
snakesandladdersgame(3)
 
using DataFrames, GLM
 
function slstats(nplayers, ngames)
players = zeros(Int, nplayers)
for i in 1:ngames
winner = snakesandladdersgame(nplayers, false)
players[winner] += 1
end
println("\n\n\nStats: out of $ngames, winners are:\n # | wins \n----------------")
for (i, player) in enumerate(players)
println(" $i $player")
end
data = DataFrame(X = collect(1:nplayers), Y = players)
ols = lm(@formula(Y ~ X), data)
println("\nStatistics:\n", ols)
end
 
slstats(5, 1000000)
</lang>{{out}}
<pre>
Player 1 on square 1 rolls a 5 and moves to square 6.
 
Player 2 on square 1 rolls a 3 and moves to square 4.
Yay! landed on a ladder. Climb up to 14.
 
Player 3 on square 1 rolls a 5 and moves to square 6.
 
Player 1 on square 6 rolls a 1 and moves to square 7.
 
Player 2 on square 14 rolls a 5 and moves to square 19.
 
Player 3 on square 6 rolls a 5 and moves to square 11.
 
Player 1 on square 7 rolls a 2 and moves to square 9.
Yay! landed on a ladder. Climb up to 31.
 
Player 2 on square 19 rolls a 3 and moves to square 22.
 
Player 3 on square 11 rolls a 3 and moves to square 14.
 
Player 1 on square 31 rolls a 5 and moves to square 36.
 
Player 2 on square 22 rolls a 3 and moves to square 25.
 
Player 3 on square 14 rolls a 4 and moves to square 18.
 
Player 1 on square 36 rolls a 6 and moves to square 42.
Rolled a 6, so roll again.
Player 1 on square 42 rolls a 6 and moves to square 48.
Rolled a 6, so roll again.
Player 1 on square 48 rolls a 3 and moves to square 51.
Yay! landed on a ladder. Climb up to 67.
 
Player 2 on square 25 rolls a 2 and moves to square 27.
 
Player 3 on square 18 rolls a 3 and moves to square 21.
 
Player 1 on square 67 rolls a 1 and moves to square 68.
 
Player 2 on square 27 rolls a 1 and moves to square 28.
Yay! landed on a ladder. Climb up to 84.
 
Player 3 on square 21 rolls a 6 and moves to square 27.
Rolled a 6, so roll again.
Player 3 on square 27 rolls a 5 and moves to square 32.
 
Player 1 on square 68 rolls a 3 and moves to square 71.
Yay! landed on a ladder. Climb up to 91.
 
Player 2 on square 84 rolls a 5 and moves to square 89.
 
Player 3 on square 32 rolls a 2 and moves to square 34.
 
Player 1 on square 91 rolls a 6 and moves to square 97.
Rolled a 6, so roll again.
Player 1 on square 97 rolls a 5 but cannot move.
 
Player 2 on square 89 rolls a 4 and moves to square 93.
Oops! Landed on a snake chute. Slither down to 73.
 
Player 3 on square 34 rolls a 1 and moves to square 35.
 
Player 1 on square 97 rolls a 3 and moves to square 100.
Player 1 wins!
 
 
 
Stats: out of 1000000, winners are:
# | wins
----------------
1 208036
2 204760
3 199392
4 195319
5 192493
 
Statistics:
StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}
 
Y ~ 1 + X
 
Coefficients:
──────────────────────────────────────────────────────────────────────────────────────
Estimate Std. Error t value Pr(>|t|) Lower 95% Upper 95%
──────────────────────────────────────────────────────────────────────────────────────
(Intercept) 2.12158e5 772.519 274.632 <1e-6 2.097e5 2.14617e5
X -4052.7 232.923 -17.3993 0.0004 -4793.97 -3311.43
──────────────────────────────────────────────────────────────────────────────────────
</pre>
 
=={{header|Kotlin}}==
4,105

edits