title: MinMax Algorithm Hex Game Javascript
body: <p>I'm trying to implement minimax algorithm on hex game, but the function never stops looping and I cant seem to tell exactly what the problem is, pls help me find the bug.</p>
  <pre><code>function initializeBoard() {
      let n = 0;
      board = (Array(boardSize)).fill().map(() =&gt; Array(boardSize).fill(0));
      for (let i = 0; i &lt; boardSize; i++){
          for (let j= 0; j &lt; boardSize; j++) {
              board[i][j] = n++;
          }
      }
      emptyCells = Array.from(Array(boardSize * boardSize).keys());
      hBoard = Array.from(Array(boardSize * boardSize).keys());
  }
  
  function turn(squareId, p, row, col) {
      board[row][col] = p.character;
      hBoard[squareId] = p.character;
      storeCells(squareId);
      let colorClass = document.createElement(&quot;div&quot;);
      colorClass.setAttribute(&quot;class&quot;, p.color);
      document.getElementById(squareId).appendChild(colorClass);
      let gameDecision = checkWin(board, p);
      if (gameDecision != 0) {
          updatePlayerWin(gameDecision);
          gameOver = true; 
      } 
  }
  </code></pre>
  <p>I am using helperBoard because I don't know how to get randomizeMoves with 2D array or only get empty cells from it.</p>
  <pre><code>    // minmax still in process
  
      function botLevel_3(newBoard, helperBoard, player, currentPlayer, opponentPlayer){
  
      var availSpots = helperBoard.filter(s =&gt; typeof s == 'number'); 
  
      if (checkWin(newBoard, opponentPlayer) == opponentPlayer.character){
          return {score: -10};
      }
      else if (checkWin(newBoard, currentPlayer) == currentPlayer.character){
          return {score : 10};
      }
      var moves = [];
      while (availSpots.length != 0){
          var currentMove = {};
  
          var randMove = randomizeMove(availSpots);
          var index = availSpots.splice(randMove, 1);
          let row = Math.floor(index / 9);
          let col = index % 9;
         
          currentMove.index = newBoard[row][col];
          newBoard[row][col] = player;
          helperBoard[index] = player;
  
          if (player == currentPlayer.character) {
              if (checkWin(newBoard, currentPlayer) == currentPlayer.character) {
                currentMove.score = 10;
                newBoard[row][col] = currentMove.index;
                helperBoard[index] = currentMove.index;
                return currentMove;
              }
              var result = botLevel_3(newBoard, helperBoard, opponentPlayer.character,  currentPlayer, opponentPlayer);
              currentMove.score = result.score;
            } 
            else {
              var result = botLevel_3(newBoard, helperBoard, currentPlayer.character, currentPlayer, opponentPlayer);
              currentMove.score = result.score;
          }
  
          newBoard[row][col] = currentMove.index;
          helperBoard[index] = currentMove.index;
          moves.push(currentMove);
  
      }
  
      // maximize
      var bestMove;
      if (player == currentPlayer.character){
          var bestScore = -10000;
          for (var i = 0; i &lt; moves.length; i++){
              if (moves[i].score &gt; bestScore){
                  bestScore = moves[i].score;
                  bestMove = i;
              }
          }
      }
      //minimize
      else {
          var bestScore = 10000;
          for (var i = 0; i &lt; moves.length; i++){
              if (moves[i].score &lt; bestScore){
                  bestScore = moves[i].score;
                  bestMove = i;
              }
          }
      }
      return moves[bestMove];
  }
  </code></pre>
  <p>source code == <a href="https://github.com/alvin-2002/Hex-Board-Game" rel="nofollow noreferrer">https://github.com/alvin-2002/Hex-Board-Game</a></p>
  <p>Thanks</p>