0% found this document useful (0 votes)
96 views2 pages

Data User Minimax

This document describes an AI for Tic Tac Toe that uses the minimax algorithm to evaluate all possible future board states and choose the move that maximizes its chance of winning. It defines a bestMove function that tries all empty spots on the board and evaluates each using minimax before choosing the one with the highest score. The minimax function recursively evaluates boards to a certain depth, alternating between maximizing and minimizing the assumed player's score at each level, until it reaches a winner and returns that player's predefined score.

Uploaded by

TAKASHI BOOKS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views2 pages

Data User Minimax

This document describes an AI for Tic Tac Toe that uses the minimax algorithm to evaluate all possible future board states and choose the move that maximizes its chance of winning. It defines a bestMove function that tries all empty spots on the board and evaluates each using minimax before choosing the one with the highest score. The minimax function recursively evaluates boards to a certain depth, alternating between maximizing and minimizing the assumed player's score at each level, until it reaches a winner and returns that player's predefined score.

Uploaded by

TAKASHI BOOKS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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