# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: 'Python 3.8.5 64-bit (''thoughts-dev'': conda)'
#     language: python
#     name: python3
# ---

# ## Choose Your Own Adventure Game
#
# This sample is for a simple Choose Your Own Adventure style game. You could implement this in a console application or other user interface, so this notebook is meant to show you the "raw" assertion logic happening behind the scenes.

# +
import os, sys
sys.path.insert(1, os.path.abspath('..\\..'))
from thoughts.rules_engine import RulesEngine
import pprint

engine = RulesEngine()
# -

# ## Define the KB Rules (World)

# +
rules = [ 
    { "#when":  {"game-event": "start"},
     "#then":  [{"#output": "You are standing in a scary woods at night."},
               {"#output": "There are even scarier sounds coming from the north."},
               {"#output": "To go north, turn to page 15."},
               {"#output": "To stand there and whimper like a 3-year old, turn to page 10."}]
    },
  
    { "#when":  "10",
      "#then":  [{"#output": "You cry, and cry and cry and cry."},
                {"game-event": "start"}]
     },

    { "#when":  "15",
      "#then":  [{"#output": "North?? OK...."},
               {"#output": "You go north (a terrible choice, btw) and run into goblins."},
               {"#output": "To try talking with the goblins, turn to page 32."},
               {"#output": "To try sneaking past the goblins, turn to page 50."}]
    },
  
    { "#when":  "32",
      "#then":  [{"#output": "You try talking with the goblins."},
               {"#output": "Unfortunately, they do not speak your language and become murderous."},
               {"#output": "Roll a die to see if you escape them."},
               {"#output": "If you rolled a 2 or lower, #then turn to page 60."},
               {"#output": "If you rolled a 3 or higher #then turn to page 65."}]
    },

    { "#when":  "50",
      "#then":  [{"#output": "Your sneaky plan does not work."},
                {"#output": "Unfortunately, they drag you back into their lair and keep you as a pet."},
                {"#output": "GAME OVER"}]
     },

     { "#when":  "60",
       "#then":  [{"#output": "You missed. That's extremely bad."},
                {"#output": "Unfortunately, they knock yout out and take all of your money."},
                {"#output": "GAME OVER"}]
     },

     { "#when":  "65",
       "#then":  [{"#output": "Great job! You sneak past the goblins!"},
                {"#output": "#then you went on to live happily ever after."},
                {"#output": "GAME OVER"}]
     }
  ]

engine.load_rules_from_list(rules, "adventure-game")
# -

# ## Start the Game
#
# Any good game has a beginning. Start the game using the initial assertion to get things going. There's nothing special here about the words "game-event" or "start", you can use any designation as long as it triggers the rules you need.

response = engine.process({"game-event": "start"})

# ## Go North (Turn to Page 15)
#
# Uh oh - We ran into goblins!
#
# Go North by asserting "15", which will match the correponding #when rule and return the matching #then portion for that rule.

response = engine.process("15")

# ## Try Talking to the Goblins (Turn to Page 32)
#
# Let's see if we can talk our way out of this. Assert "32" to turn to page 32.

response = engine.process("32")

# ## Roll a 2 or Lower (Turn to Page 60)
#
# Run! Roll a die to see what happens next. Let's assume we roll a 2. Assert "60" and check the result

response = engine.process("60")

# ## Game Over
#
# Oh well - better luck next time! :)
#
# Modify the rules above to create your own game or try different paths through the game.

StarcoderdataJupyterScriptsDedupFiltered