I'm basically trying to understand the chronology/sequence of the algorithm and where should I call the scoring mechanism function that calculates the score of each move and returns an int. For example, this code is an implementation of the Minimax algorithm in the Tic-Tac-Toe game:
function bestMove() {
// AI to make its turn
let bestScore = -Infinity;
let move;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
// Is the spot available?
if (board[i][j] == '') {
board[i][j] = ai;
let score = minimax(board, 0, false);
board[i][j] = '';
if (score > bestScore) {
bestScore = score;
move = { i, j };
}
}
}
}
board[move.i][move.j] = ai;
currentPlayer = human;
}
let scores = {
X: 10,
O: -10,
tie: 0
};
function minimax(board, depth, isMaximizing) {
let result = checkWinner();
if (result !== null) {
return scores[result];
}
if (isMaximizing) {
let bestScore = -Infinity;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
// Is the spot available?
if (board[i][j] == '') {
board[i][j] = ai;
let score = minimax(board, depth + 1, false);
board[i][j] = '';
bestScore = max(score, bestScore);
}
}
}
return bestScore;
} else {
let bestScore = Infinity;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
// Is the spot available?
if (board[i][j] == '') {
board[i][j] = human;
let score = minimax(board, depth + 1, true);
board[i][j] = '';
bestScore = min(score, bestScore);
}
}
}
return bestScore;
}
}
The only implementation of the scoring mechanism is when the board identifies a win situation for one of the players. In complex games there are too many combinations so the depth will be restricted to let's say 3. the scoring mechanism needs to be called for each "move" that is being examined, otherwise we will get back 0 for each move, and the algorithm will just go for the next open spot, since the depth doesn't reach a possible wining series of moves.
If I planned a scoring mechanism function that compensate and "punishes" each move (for every i,j) minimax goes through and returns an int (the score), how should I fit it correctly in the algorithm?
It sounds simple but honestly nothing makes sense to me at this point...
P.S My game is pretty complex to explain and would take a long time, therefore, I'm looking for the more logic answer so I can implement it myself.
Thanks in advance, Omer