Last active
November 1, 2022 13:04
-
-
Save kartikkukreja/4fe91a19c25b25f11c88 to your computer and use it in GitHub Desktop.
Tic Tac Toe Heuristic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define Possible_Wins 8 | |
const int Three_in_a_Row[Possible_Wins][3] = { | |
{ 0, 1, 2 }, | |
{ 3, 4, 5 }, | |
{ 6, 7, 8 }, | |
{ 0, 3, 6 }, | |
{ 1, 4, 7 }, | |
{ 2, 5, 8 }, | |
{ 0, 4, 8 }, | |
{ 2, 4, 6 } | |
}; | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment