So I’m making a IRC betting bot. Basically the game/bot has three states of taking-in chat.
State 1 : Betting
If anyone says “!bet team money” parse it and record.
State 2 : Battling
Take nothing in, no more !bet etc. Just watch.
State 3 : Waiting/Bank
Don’t take bets. But take-in “!bank” and print out the users money count.
Right now for each of my bot message parsers I have a big fat IF.
if (global_state == BETTING):
blah blah bah
AND I’m starting to need to have my bot do stuff when the state changes. So now I’m starting to do this (which hurts my soul a little every time I read it)
if (global_state == BETTING && past_state == WAITING):
do blah stuff like reset the bet money
As some of you might have noticed I’m using Python, but this problem definitely is not python-only. What should I do instead?
5
This looks like a good application of the ideas of “Finite State Machine” and “Polymorphism”.
You’ve already defined your states, so the next step is to define the transitions between states, and the behavior of the system when it’s in each state.
There are many ways to implement state machines, but in this case you might benefit from three classes, one for each state. Each should have functions “parse(…)” and “transition(…)”. The parse method just reads a line of input and does the right thing. The transition function decides what to do based on the new state that its going to, and returns the new state object.
(That’s a very rough cut for the transition function; there are probably better ways to do it.)
Then your state variable, instead of referring to a string, actually refers to the current state object. Then you can use nice syntax like “global_state.parse(…)” and “global_state = global_state.transition(…)”
That’s an extremely high-level view, and there are lots of ways to do it, but even if you just do that much, you’ll get rid of almost all the big fat ifs in your program, and your heart will feel lighter.
3