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

📅  最后修改于: 2021-09-24 04:57:48             🧑  作者: Mango

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

如上一篇文章所示,每个叶节点都有一个与之关联的值。我们已经将这个值存储在一个数组中。但在现实世界中,当我们创建一个程序来玩井字游戏、国际象棋、西洋双陆棋等时,我们需要实现一个函数,根据棋子在棋盘上的位置计算棋盘的价值。这个函数通常被称为评估函数。它有时也称为启发式函数。

对于每种类型的游戏,评估函数都是独一无二的。在这篇文章中,讨论了 Tic-Tac-Toe 游戏的评估函数。评估函数背后的基本思想是,如果轮到最大化者,则为棋盘提供高值,如果轮到最小者,则为棋盘提供低值。

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

让我们构建我们的评估函数:

  1. 如果 X 在棋盘上获胜,我们给它一个正值 +10。
    评估功能1
  2. 如果 O 在棋盘上获胜,我们给它一个负值 -10。
    评估功能2
  3. 如果没有人赢或比赛结果为平局,则我们给出 +0 的值。
    评估功能3

我们可以选择除 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 编写一个简单的评估函数。在下一篇文章中,我们将看到如何将这个评估函数与极大极小函数结合起来。敬请关注。

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程