I am very new to python and am trying to start getting in to coding. I am trying to create a simple bot to play a facebook game and after doing some research, I believe that a state machine is they right way to go. This is an outline of what I'm trying to do, but I am sure this isn't the proper way to code it. Once the program gets more complicated, this code would become convoluted. Could someone help me translate this into more robust state machine code? Also, is there a way I can check in between states if the user has pressed a key so I can pause the machine? Thanks.
state = 1
def runStateMachine():
global state
if state == 1:
doSomething()
state = 2
elif state == 2:
if checkSomething():
state = 3
else:
state = 1
elif state == 3:
finishSomething()
state = 1
while True:
runStateMachine()
sleep(5)
#check if a key has been pressed and pause here until the key is pressed again
The idea of a state machine isn't a bad one, depending on the type of game you're trying to write a bot for. Your observation that the code is going to get more complicated than you want it to be is a good one, and this is a good time to step back and consider your options.
From your code and your questions, it appears you're fairly new to programming, so you might want to look into a few basics to decide if they will be of help to you.
For example, if you need more than one state machine, or would like to compose your state machine from multiple smaller ones, you might want to consider writing your state machine as a class, which you can then reuse. The same applies if you want to write different ones for different games, or for different strategies to play the same game.
You're asking about keyboard input and that's something you can tackle separately, but having a single main loop, that progresses in steps that are interrupted by key presses may not be the best approach when you start adding things. Consider what kinds of input and state changes should affect what your program is doing and focus on getting the important parts right before worrying too much about how to put it all together.
To answer your questions: there are better ways to write your code and since finite state machines are a very common computer science concept, there are many libraries out there, one of which is likely to meet your needs and save you a lot of time. For example, look at https://github.com/pytransitions/transitions
As for checking for keypresses: you can simply force the user to enter something with input()
, but if you want to see if a key happens to have been pressed, you should check out a library like keyboard
and specifically is_pressed()
https://github.com/boppreh/keyboard#keyboard.is_pressed
Note that I don't recommend these libraries, I just want to point out that for common problems, libraries are often the best way to go. Just take a look at the enormous amount of work that has already gone into them and the time it took the authors to get it right.
Not every library is perfect and some programmers dislike the idea of including code they don't need, but it's definitely the way to go if you want to work on something that does something without spending days reinventing the wheel. Once you have something that works, you can always consider replacing a library with something more concise, if you feel confident you can do better - and perhaps you want to release that as a library yourself!