I have become frustrated with trying to learn minimax. I’ve read many, many, many links and worked on this for days. My problem is with understanding other’s different implementations, understanding how they connect logically (they all seem to do it different ways), and the commands and structures they use for it. I understand theoretically how it works, implementation is the problem. I want to code a basic implementation and tweak it/review it and learn from that. I.E. player 1 (x) will be human, and player 2 (o) will be the computer. If i can get it functional I can understand it and then move on to making which player is which irrelevant, add “clues” button, etc.
My implementation comes from https://www.geeksforgeeks.org/minimax-algorithm-in-game-theory-set-3-tic-tac-toe-ai-finding-optimal-move/ which is the simplest explanation I’ve found.
My codepen is https://codepen.io/tylerOtj/pen/qpyKJr?editors=1112
Everything is thus far done in console. The source code (geeks for geeks) says minimax will play for x, however on board 0 it won’t block OR win, and on other tested boards it makes moves not optimal.
Things I’ve tried:
-swapping computer and player (o->x, x->o)
-setting isMax in minimax call (in findBestMove) to true\false for both combinations of computer\player x\o
-Alert statements on every change in bestVal, board, setMove to check correct cell assignment, board updating, value assignment
-Noted that “new best val” statement in findBestMove function is only called once, sometimes twice (on an empty or 1-celled board shouldn’t it be called more?)
Like I said, my goal is to really understand this. Even if you don’t give me the answer a hint in the right direction, a behavior to look at, anything would be greatly appreciated.