Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

105
Visualizações
Test if Javascript Tic Tac Toe Game with mini max algorithm working correctly

I have created a Tic Tac Toe Game that allows the user to play against the computer. The computer is supposed to implement the mini max algorithm where it chooses the best move to make in relation to the human user, whether it be to prevent the player from winning, or winning itself. The problem is, I can't tell if the program is working correctly as sometimes it appears to work and other times it doesn't. The code snippet below is the mini max function that I added to my code. Here is the link to the full code https://github.com/1reemy/tictactoe.

    const minimax = (newBoard, user) => {
        let legitMove = emptySquares();
        let moves = [];
        const gameWinner = (user) => {
            let win = winner.find((combo)=>combo.every((idx) => sector[idx].textContent === user));
            if(user === player.humanPlayer){
                win;
            }else if(user === player.machine()){
                win;
            }else if(boardFull()){
                return;
            }
            return user;
         };
        if(gameWinner(player.humanPlayer)){
            return {score:-10};
        }else if(gameWinner(player.machine())){
            return {score:10};
        }
        for(let i = 0; i < legitMove.length; i++){       
          let move = {};
          move.index = newBoard[legitMove[i]];
          newBoard[legitMove[i]] = user;
          if(user === player.machine()){
          let result = minimax(newBoard,player.humanPlayer);
          move.score = result.score;
          }
          else{
              let result = minimax(newBoard, player.machine());
          move.score = result.score;
              }
          newBoard[legitMove[i]] = move.index;
          moves.push(move);
        }
        let bestMove;
        if(user === player.machine()){
          let bestScore = -10000;
          for(let i = 0; i < moves.length; i++){
          if(moves[i].score > bestScore){
            bestScore = moves[i].score;
            bestMove = i;
          }
          }
        }
        else{
          let bestScore = 10000;
          for(let i = 0; i < moves.length; i++){
          if(moves[i].score < bestScore){
             bestScore = moves[i].score;
             bestMove = i;
          }
          }
        }
        return moves[bestMove];
      }
    
    
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda