This is the actual AI. Pretty long ! have to reduce the code