This is much harder than you'd think.
Here's a fun story from 15 years ago when a friend of mine tried to do some simple AI:
His goal was to have a humanoid shape created is software learn how to walk using simple AI. The idea was that it would obey some basic laws (gravity, the limits of its joints, etc.), do something random, check whether or not it was closer to the goal of walking, tweak its parameters, and iterate. The chosen goal was not to fall over.
He set up the program, let it run over the weekend and let it do millions of tries. Hopefully when he came back to the office it would have learned how to walk, or at least stand up without falling.
His disappointment was huge when he came into the office: The simulated robot was sitting down with its knees bent, thus having achieved the goal of not falling over.