-1

I need to make my tic tac toe AI unbeatable .

The computer AI 'O' should choose the best move.

I start by using a random number to create my first AI for the game but now the problem is to make it unbeatable. I found something call minimax algorithm but when I use it in my JavaScript code the minimax algorithm worked but it take a while to place the O and it is not choosing the best move, so what do i have to add or change to make the computer choose automatically the best move . If someone can help me find the answer is really going to help me thanks.

This is my JavaScript code:

  



 
  
 td { 
 
 height: 100px; 

 width: 100px; 
 
   font-size: 20px; 
 
  text-align: center; 
   } 
 

    
  
  <body onload="startGame();"> 
 
 

 
 <div id="message">sss</div> 
 

 
 <table class="squares"  border = "1"> 
 
 <tr> 
 
  <td class="square" id="s1" onClick="nextMove(this)"></td> 
 
  <td class="square" id="s2" onClick="nextMove(this)"></td> 
 
  <td class="square" id="s3" onClick="nextMove(this)"></td> 
  </tr> 
  <tr> 
 
   <td class="square" id="s4" onClick="nextMove(this)"></td> 
 
   <td class="square" id="s5" onClick="nextMove(this)"></td> 
 
    <td class="square" id="s6" onClick="nextMove(this)"></td> 
 
    </tr> 
 
    <tr> 
 
    <td class="square" id="s7" onClick="nextMove(this)"></td> 
 
     <td class="square" id="s8" onClick="nextMove(this)"></td> 
 
      <td class="square" id="s9" onClick="nextMove(this)"></td> 

       </tr> 
       </div>
       </table> 

        

It should choose all of the best move and never lose.

  • Please i am asking kindly to please take a moment to help me with this code , i known that i taking your time but please help me I really need help Thank you – Kenneth Gandonou Oct 25 '19 at 19:58
  • Do you not understand what minimax needs to do? Do you not understand why your code doesn't do that? Both? – Luke Persola Oct 25 '19 at 19:59
  • yes I do not understand why my code doesn't do that – Kenneth Gandonou Oct 25 '19 at 20:01
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [Minimal, complete, verifiable example](https://stackoverflow.com/help/minimal-reproducible-example) applies here. We cannot effectively help you until you post your MCVE code and accurately specify the problem. We should be able to paste your posted code into a text file and reproduce the problem you specified. – Prune Oct 25 '19 at 22:07
  • Your "Run code snippet" button produces no output, and you haven't specified the problem ("it doesn't work" is not a specification). See this lovely [debug](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) blog for help. You should post the results of your debugging attempt, especially a trace of the operation and some insight as to where the program deviates from yoru expectations. – Prune Oct 25 '19 at 22:13

1 Answers1

1

I don't have a solution to minimax, but I would recommend putting this into the eslint demo to help pick out possible errors.

ESLint Demo

That will help see things that would be obviously wrong. Here are some things I saw on a quick look through:

  1. origboard needs to be uppercased in startGame to match your variable up top
  2. The empty variable in the empty function needs be re-named.
  3. In general, you should use === to compare instead of == MDN comparison docs
  4. else if(turn = human) in switchTurn should most likely not be a single =. Assignment shouldn't happen in an if check. I am betting you meant that to be a comparison.
  5. I don't see gameOver being used. That might cause it to run forever unless there is code that wasn't pasted into the question.

Hope that helps! I am guessing there are some more things that need to be updated but eslint can help find more of those.

Special Character
  • 2,321
  • 4
  • 25
  • 34