dimanche 30 janvier 2022

The State Pattern for a Multiroom Game

I'm learning the state pattern. I have trouble to implement the following pattern with python and state_machine library.

enter image description here

My code so far:

from state_machine import (
    State,
    Event,
    acts_as_state_machine,
    after,
    before,
    InvalidStateTransition
)


@acts_as_state_machine
class Room:
    """The State pattern implemented for multiroom
    computer game in docs/state-diagrams.pdf
    """

    entry = State(initial=True)
    hall = State()
    dining_room = State()
    study = State()
    cellar = State()
    barn = State()
    ferry = State()

States seems easy to identify. Then come to transitions. I have trouble because each transition is from different state to another different state.

I tried to do the following:

east_to_hall = Event(from_states=entry,
                     to_state=hall)
south_to_hall = Event(from_states=dining_room,
                      to_state=hall)
west_to_hall = Event(from_states=study,
                     to_state=hall)
west_to_dining_room = Event(from_states=hall,
                            to_state=dining_room)
north_to_dining_room = Event(from_states=hall,
                             to_state=dining_room)
down_to_cellar = Event(from_states=(hall, study),
                       to_state=cellar)

But then it's messed up the transition. I'm asking for ideas on what's the transitions looks like in this state diagram.

Aucun commentaire:

Enregistrer un commentaire