0% found this document useful (0 votes)
429 views1 page

Heuristic Function For Tic-Tac-Toe Game

This document defines a heuristic function to evaluate positions in a Tic-Tac-Toe game. It defines 8 possible ways to win as rows, columns or diagonals. It also defines a heuristic evaluation array to score positions based on having 1, 2, or 3 pieces in a row. The evaluatePosition function takes the game board, loops through the 8 win conditions, counts pieces for each player, references the heuristic array to score that position, and returns the total score.

Uploaded by

RDxR10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
429 views1 page

Heuristic Function For Tic-Tac-Toe Game

This document defines a heuristic function to evaluate positions in a Tic-Tac-Toe game. It defines 8 possible ways to win as rows, columns or diagonals. It also defines a heuristic evaluation array to score positions based on having 1, 2, or 3 pieces in a row. The evaluatePosition function takes the game board, loops through the 8 win conditions, counts pieces for each player, references the heuristic array to score that position, and returns the total score.

Uploaded by

RDxR10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 1

TEAM - III

Heuristic Function Evaluation For Tic - Tac - Toe Game

#define Possible_Wins 8 // 3 rows, 3 columns, 2 diagonals(Win can be in any


manner)
const int Three_in_a_Row[Possible_Wins][3] = {
{ 0, 1, 2 },
{ 3, 4, 5 },
{ 6, 7, 8 },
{ 0, 3, 6 }, //taking transpose of the first 3 arrays considering a matrix.
{ 1, 4, 7 },
{ 2, 5, 8 },
{ 0, 4, 8 }, //Diagonal 1
{ 2, 4, 6 } //Diagonal 2
};
// Scores for heuristic evaluation fn. :
// No move = 0
// One piece = 10 points.
// Two pieces in a row/column/diagonal = 100 points.
// Three pieces in a row/column/diagonal = 1000 points.

const int Heuristic_Array[4][4] = {


{ 0, -10, -100, -1000 },
{ 10, 0, 0, 0 },
{ 100, 0, 0, 0 },
{ 1000, 0, 0, 0 }
};

int evaluatePosition(char board[9], char player) {


char opponent = (player == 'X') ? 'O' : 'X', piece;
int players, others, t = 0, i, j;

for (i = 0; i < 8; i++) {


players = others = 0;
for (j = 0; j < 3; j++) {
piece = board[Three_in_a_Row[i][j]];
if (piece == player)
players++;
else if (piece == opponent)
others++;
}
t += Heuristic_Array[players][others];
}
return t;
}

You might also like