Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

https://raw.githubusercontent.com/CodingTrain/...

// Tic Tac Toe AI with Minimax Algorithm


// The Coding Train / Daniel Shiffman
// https://thecodingtrain.com/CodingChallenges/154-tic-tac-toe-
minimax.html
// https://youtu.be/I64-UTORVfU
// https://editor.p5js.org/codingtrain/sketches/0zyUhZdJD

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?

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

You might also like