{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# import piplite\n# await piplite.install('python-statemachine[diagrams]')\n# import patch_repr_svg"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Guess the number machine\n\nAn StateMachine for the well know game.\n\nWell leave the machine imagine a number and also play the game. Why not?\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import random\n\nfrom statemachine import State\nfrom statemachine import StateMachine\n\n\nclass GuessTheNumberMachine(StateMachine):\n\n    start = State(\"Start\", initial=True)\n    low = State(\"Low\")\n    high = State(\"High\")\n    won = State(\"Won\", final=True)\n    lose = State(\"Lose\", final=True)\n\n    guess = (\n        lose.from_(low, high, cond=\"max_guesses_reached\")\n        | won.from_(low, high, cond=\"guess_is_equal\")\n        | low.from_(low, high, start, cond=\"guess_is_lower\")\n        | high.from_(low, high, start, cond=\"guess_is_higher\")\n    )\n\n    def __init__(self, max_attempts=5, lower=1, higher=5, seed=42):\n        self.max_attempts = max_attempts\n        self.lower = lower\n        self.higher = higher\n        self.guesses = 0\n\n        # lets play a not so random game, or our tests will be crazy\n        random.seed(seed)\n        self.number = random.randint(self.lower, self.higher)\n        super(GuessTheNumberMachine, self).__init__()\n\n    def max_guesses_reached(self):\n        return self.guesses >= self.max_attempts\n\n    def before_guess(self, number):\n        self.guesses += 1\n        print(\"You guess is {}...\".format(number))\n\n    def guess_is_lower(self, number):\n        return number < self.number\n\n    def guess_is_higher(self, number):\n        return number > self.number\n\n    def guess_is_equal(self, number):\n        return self.number == number\n\n    def on_enter_start(self):\n        print(\"(psss.. don't tell anyone the number is {})\".format(self.number))\n        print(\n            \"I'm thinking of a number between {} and {}. Can you guess what it is?\".format(\n                self.lower, self.higher\n            )\n        )\n\n    def on_enter_low(self):\n        print(\"Too low. Try again.\")\n\n    def on_enter_high(self):\n        print(\"Too high. Try again.\")\n\n    def on_enter_won(self):\n        print(\n            \"Congratulations, you guessed the number in {} guesses!\".format(\n                self.guesses\n            )\n        )\n\n    def on_enter_lose(self):\n        print(\"Oh, no! You've spent all your {} attempts!\".format(self.guesses))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Playing\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sm = GuessTheNumberMachine(seed=103)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sm.guess(random.randint(1, 5))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sm"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sm.guess(random.randint(1, 5))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sm.guess(random.randint(1, 5))\n\n\nsm"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sm.guess(random.randint(1, 5))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sm.guess(random.randint(1, 5))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sm"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "try:\n    sm.guess(random.randint(1, 5))\nexcept Exception as e:\n    print(e)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8.1"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}