Skip to content

Instantly share code, notes, and snippets.

@penryu
Created December 27, 2015 09:27
Show Gist options
  • Save penryu/f180953d96dce3f4827a to your computer and use it in GitHub Desktop.
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.
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}")
}
}
@penryu
Copy link
Author

penryu commented Dec 27, 2015

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:

  • The initial position is just off the board, designated square 0.
  • Only an exact spin to land on square 100 win. Spins landing beyond square 100 are ignored and play passes to the next player.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment