Optimized algorithm for player ai