AI assignment using mini-max algorithm to play again a predefined opponent on server
For this assignment we have used, MiniMax algorithm. This algorithm creates all possible moves till a certain depth and then selects the best possible move with a definite heuristic.
The minimax algorithm works by simulating gameplay where we want to maximise our profit and the opponent wants to minimize the offering to us. Hence, we use a heuristic such that, we want to increase the heuristic and the opponent chooses the heuristic value which is least in value. We start our value, ie alpha with -infinity, which is the least value and the opponent starts the game with the max value ie +infinity. And we keep checking for each valid next move in the recursion and see what the heuristic value is. If it is greater than the current heuristic saved, then we replace it with the new one. Hence we get the most optimized result till a certain depth.
Our program works by the following points: Parse the game move sent by the server, and place the move to the board. Update the next set of possible moves. Next, we simulate the alpha-beta by deep copying the state of the game and play the game with the given heuristic and get the maximum profitable state and return and play it.
We have implemented the following heuristic (We are X and opponent is O) :- All the following conditions will be checked row-wise, column-wise and both diagonally.
| X | X | X |
| _ | _ | _ |
| _ | _ | _ |
| X | X | 0 |
| _ | _ | _ |
| _ | _ | _ |
| X | _ | X |
| _ | _ | _ |
| _ | _ | X |
Play centre: When the centre is not played, we give it 1 heuristic point. (5th tile)
| O | _ | _ |
| _ | _ | _ |
| _ | _ | _ | <- one possible point to play
Play empty corner: If any of the corners is empty, we give 1 point to the heuristic.
| X | X | _ | <- possible position to play
| _ | _ | _ |
| _ | _ | _ |
| X | _ | X |
| _ | _ | _ |
| _ | _ | X | <-one possible point to create a fork
| 0 | _ | 0 |
| _ | _ | _ |
| _ | _ | _ | <-one possible point to stop a fork
| O | X | O |
| _ | _ | _ |
| _ | _ | _ |
Moves leading us to winning position-> (1, 3, 7, 8) Moves that block opponent from winning -> (5, 9, 10) Moves that bring little benefit -> (2, 4, 6)
We assign weights to all these possible heuristic values and subtract the values with the opponent’s heuristic value which gives us our actual heuristic value (with consideration of the opponent).