Fix AI logic.

Use a sort of minimax method. More details about the implementation
in the NOTES.