


Suppose the board looks as follows:
X O X
O O X
_ _ _
We can see that if we put X at positions 3, 3 in the grid we win straightaway. Hence the best possible score is 1 (Win). If we put our symbol anywhere else we will not be able to achieve the desired score.
If there are multiple positions leading to the same score, choose the position with the minimum row. If there are still multiple positions, choose the one with the minimum column.
Each input is described by 3 lines each of which is a string of 3 characters denoting the state of that row of the grid.
Output 3 integers - the maximum score, the row and the column of the optimal move.
You are not required to print anything; it has already been taken care of. Just implement the function.
The size of the grid will always be 3.
Time Limit: 1 sec.
We will be using the minimax algorithm to evaluate the score of the board. Our objective will be to maximize the score we can get across all possibilities, while the objective of the opponent will be to minimize the score. So at every stage we will maintain whose turn it is now, if it is ours we maximize across all the remaining states else the opponent minimizes across all possible moves on the current board.
findTheWinner(board, player) finds the best score achievable and the position of the move to make.
minimax(board, turn) takes the state of the board and the player whose turn it is. It evaluates the board to check for final state (when one of the players wins or no moves are left) and returns a score based on that.
isMovesLeft(board) returns true if the board has a move left else returns false
isGameOver(board) returns 1 if you have won the game, -1 if the opponent won the game, else 0.