Created
December 27, 2015 09:27
-
-
Save penryu/f180953d96dce3f4827a to your computer and use it in GitHub Desktop.
Estimates the minimum, maximum, and average number of turns in a game of Chutes and Ladders.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Chutes { | |
val adjust = Map( | |
1 -> 38, 4 -> 14, 9 -> 31, 16 -> 6, 21 -> 42, 28 -> 84, 36 -> 44, | |
48 -> 26, 49 -> 11, 51 -> 67, 56 -> 53, 62 -> 19, 64 -> 60, 71 -> 91, | |
80 -> 100, 87 -> 24, 93 -> 73, 95 -> 75, 98 -> 78 | |
) | |
val rand = new util.Random | |
val runCount = 10000 | |
@annotation.tailrec | |
def play(pos: Int, turns: Int): Int = | |
if (pos == 100) turns | |
else { | |
val next = pos + rand.nextInt(6) + 1 | |
if (next > 100) play(pos, turns + 1) | |
else play(adjust.getOrElse(next, next), turns + 1) | |
} | |
def main(args: Array[String]): Unit = { | |
val runs = (1 to runCount).map { _ => play(0, 0) } | |
val avg = runs.sum.toDouble / runCount | |
println(f"Average of $runCount runs is $avg%.2f") | |
println(f"Max turns was ${runs.max}") | |
println(f"Min turns was ${runs.min}") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Most instructions specify that an exact spin is required to win, but none specify what happens if the spin is not exact... except the following:
Chutes and Ladders instructions, ca. 1978
This program includes adjustments for gameplay according to these instructions, notably the following: