As the final project in my Artificial Intelligence class, we created a game of chess in Unity where the computer made informed and strategic moves.
The AI is relatively simple. First It generates a list if possible moves, pushing them into a fitness sorted priority queue. Of those moves, it evaluates the ‘fitness’ of the resulting board. This is an incredibly simple calculation, as it just tallys each of the AI’s piece’s multiplied it by its associated value in inspector.
The players pieces then do some calculation, and the result is subtracted. This final value gives us our fitness for the move.
After each move is generated, it chooses the current best move (highest fitness), simulates it, and then attempts to predict what the players response will be. After generating a list of all of those moves, it assumes that the player will make the best move that it can, and so simulates said move. The new board (now two simulations in) is then fed back into the priority queue.
The algorithm then grabs the top of the priority queue, and the simulations begin again (resuming where they were if future moves have already been simulated for the current best performing move).
Now, it could keep doing this forever (or until it runs out of moves to simulate), but that wouldn’t be very fun for the player or the machine its running on.
Soooooo, there are two limiting factors: Depth, and Think Time
The depth refers to the number of simulated moves the game will generate spanning from a single starter move (included in the total). Given that it simulates its own moves and the players, a depth setting of 5 allows the AI to “plan” its next 3 moves.
Think time is what usually kills the AI’s turn, as it ensures that no matter what depth the AI is allowed to simulate to, it cant be thinking for more seconds than the Think Time Limit.
If you would like to see the code or download a playable build, head on over to my github: https://github.com/QuantumInfinite/ChessGame