What you need is logic; Just sit, get a piece of paper if you have to, and write out all the things that are involved, (maybe in a circle or something).
Look, think about everything that could happen and what you would do at those times.
Think about if's and changing variables.
If that isn't enough (which it should be):
Consider your AI as being a thing with various 'states', and whenever in one 'state', the ai does certain things, and is succeptible to having the state changed by certain things happenening - These things ar
e different for each 'state', and so the AI moves from state to state throughout its existance, doing the things it does at those times.
Think about a beamos for example:
States: Rotating about, shooting the player.
In rotating about, it Rotates about, and IF it sees the player, it changes state into 'shooting the player'.
In 'shooting the player', it does nothing for a bit of a second, fires a beam, and then returns to the state 'rotating about'.
Think about this kind of plan for a things like enemies and CPU players, and how to implement this using code.