Professional Documents
Culture Documents
Data User Minimax
Data User Minimax
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
};
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?
1 of 2 09/06/2020 10:20
https://raw.githubusercontent.com/CodingTrain/...
if (board[i][j] == '') {
board[i][j] = human;
let score = minimax(board, depth + 1, true);
board[i][j] = '';
bestScore = min(score, bestScore);
}
}
}
return bestScore;
}
}
2 of 2 09/06/2020 10:20