Now let’s get back to Chutes and Ladders! Depending on the game you’ve chosen to simulate, you’ll need to structure the simulation differently. For a linear game like Chutes and Ladders, we know that we will progress relatively linearly from tile 0 to tile 100, with a few exceptions (the chutes and the ladders). We will construct a tileMap variable so we can call

tileMap[tile_I_just_landed_on]

and return the tile number a chute or ladder would move me to.

Starting with library imports, we set up our header:

import numpy as np
import matplotlib.pyplot as plt
from copy import deepcopy


nStates = 107   # the number of possible states attainable immediately after a roll
tileMap = np.arange(nStates)    # initialize the tilemap
mapFrom = [1,  4,  9,  21, 28, 36, 48, 49, 51, 56, 62, 64, 71, 80,  87, 93, 95, 98]
mapTo =   [38, 14, 31, 42, 84, 44, 26, 11, 67, 53, 18, 60, 91, 100, 24, 73, 76, 78]
tileMap[mapFrom] = mapTo    # mapFrom is the start of a chute/ladder, mapTo is its destination
tileMap[101:] = 99   # overshooting tile 100 leaves the player on tile 99

The Numpy library will handle all our math, matplotlib will handle plotting, and deepcopy will be used to fix a bug that took me WAY TOO DANG LONG to track down.

NOTE: Despite only having tiles 0-100, I’ve defined 107 states here — why? If you’re standing on tile 99, and you (very unwisely) try to move forward 6 tiles, and move 7 tiles due to overshoot, you’d land on tile 106, meaning we actually have to account for 107 total states (0-106). On the last line, we say that landing on any tile over 100 just plops you back on tile 99.

Leave a Reply