FSM - conditional transition

I would like to handle such a scenario: I've got a game. A few players (unknown number) can join the game. A player can show his readiness to play pushing the start button. All other players are notified about pushing the start button. When the last player pushes the button the game starts.

So I don't know how to use the transition matrix to define my game's state machine. It looks like sometimes the start event leads to a transition to the same state while at the end to a new state. Maybe the problem is with bad states/events design?

asked Apr 5, 2018 at 19:58 user2146414 user2146414 1,018 1 1 gold badge 14 14 silver badges 31 31 bronze badges

2 Answers 2

If it is still relevant, what you can do here is modeling your space with 2 states:

Then monitor the events with a counter. Let us say that N is the number of players, each time a player click on ready you set counter = counter + 1 . Your event is called PlayersReady = false at the beginning. At each cycle PlayersReady = counter >= N , so you can monitor whether everyone are ready or not. PlayersReady becomes your transition logic:

Initial state = NotReadyState if (NOT PlayersReady) newState = NotReadyState else newState = ReadyState

answered Jul 27, 2018 at 10:27 Andrea Nisticò Andrea Nisticò 496 2 2 gold badges 4 4 silver badges 12 12 bronze badges

Conditional transition is not pure FSM approach. A strict solution is to have as many states as quantity of players, say N. So for each K-th ready player transition would look something like -> . So N Players Ready switch will be equivalent to All Players Ready state.

However if there are non constant number of players each time, or number is just too large, so there is no way to hold so many states, you will need a bit complex solution that includes stack state memory that remembers previous state. Solution would be to initialize stack as if there were N-1 non ready states before and All Players Ready state in the bottom of stack (as if in q[0-N] state players were all ready). So when the K-th player presses ready button state stack will be reduced by one (player becoming not ready adds previous state to stack). That does not break determinism but still makes it a bit messy.

Another approach, that I used for poker croupier bot, is to delegate counting duty to an external system. The game state machine does not need to keep every state literally. It is enough to keep it consistent to it's own logic. So added a new object "Table" (player slot holder) which acts as an event source. It resides between real events and state machine and has a trivial counter. Table itself sends events like "Empty", "Full", "Players voted ready" and so on. A game instead deals only with "Current", "Next", "Next active" player entities. So the actual number could be infinite.