The AI uses a Minimax algorithm with Alpha-Beta pruning to figure out the best move.
It simulates all the possible game outcomes and chooses the path that minimizes loss and maximizes its chances of winning.
This ensures the AI plays optimally and never loses.