博弈论中的极小极大算法第2组(评估功能简介)

2021年3月10日16:00:59 发表评论 840 次浏览

本文概述

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

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

评估功能对于每种类型的游戏都是唯一的。在这篇文章中, 讨论了井字游戏的评估功能。评估功能背后的基本思想是为董事会提供高价值, 如果最大化器轮到板子还是走低最小化器轮到你了。

对于这种情况, 让我们考虑 X作为最大化器和Ø作为最小化器.

让我们建立评估函数

如果X在棋盘上获胜, 我们给它正值+10。

Evaluation_function1

如果O在棋盘上获胜, 我们给它负值-10。

Evaluation_function2

如果没有人获胜或游戏导致平局, 那么我们给出+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<stdio.h>
#include<algorithm>
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

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

本文由...撰写阿克沙伊·阿拉迪亚。如果你喜欢lsbin并希望做出贡献, 那么你也可以写一篇文章并将你的文章邮寄到contribution@lsbin.org。查看你的文章出现在lsbin主页上, 并帮助其他Geeks。

如果发现任何不正确的地方, 或者你想分享有关上述主题的更多信息, 请发表评论

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: