Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

103
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda