# Snakes and ladders by @ellis2013nz

**free range statistics - R**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

More than five years ago I started a series of posts on games of chance and for one reason or another never got around to finishing them. Partially redressing that, here is a long-lost and now tidied up post on Snakes and Ladders. Read it before you next *gaze across the smoke filled casino table at your opponent, a sinister operative of SMERSH, and take a sip at your vodka martini while deciding whether to take his proferred wager or not with him on square 90 and you on 75…*

OK, maybe that scenario is unlikely. But one of my themes in these posts is that even games of pure chance will become skill if you gamble on them. Choices like whether to bet, what odds to offer or accept, and (in some games) whether to offer, accept or decline a doubling cube all turn even a pure chance game like Persian Monarchs into one where the best player will win (in the long run).

First, a reminder of how Snakes and Ladders works. Here is a stylised version of a common Snakes and Ladders board, the Milton Bradley 1952 “Chutes and Ladders” (American children are frightened of snakes, apparently) depicted on the Wikipedia page. I’ve used arrows rather than snakes or chutes and ladders, and of course omitted all the extra imagery, which while interesting for a historian of morality and games, isn’t relevant to gameplay. I was actually in Sri Lanka back in 2018 when I started this Snakes and Ladders work, so had a particular interest in its origins, but I don’t have time to explain that now.

You start at a virtual square 0, roll a six-sided dice and move your counter that number of squares. If you end in a position with a ladder, you move up to where it finishes. So if your first roll is a 1,you move to square 1 and immediately climb the ladder to square 38. If you end on a snake, you move down to the tail of the snake.

Here’s the R code to draw that board:

Now, the Wikipedia page claims that Snakes and Ladders can be represented exactly as an absorbing Markov chain, because the transition probability from each square to any other square is fixed and easy to define, and not dependent on the path the counter took to that square. However, this model is only a useful one if you omit the common rule that rolling a six gets you another roll; and rolling three sixes sends you “back to square one” (in fact, Snakes and Ladders is the origin of this common English phrase).

I want a realistic simulation of the game as actually played, so after mucking around with Markov chains for a while realised that it was going to be much simpler to write code that mimics how the players go about it. That’s what I used to simulate 10,000 solo games to create this animation of a hundred Snakes and Ladders games:

The code for which is below. All the heavy work is done by the `sl_game()`

function, which takes as an argument the current position of the player (defaults to zero) – we’ll be using this more later.

Here’s the output from that bit of analysis of the number of turns and rolls (remembering you can get more rolls than turns, if you get sixes)

variable `mean(value)` `median(value)` <chr> <dbl> <dbl> 1 rolls 42.7 34 2 turns 35.0 28

It’s a bit more complicated than the mean 39.2 dice throws that can be analytically calculated as the average number of rolls needed under the simplified version modelled with a Markov chain. The expected number of rolls is higher because of ‘back to square one’; the number of turns is fewer because of the bonus rolls you get when you have a six.

The distribution of the number of rolls required is very much squewed with a long tail – it is in fact possible for a game to go on forever (although extremely improbable):

OK, so where does the gambling come in? Gambling becomes interesting when there is a choice about the *timing* or *odds* of a wager. If all gambling was an even odds stake put down before the game, then Snakes and Ladders would stay a game of pure chance. But if it is possible, say at the beginning of your turn, to size up the board and say “I’ll bet you on even odds that I’ll win”, you’re making a choice based on your knowledge of the game. A naive observer might think that anyone who is at a more advanced square is more likely to win, and therefore could be trapped into taking a bet that would only be fair if there were favourable odds given to them.

To explore this with Snakes and Ladders, I simulated a thousand solo games from each square that is a valid starting position for a turn (e.g. excluding squares that are at the bottom of a ladder – you can’t end a turn on that square because if you land there you would go straight up the ladder). This gives us a probability distribution for how many turns it is expected to take to win from that point. If we do a full join of this distribution to itself, we will get a joint probability for how many turns it will take player 1 to win from that position and every combination of player 2’s starting points.

We can visualise the result in a chart like this one. The highlight boxes are where Player 1, who is about to roll, has a surprisingly good chance of winning (higher than 0.55), despite being behind Player 2 in the race. These are the probable opportunities to offer a 50:50 bet to your opponent; only an unusually disciplined or knowledgeable player would think they were losing in this position.

.

Here are some of those positions:

start_p1 start_p2 p1 p2 unusual how_surprising <int> <int> <dbl> <dbl> <lgl> <dbl> 1 65 81 0.611 0.389 TRUE 0.761 2 66 81 0.607 0.393 TRUE 0.746 3 65 82 0.590 0.410 TRUE 0.744 4 67 81 0.615 0.385 TRUE 0.743 5 68 81 0.622 0.378 TRUE 0.740 6 69 81 0.626 0.374 TRUE 0.735 7 63 81 0.570 0.430 TRUE 0.733 8 22 29 0.555 0.445 TRUE 0.731 9 74 81 0.667 0.333 TRUE 0.730 10 66 82 0.587 0.413 TRUE 0.729

The `how_surprising`

column is a metric I made up that tries to incorporate both the high probability of Player 1 winning and how far they currently *seem* to be behind. So if you are Player 1 on square 65, and see your opponent on 81, now is the time to offer them a bet.

Intuitively, why are 81 and 82 bad squares? It’s because you’ve missed the ladders from 80 straight to 100 (instant win) and from 71 to 90. You still have snakes at 93, 95 and 98 that might trip you up whereas your opponent might skip them altogether if they land on square 80 – which is a non-trivial 1/6 chance for them.

Similarly, square 29 is a bad one because you’ve just missed the big ladder starting at square 28, whereas your apparently-behind opponent still has a chance to get on it.

Here’s the code for running those simulations and drawing the chart:

Finally, because of my backgammon interests, I wondered about the use of a doubling cube. In backgammon, at the beginning of your turn (before throwing the dice), you have the option of offering your opponent the doubling cube. They can accept, in which case the game is now being played for twice as many points / stakes; or refuse in which case they lose the game instantly at the current stake. Once they have accepted the cube, only they can offer it again (presumably if the fortunes change their way).

Generally speaking – putting aside some backgammon-specific complications – it makes sense to accept the doubling cube if you have a 1 in 4 or better chance of winning. If you have a 0.75 probability of winning and have the cube available, you should definitely offer the double; and your opponent should refuse. Now, I’ve never heard of Snakes and Ladders with a doubling cube but actually I am sure it has happened (or will happen in the future). So it’s worth highlighting what are the points at which Player 2 should decline the cube if offered and accept the loss at the current stake? This chart answers this for us (although probably a table would be more useful for actual use):

Produced with this little snipped of extra code:

OK folks, that’s all. Remember to gamble responsibly. In particular, if you use this post to rip off any kids, their sadness will be on your conscience, and I absolutely disclaim all responsibility for any misuse of any kind of any of the above material.

**leave a comment**for the author, please follow the link and comment on their blog:

**free range statistics - R**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.