![]() |
VOOZH | about |
Prerequisite: Minimax Algorithm in Game Theory
As seen in the above article, each leaf node had a value associated with it. We had stored this value in an array. But in the real world when we are creating a program to play Tic-Tac-Toe, Chess, Backgammon, etc. we need to implement a function that calculates the value of the board depending on the placement of pieces on the board. This function is often known as Evaluation Function. It is sometimes also called a Heuristic Function.
The evaluation function is unique for every type of game. In this post, the evaluation function for the game Tic-Tac-Toe is discussed. The basic idea behind the evaluation function is to give a high value for a board if the maximizer turn or a low value for the board if the minimizer turn.
For this scenario let us consider X as the maximizer and O as the minimizer.
Let us build our evaluation function :
We could have chosen any positive/negative. For the sake of simplicity, we chose 10 and shall use lowercase ‘x’ and lowercase ‘o’ to represent the players and an underscore ‘_’ to represent a blank space on the board.
If we represent our board as a 3x3 2D character matrix, like char board[3][3]; then we have to check each row, each column, and the diagonals to check if either of the players has gotten 3 in a row.
The value of this board is 10
Time Complexity: O(max(row,col))
Auxiliary Space: O(1)
The idea of this article is to understand how to write a simple evaluation function for the game Tic-Tac-Toe. In the next article we shall see how to combine this evaluation function with the minimax function. Stay Tuned.
This article is written by Akshay L. Aradhya.