Coding Challenge 154: Minimax Algorithm for Tic Tac Toe
Why Stack Overflow recommends it
Stack Overflow users link it when asking about or debugging their own Minimax Tic Tac Toe AI, often after following along in JavaScript or translating it to another language such as C#.
Recommended by 2 people across 2 different months, 2019 to 2021.
What it is
Daniel Shiffman of The Coding Train adds a computer opponent to his p5.js Tic Tac Toe game by implementing the Minimax algorithm. He explains the idea, codes it live with recursive max and min scoring, fixes bugs, and refactors with min() and max().
What people said
Recommends Låne Book · 2020-06 · Stack Overflow · on “MiniMax TicTacToe AI”
I'm trying to implement MiniMax to create a TicTacToe AI. I am using C# and Unity. I was following CodingTrain's video [this video] . The tutorial is in JavaScript, but I followed along in C#. I've tried looking at the MiniMax code from his website, but I can't figure out why my "translation" doesn't work: using System.Collections.Generic; using UnityEngine; public class GameManager : MonoBehaviour { public SpriteRenderer[] sprites; char[] board = new char[] { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }; Dictionary<char, Color> boardColor = new Dictionary<char, Color>() { { ' ', Color.white }, { 'O', Color.red }, { 'X', Color.blue } }; char ai = 'O'; char player = 'X'; Camera cam; bool gameOver
Recommends acchang · 2021-01 · Stack Overflow · on “Can you help me understand a few aspects of this tic-tac-toe minimax code? (Specifics within)”
I’m trying to teach myself minimax in javscript from the code here: [LINK] And video: [LINK] This is the function: function minimax(newBoard, player) { var availSpots = emptySquares(); if (checkWin(newBoard, huPlayer)) { return {score: -10}; } else if (checkWin(newBoard, aiPlayer)) { return {score: 10}; } else if (availSpots.length === 0) { return {score: 0}; } var moves = []; for (var i = 0; i < availSpots.length; i++) { var move = {}; move.index = newBoard[availSpots[i]]; newBoard[availSpots[i]] = player; if (player == aiPlayer) { var result = minimax(newBoard, huPlayer); move.score = result.score; } else { var result = minimax(newBoard, aiPlayer); move.score = result.score; } newBoard[ava
Cites Harold Kwan · 2019-12 · Stack Overflow · on “A nested array is defined in one line, then undefined right after (javascript)”
So I am kind of new to the minimax algorithm. I know what it is supposed to do and how it works, but I am struggling on the implementation of the algorithm on a game of tic tac toe. I followed through the video of codingtrain writing the algorithm, and also referenced his original code. However, when i finish calling the minimax function within the minimax function, i cannot revert the board. The following code is my minimax function. function minimax(board, depth, isMaximizing) { let result = checkWinner(); if (result != null) { // if the game is over return the scores and no more minimax needed return scores[result]; } if (isMaximizing) { let bestScore = -Infinity; let move; for (i = 0; i