Skip to content

Instantly share code, notes, and snippets.

@robclewley
Last active January 25, 2016 20:27
Show Gist options
  • Save robclewley/d20155e8af3c59f51ae7 to your computer and use it in GitHub Desktop.
Save robclewley/d20155e8af3c59f51ae7 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Finite State Machine for Snakes and Ladders game"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class State(object):\n",
" def __init__(self, ix):\n",
" self.index = ix\n",
" self.link = None # placeholder, not None if Snake or Ladder\n",
" \n",
" def process(self):\n",
" \"\"\"Action when landed upon\"\"\"\n",
" if self.link:\n",
" if self.link > self.index:\n",
" # Ladder!\n",
" return self.link\n",
" else:\n",
" # Snake!\n",
" return self.link\n",
" else:\n",
" # link is None: \"Normal\" = not a snake or ladder\n",
" return self.index\n",
" \n",
"class GameFSM(object):\n",
" def __init__(self, n):\n",
" self.all_states = []\n",
" for ix in range(n+1):\n",
" self.all_states.append(State(ix))\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"game = GameFSM(16)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[<__main__.State at 0x3c81170>,\n",
" <__main__.State at 0x3c81190>,\n",
" <__main__.State at 0x3c811b0>,\n",
" <__main__.State at 0x3c811d0>,\n",
" <__main__.State at 0x3c811f0>,\n",
" <__main__.State at 0x3c81210>,\n",
" <__main__.State at 0x3c81230>,\n",
" <__main__.State at 0x3c81250>,\n",
" <__main__.State at 0x3c81270>,\n",
" <__main__.State at 0x3c81290>,\n",
" <__main__.State at 0x3c812b0>,\n",
" <__main__.State at 0x3c812d0>,\n",
" <__main__.State at 0x3c812f0>,\n",
" <__main__.State at 0x3c81310>,\n",
" <__main__.State at 0x3c81330>,\n",
" <__main__.State at 0x3c81350>,\n",
" <__main__.State at 0x3c81370>]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"game.all_states"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Ladders**"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"game.all_states[2].link = 10\n",
"game.all_states[8].link = 14"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"game.all_states[2].link"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"**Snakes**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment