📌  相关文章
📜  博弈论中的极小极大算法第2组(评估函数简介)

📅  最后修改于: 2021-04-29 13:32:36             🧑  作者: Mango

先决条件:博弈论中的极小极大算法

从上面的文章中可以看出,每个叶节点都有一个与之关联的值。我们已经将该值存储在数组中。但是在现实世界中,当我们创建一个程序来玩井字游戏,国际象棋,步步高等游戏时,我们需要实现一个功能,该函数根据棋子在棋盘上的放置来计算棋盘的价值。此函数通常称为评估函数。有时也称为启发式函数。

评估函数对于每种类型的游戏都是唯一的。在这篇文章中,讨论了井字游戏的评估函数。评估函数背后的基本思想是,如果最大化器的转向,则为该板提供较高的价值,而如果最小化器的转向,则为该板提供较低的价值。

对于这种情况,我们将X视为最大化器,将O视为最小化器

让我们建立评估函数:

  1. 如果X在棋盘上获胜,我们给它一个+10的正值。
    Evaluation_function1
  2. 如果O在棋盘上获胜,我们给它负值-10。
    Evaluation_function2
  3. 如果没有人赢得比赛,或者游戏导致平局,那么我们给出+0的值。
    Evaluation_function3

我们可以选择除10之外的任何正/负值。为简单起见,为简单起见,我们选择10,我们将使用小写字母“ x”和小写字母“ o”代表玩家,并使用下划线“ _”代表板上的空白。

如果我们将板表示为3×3 2D字符矩阵,例如char board [3] [3];那么我们必须检查每一行,每一列和对角线,以检查是否有一个玩家连续获得3个。

C++
// C++ program to compute evaluation function for
// Tic Tac Toe Game.
#include
#include
using namespace std;
  
// Returns a value based on who is winning
// b[3][3] is the Tic-Tac-Toe board
int evaluate(char b[3][3])
{
    // Checking for Rows for X or O victory.
    for (int row = 0; row<3; row++)
    {
        if (b[row][0]==b[row][1] && b[row][1]==b[row][2])
        {
            if (b[row][0]=='x')
               return +10;
            else if (b[row][0]=='o')
               return -10;
        }
    }
  
    // Checking for Columns for X or O victory.
    for (int col = 0; col<3; col++)
    {
        if (b[0][col]==b[1][col] && b[1][col]==b[2][col])
        {
            if (b[0][col]=='x')
                return +10;
            else if (b[0][col]=='o')
                return -10;
        }
    }
  
    // Checking for Diagonals for X or O victory.
    if (b[0][0]==b[1][1] && b[1][1]==b[2][2])
    {
        if (b[0][0]=='x')
            return +10;
        else if (b[0][0]=='o')
            return -10;
    }
    if (b[0][2]==b[1][1] && b[1][1]==b[2][0])
    {
        if (b[0][2]=='x')
            return +10;
        else if (b[0][2]=='o')
            return -10;
    }
  
    // Else if none of them have won then return 0
    return 0;
}
  
// Driver code
int main()
{
    char board[3][3] =
    {
        { 'x', '_', 'o'},
        { '_', 'x', 'o'},
        { '_', '_', 'x'}
    };
  
    int value = evaluate(board);
    printf("The value of this board is %d\n", value);
    return 0;
}


Java
// Java program to compute evaluation function for
// Tic Tac Toe Game.
  
class GFG
{
  
// Returns a value based on who is winning
// b[3][3] is the Tic-Tac-Toe board
static int evaluate(char b[][])
{
    // Checking for Rows for X or O victory.
    for (int row = 0; row < 3; row++)
    {
        if (b[row][0] == b[row][1] && b[row][1] == b[row][2])
        {
            if (b[row][0] == 'x')
            return +10;
            else if (b[row][0] == 'o')
            return -10;
        }
    }
  
    // Checking for Columns for X or O victory.
    for (int col = 0; col < 3; col++)
    {
        if (b[0][col] == b[1][col] && b[1][col] == b[2][col])
        {
            if (b[0][col] == 'x')
                return +10;
            else if (b[0][col] == 'o')
                return -10;
        }
    }
  
    // Checking for Diagonals for X or O victory.
    if (b[0][0] == b[1][1] && b[1][1] == b[2][2])
    {
        if (b[0][0] == 'x')
            return +10;
        else if (b[0][0] == 'o')
            return -10;
    }
    if (b[0][2] == b[1][1] && b[1][1] == b[2][0])
    {
        if (b[0][2] == 'x')
            return +10;
        else if (b[0][2] == 'o')
            return -10;
    }
  
    // Else if none of them have won then return 0
    return 0;
}
  
// Driver code
public static void main(String[] args)
{
    char board[][] =
    {
        { 'x', '_', 'o'},
        { '_', 'x', 'o'},
        { '_', '_', 'x'}
    };
  
    int value = evaluate(board);
    System.out.printf("The value of this board is %d\n", value);
}
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 program to compute evaluation 
# function for Tic Tac Toe Game. 
  
# Returns a value based on who is winning 
# b[3][3] is the Tic-Tac-Toe board 
def evaluate(b): 
   
    # Checking for Rows for X or O victory. 
    for row in range(0, 3): 
       
        if b[row][0] == b[row][1] and b[row][1] == b[row][2]: 
           
            if b[row][0] == 'x':
                return 10 
            elif b[row][0] == 'o': 
                return -10 
  
    # Checking for Columns for X or O victory. 
    for col in range(0, 3): 
       
        if b[0][col] == b[1][col] and b[1][col] == b[2][col]: 
           
            if b[0][col]=='x':
                return 10 
            elif b[0][col] == 'o': 
                return -10 
  
    # Checking for Diagonals for X or O victory. 
    if b[0][0] == b[1][1] and b[1][1] == b[2][2]: 
       
        if b[0][0] == 'x': 
            return 10 
        elif b[0][0] == 'o': 
            return -10 
       
    if b[0][2] == b[1][1] and b[1][1] == b[2][0]: 
       
        if b[0][2] == 'x': 
            return 10 
        elif b[0][2] == 'o': 
            return -10 
       
    # Else if none of them have won then return 0 
    return 0 
   
# Driver code 
if __name__ == "__main__": 
   
    board = [['x', '_', 'o'], 
             ['_', 'x', 'o'], 
             ['_', '_', 'x']] 
       
    value = evaluate(board) 
    print("The value of this board is", value) 
  
# This code is contributed by Rituraj Jain


C#
// C# program to compute evaluation function for
// Tic Tac Toe Game.
using System;
  
class GFG
{
  
// Returns a value based on who is winning
// b[3,3] is the Tic-Tac-Toe board
static int evaluate(char [,]b)
{
    // Checking for Rows for X or O victory.
    for (int row = 0; row < 3; row++)
    {
        if (b[row, 0] == b[row, 1] && b[row, 1] == b[row, 2])
        {
            if (b[row, 0] == 'x')
            return +10;
            else if (b[row, 0] == 'o')
            return -10;
        }
    }
  
    // Checking for Columns for X or O victory.
    for (int col = 0; col < 3; col++)
    {
        if (b[0, col] == b[1, col] && b[1, col] == b[2, col])
        {
            if (b[0, col] == 'x')
                return +10;
            else if (b[0, col] == 'o')
                return -10;
        }
    }
  
    // Checking for Diagonals for X or O victory.
    if (b[0, 0] == b[1, 1] && b[1, 1] == b[2, 2])
    {
        if (b[0, 0] == 'x')
            return +10;
        else if (b[0, 0] == 'o')
            return -10;
    }
    if (b[0, 2] == b[1, 1] && b[1, 1] == b[2, 0])
    {
        if (b[0, 2] == 'x')
            return +10;
        else if (b[0, 2] == 'o')
            return -10;
    }
  
    // Else if none of them have won then return 0
    return 0;
}
  
// Driver code
public static void Main(String[] args)
{
    char [,]board =
    {
        { 'x', '_', 'o'},
        { '_', 'x', 'o'},
        { '_', '_', 'x'}
    };
  
    int value = evaluate(board);
    Console.Write("The value of this board is {0}\n", value);
}
}
  
// This code is contributed by Rajput-Ji


输出 :

The value of this board is 10

本文的目的是了解如何为Tic-Tac-Toe游戏编写简单的评估函数。在下一篇文章中,我们将看到如何将此评估函数与minimax函数结合使用。敬请关注。