# 博弈论中的极小极大算法第2组（评估功能简介）

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

## 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``