Skip to content

Instantly share code, notes, and snippets.

@gbzarelli
Last active November 9, 2020 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbzarelli/ee38dc724dbbd4ccc4b16924b0d2e012 to your computer and use it in GitHub Desktop.
Save gbzarelli/ee38dc724dbbd4ccc4b16924b0d2e012 to your computer and use it in GitHub Desktop.
Article about Finite State Machine - Step 2 - The simple implementation
# def doFind(pokeId) ...
# def doAttack(pokeId) ...
# def doCatch(pokeId) ...
while True:
print("Find the pokemon (input the pokeId):")
pokeId = input()
if not doFind(pokeId):
continue
visible = True
while visible:
print("Attack pokemon?")
attack = input()
if attack and doAttack(pokeId):
print("Pokemon is injured")
while True:
print("Attack pokemon?")
attack = input()
if attack and doAttack(pokeId):
print("Pokemon is dead")
visible = false
break
else:
print("Catch the pokemon?")
catch = input()
if catch and doCatch(pokeId):
print("Pokemon is catch")
visible = false
break
else:
print("Catch pokemon?")
catch = input()
if catch:
print("Pokemon ran away and hid")
from enum import Enum, auto
class States(Enum):
HIDDEN = auto()
VISIBLE = auto()
INJURED = auto()
CAPTURED = auto()
DEAD = auto()
class Events(Enum):
FIND = auto()
OBSERVE = auto()
ATTACK = auto()
CATCH = auto()
@staticmethod
def get_by_name(name):
retunr Events[name.upper()]
class Pokemon:
def __init__(self, poke_id, name):
self.state = States.HIDDEN
self.poke_id = poke_id
self.name = name
def process_event(pokemon, event):
if pokemon.state == States.HIDDEN:
if event == Events.FIND:
# add some method to check
pokemon.set_state(States.VISIBLE)
print(f'Wild {pokemon.name} appeared!')
else:
return False
elif pokemon.state == States.VISIBLE:
if event == Events.OBSERVE:
print('Pokemon was observed!')
elif event == Events.ATTACK:
# add some method to check if attack is effective
pokemon.set_state(States.INJURED)
print('Ohh, you hurt the Pokemon!')
else:
return False
elif pokemon.state == States.INJURED:
if event == Events.ATTACK:
# add some method to check if attack is effective
pokemon.set_state(States.DEAD)
print('Pokemon was DEAD!')
elif event == Events.CATCH:
# add some method to check if catch action is effective
pokemon.set_state(States.CAPTURED)
print(f'{pokemon.name} was captured!')
else:
return False
return True
pokemons = {
'1': Pokemon(1, "Bulbasaur"),
'2': Pokemon(2, "Charizard"),
'3': Pokemon(3, "Magikarp")
}
while True:
print("Input the command: poke_id, action | Like: 1,find:")
data = input().split(",")
pokemon = pokemons.get(data[0])
event = Events.get_by_name(data[1])
processed = process_event(pokemon, event)
if processed is False:
print(f'The event {event.name} is not accepted for status {pokemon.state.name}')
from enum import Enum, auto
import time
class States(Enum):
HIDDEN = auto()
VISIBLE = auto()
INJURED = auto()
CAPTURED = auto()
DEAD = auto()
class Events(Enum):
FIND = auto()
OBSERVE = auto()
ATTACK = auto()
CATCH = auto()
@staticmethod
def get_by_name(name):
retunr Events[name.upper()]
class Pokemon:
def __init__(self, poke_id, name):
self.state = States.HIDDEN
self.poke_id = poke_id
self.name = name
self.level = level
pokemons = {
'1': Pokemon(1, "Bulbasaur"),
'2': Pokemon(2, "Charizard"),
'3': Pokemon(3, "Magikarp")
}
def process_event(pokemon, event):
if pokemon.state == States.HIDDEN:
if event == Events.FIND:
# add some method to check
pokemon.set_state(States.VISIBLE)
print(f'Wild {pokemon.name} appeared!')
else:
return False
elif pokemon.state == States.VISIBLE:
if event == Events.OBSERVE:
print('Pokemon was observed!')
elif event == Events.ATTACK:
# add some method to check if attack is effective
pokemon.set_state(States.INJURED)
print('Ohh, you hurt the Pokemon!')
else:
return False
elif pokemon.state == States.INJURED:
if event == Events.ATTACK:
# add some method to check if attack is effective
pokemon.set_state(States.DEAD)
print('Pokemon was DEAD!')
elif event == Events.CATCH:
# add some method to check if catch action is effective
pokemon.set_state(States.CAPTURED)
print(f'{pokemon.name} was captured!')
else:
return False
return True
print("Input the command: poke_id, action | Like: 1,find:")
while True:
data = input().split(",")
pokemon = pokemons.get(data[0])
event = Events.get_by_name(data[1])
processed = process_event(pokemon, event)
if processed is False:
print(f'The event {event.name} is not accepted for status {pokemon.state.name}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment