## Chutes And Ladders

### March 4, 2011

The children’s game *Chutes and Ladders*, pictured at right, is a chase game in which one or more players, moving alternately, move their tokens along the numbered spaces of a board according to the roll of a die; the tokens are initially off-the-board at what is effectively space zero. Tokens that land on the bottom end of a ladder are promoted to the space at the top end of the ladder, and tokens that land on the top end of a chute are demoted to the space at the bottom end of the chute. Space 100 must be reached by exact roll of the die; if the roll of the die would take the token past space 100, the token remains where it is and play passes to the next player. The winner of the game is the first token to reach space 100.

There are several interesting questions that can be asked about the game. First, what is the minimum number of rolls required to reach space 100. Second, for a single player, what is the average number of rolls required to reach space 100. And third, for *k* players, what is the average number of rolls until one of the players reaches space 100 and wins the game. S. C. Althoen, L. King and K. Schilling studied these and other questions in their paper “How Long Is a Game of Snakes and Ladders?” *The Mathematical Gazette*, Volume 77, Number 478, March 1993, pages 71-76.

Your task is to write programs that will answer those questions. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

[...] today’s Programming Praxis exercise, our goal is to simulate the board game Chutes and Ladders. Let’s [...]

My Haskell solution (see http://bonsaicode.wordpress.com/2011/03/04/programming-praxis-chutes-and-ladders/ for a version with comments):

Not terribly fast, though it gets the job done. To speed this up, I tried

1. Numpy arrays (which doesn’t get much of an improvement),

2. PyPy (which sped things up a lot), and

3. Cython to compile the code.

None of these three options come standard with the typical Python installation,

unfortunately. As a further exercise for myself, I went about compiling via

Cython, without changing the original code beyond all recognition; details

available on codepad.

I’ve got a Haskell solution similar to Remco Niemeijer’s one, so I decided not to repeat it here once more.

Instead, I solved the first question regarding the minimal number of rolls in a “theoretical” way, without simulating the game itself. I converted the game board into a directed graph — each node has 6 outgoing edges, corresponding to 6 possible rolls (or less, if it’s near 100) and then used breadth-first search to find out the shortest path length from start to finish. It turned out to be 7.

Here’s my python solution. Uses the dijkstra function from a previous exercise to find the minimum path.

These implementations which walk a tree take a long time to run – the first haskell program takes on the order of 30 seconds to print the first statistics on my system. A faster way is to create a transition matrix and repeatedly multiply with a state vector to compute the probability of any particular state after N turns. Experimentally, after 350 iterations, this accounts for 99.9999% of all games and produces an answer of avg turns = 39.2 in under a second of CPU time.

My Try in Rexx, Using the Dijkstra-Algorithm from http://programmingpraxis.com/2011/01/04/dijkstras-algorithm/ to find the optimal strategy