Step 6 of 6
Challenge: a table-driven state machine
Protocols, UI flows, order pipelines and game rounds are state machines. Encoding the legal transitions in a table (instead of nested ifs scattered through the code) makes them easy to review, test and extend:
struct Transition { State from; Event on; State to; };
constexpr Transition table[] = {
{State::Idle, Event::Start, State::Warmup},
...
};
Your turn: implement a match lifecycle with these transitions:
| From | Event | To |
|---|---|---|
| Idle | Start | Warmup |
| Warmup | GoLive | Live |
| Live | Pause | Paused |
| Paused | Resume | Live |
| Live | End | Over |
| Paused | End | Over |
Write std::optional<State> next(State s, Event e) using a table, and class Match whose bool handle(Event e) applies a legal event (returning true) or rejects an illegal one (returning false, state unchanged). state() returns the current state and history() the number of accepted events.