Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!