Skip to content

Instantly share code, notes, and snippets.

@kartikkukreja
Last active November 1, 2022 13:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kartikkukreja/4fe91a19c25b25f11c88 to your computer and use it in GitHub Desktop.
Save kartikkukreja/4fe91a19c25b25f11c88 to your computer and use it in GitHub Desktop.
Tic Tac Toe Heuristic
#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