Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/drvinceknight/sal
Generate the transition matrix for snakes 🐍 and ladders
https://github.com/drvinceknight/sal
Last synced: 2 months ago
JSON representation
Generate the transition matrix for snakes 🐍 and ladders
- Host: GitHub
- URL: https://github.com/drvinceknight/sal
- Owner: drvinceknight
- License: cc0-1.0
- Created: 2020-11-03T14:07:43.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2020-11-03T16:59:42.000Z (about 4 years ago)
- Last Synced: 2024-06-20T00:44:48.838Z (7 months ago)
- Language: Python
- Size: 15.6 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.4236276.svg)](https://doi.org/10.5281/zenodo.4236276)
![Validate README.md](https://github.com/drvinceknight/sal/workflows/Validate%20README.md/badge.svg)
# Snakes and ladders
This repository contains a script to generate [`main.csv`](./main.csv) which
contains the transition matrix for a Markov chain representation of the popular
board game Snakes and Ladders.## Assumptions
The official rules are used: a player must land exactly on 100, if they do not
they stay where they are. This is reflected in the final rows of the matrix.## Usage
### Generate the data
To generate the data:
$ python main.py
### Use the dataa
To load the data as a numpy array:
>>> import numpy as np
>>> P = np.loadtxt(fname="main.csv", delimiter=",")We can see we have a 101 by 101 array:
>>> P.shape
(101, 101)The last row corresponds to the absorbing state of ending the game:
>>> P[-1]
array([0., 0., ... 0., 1.])The penultimate row corresponding to the 99th square shows that 1/6 of the time
the game ends but 5/6th of the time the player stays where they are:>>> P[-2]
array([0. ... 0.833333, 0.166667])Similarly the row that corresponds to the 98th square captures that:
- 1/6 of the time they roll leads us to square 99 where there is a snake to square 70.
- 1/6 of the time the game ends
- 2/3 of the time the player stays where they are.We can see check this:
>>> P[-3]
array([0. ... 0.166667, ... 0.666667, 0. , 0.166667])
>>> P[-3, 70]
0.166667## To test that the results listed in this README are as expected
$ python -m pytest --doctest-glob=README.md