title: I'm working on a Minimax Algorithm for my reversi game(w/ react.js) but it gives RangeError
body: <p>I'm working on a Minimax Algorithm for my reversi game so that I'll have a strong AI opponent facing the player. But I bumped into this error: &quot;RangeError: Maximum call stack size exceeded&quot;</p>
  <p>What can I do to fix it?</p>
  <p>Here's the code(I won't explain it with pseudo code since my question is not about the function not working):</p>
  <p>AI Algorithm:</p>
  <pre><code>function minimaxAI(){
          const squares = history[history.length - 1].slice()
          let bestScore = -Infinity
          let bestMove = null
          // Perform minimax algorithm for each valid move and pick the best score
          const AI = (ourPlayer === &quot;white&quot;) ? &quot;black&quot; : &quot;white&quot;
          const AIValidMoves = checkSquaresForSides()[(AI === &quot;black&quot;) ? 0 : 1 ]
          console.log(AIValidMoves)
          for (var AIMove=0;AIMove&lt;AIValidMoves.length;AIMove++){
              const crds = AIValidMoves[AIMove].turned
              crds.unshift(AIValidMoves[AIMove].coordinates)
              const newBoard = handleMove(crds, squares)
              const score = minimax(newBoard,5,(AI === &quot;black&quot;) ? false : true)
              if (score &gt; bestScore) {
                  bestScore = score
                  bestMove = crds
              }
          }
          const handling = bestMove
          const upcomingAI = handleMove(handling)
          setHistory(upcomingAI)
          setStepNumber(upcomingAI.length - 1)
      }
  </code></pre>
  <p>Minimax Algorithm:</p>
  <pre><code>function minimax(board, depth, isMaximizing){
          const AI = (ourPlayer === &quot;white&quot;) ? &quot;BLACK&quot; : &quot;WHITE&quot;
          let result = setWinnerAndTurn(true)
          // Retrieve the filled squares of the current board
          let currentStones = 0
          history[history.length - 1].slice().map((row, y) =&gt;
              row.map((square, x) =&gt;
                  currentStones += (square) ? 1 : 0
              )
          );
          let newStones = 0
          // Retrieve the filled squares of the updated board
          board.map((row, y) =&gt;
              row.map((square, x) =&gt;
                  newStones += (square) ? 1 : 0
              )
          );
          console.log(currentStones)
          console.log(newStones)
          if (result.winner || newStones - currentStones === depth) {
              // Get the evaluated version of the last move
              let score = (result.winner === AI) ? Infinity : -Infinity
              return score
          }
          if (isMaximizing){
              // Play the optimal move for the opposing player
              let bestScore = -Infinity
              let bestMove = null
              const selection = (AI === &quot;BLACK&quot;) ? 0 : 1 
              const AIValidMoves = checkSquaresForSides()[selection]
              console.log(AIValidMoves)
              for (var AIMove=0;AIMove&lt;AIValidMoves.length;AIMove++){
                  const crds = AIValidMoves[AIMove].turned
                  crds.unshift(AIValidMoves[AIMove].coordinates)
                  const newBoard = handleMove(crds, board)
                  const score = minimax(newBoard,10,(selection === 0) ? false : true)
                  if (score &gt; bestScore) {
                      bestScore = score
                      bestMove = crds
                  }
              }
              return bestScore
          } else {
              // Play the optimal move for the maximizing player
              let bestScore = Infinity
              let bestMove = null
              const selection = (AI === &quot;BLACK&quot;) ? 1 : 0 
              const AIValidMoves = checkSquaresForSides()[selection]
              console.log(AIValidMoves)
              for (var AIMove=0;AIMove&lt;AIValidMoves.length;AIMove++){
                  const crds = AIValidMoves[AIMove].turned
                  crds.unshift(AIValidMoves[AIMove].coordinates)
                  const newBoard = handleMove(crds, board)
                  const score = minimax(newBoard,10,(selection === 1) ? false : true)
                  if (score &lt; bestScore) {
                      bestScore = score
                      bestMove = crds
                  }
              }
              return bestScore
          }
      }
  </code></pre>