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, Backgamon, 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 Heuristic Function.
The evaluation function is unique for every type of game. In this post, 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 maximizer‘s turn or a low value for the board if minimizer‘s turn.
For this scenario let us consider X as the maximizer and O as the minimizer.
Let us build our evaluation function :
- If X wins on the board we give it a positive value of +10.
- If O wins on the board we give it a negative value of -10.
- If no one has won or the game results in a draw then we give a value of +0.
We could have chosen any positive / negative value other than 10. For the sake of simplicity we chose 10 for the sake of simplicity we shall use lower case ‘x’ and lower case ‘o’ to represent the players and an underscore ‘_’ to represent a blank space on the board.
If we represent our board as a 3×3 2D character matrix, like char board; then we have to check each row, each column and the diagonals to check if either of the players have gotten 3 in a row.
The value of this board is 10
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. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimax Algorithm in Game Theory | Set 1 (Introduction)
- Minimax Algorithm in Game Theory | Set 5 (Zobrist Hashing)
- Minimax Algorithm in Game Theory | Set 3 (Tic-Tac-Toe AI - Finding optimal move)
- Minimax Algorithm in Game Theory | Set 4 (Alpha-Beta Pruning)
- Game Theory (Normal - form game) | Set 1 (Introduction)
- Expectimax Algorithm in Game Theory
- Combinatorial Game Theory | Set 1 (Introduction)
- Game Theory (Normal-form Game) | Set 6 (Graphical Method [2 X N] Game)
- Game Theory (Normal-form Game) | Set 7 (Graphical Method [M X 2] Game)
- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
- Game Theory (Normal-form Game) | Set 5 (Dominance Property-Mixed Strategy)
- Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)
- Combinatorial Game Theory | Set 2 (Game of Nim)
- The prisoner's dilemma in Game theory
- Pareto Optimality and its application in Game Theory
- Combinatorial Game Theory | Set 3 (Grundy Numbers/Nimbers and Mex)
- Combinatorial Game Theory | Set 4 (Sprague - Grundy Theorem)
- Game Theory in Balanced Ternary Numeral System | (Moving 3k steps at a time)
- Game Development with Unity | Introduction
- Variation in Nim Game