title: When making a tic tac toe game in javascript, how can you create an A.I. that will select any random box, but one that hasn't been chosen?
body: <p>I'm making a tic-tac-toe game, and I'm stuck. I sort of made an A.I. that moves after you but it's all a bit messed up. Try it yourself and see what happens. Can anybody have a look and see if they're able to improve it and explain how they did it? And to make things simple, how could I make the A.I. choose any box which hasn't been chosen yet.
  Here's the code:</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;body&gt;
      &lt;input type=&quot;button&quot; id=&quot;k1&quot; value=&quot;  &quot; onclick=&quot;tictactoe(this)&quot;&gt;
      &lt;input type=&quot;button&quot; id=&quot;k2&quot; value=&quot;  &quot; onclick=&quot;tictactoe(this)&quot;&gt;
      &lt;input type=&quot;button&quot; id=&quot;k3&quot; value=&quot;  &quot; onclick=&quot;tictactoe(this)&quot;&gt;
      &lt;br /&gt;
      &lt;input type=&quot;button&quot; id=&quot;k4&quot; value=&quot;  &quot; onclick=&quot;tictactoe(this)&quot;&gt;
      &lt;input type=&quot;button&quot; id=&quot;k5&quot; value=&quot;  &quot; onclick=&quot;tictactoe(this)&quot;&gt;
      &lt;input type=&quot;button&quot; id=&quot;k6&quot; value=&quot;  &quot; onclick=&quot;tictactoe(this)&quot;&gt;
      &lt;br /&gt;
      &lt;input type=&quot;button&quot; id=&quot;k7&quot; value=&quot;  &quot; onclick=&quot;tictactoe(this)&quot;&gt;
      &lt;input type=&quot;button&quot; id=&quot;k8&quot; value=&quot;  &quot; onclick=&quot;tictactoe(this)&quot;&gt;
      &lt;input type=&quot;button&quot; id=&quot;k9&quot; value=&quot;  &quot; onclick=&quot;tictactoe(this)&quot;&gt;
      &lt;script&gt;
          var Xturn = true;
          var nummoves = 0;
          var cat;
          function tictactoe(square) {
              var value = square.value;
              var doc1 = document.getElementById(&quot;k1&quot;).value;
              var doc2 = document.getElementById(&quot;k2&quot;).value;
              var doc3 = document.getElementById(&quot;k3&quot;).value;
              var doc4 = document.getElementById(&quot;k4&quot;).value;
              var doc5 = document.getElementById(&quot;k5&quot;).value;
              var doc6 = document.getElementById(&quot;k6&quot;).value;
              var doc7 = document.getElementById(&quot;k7&quot;).value;
              var doc8 = document.getElementById(&quot;k8&quot;).value;
              var doc9 = document.getElementById(&quot;k9&quot;).value;
  
              for (nummoves = 0; nummoves &lt; 2; nummoves++) {
  
                  if (doc1 == &quot;X&quot;) {
                      cat = document.getElementById(&quot;k2&quot;).value = &quot;O&quot;;
                      Xturn = true;
                  }
  
                  if (doc2 = &quot;X&quot;) {
                      cat = document.getElementById(&quot;k4&quot;).value = &quot;O&quot;;
                      Xturn = true;
                  }
  
                  if (doc3 == &quot;X&quot;) {
                      cat = document.getElementById(&quot;k5&quot;).value = &quot;O&quot;;
                      Xturn = true;
                  }
  
                  if (doc4 == &quot;X&quot;) {
                      car = document.getElementById(&quot;k9&quot;).value = &quot;O&quot;;
                  }
              }
  
              for (nummoves = 2; nummoves &lt; 3; nummoves++) {
  
                  if (doc1 == &quot;X&quot;) {
                      cat = document.getElementById(&quot;k7&quot;).value = &quot;O&quot;;
                      Xturn = true;
                  }
  
              }
  
              if (value != &quot;X&quot; &amp;&amp; value != &quot;O&quot;) {
                  if (Xturn == true) {
                      square.value = &quot;X&quot;;
                      return Xturn = false;
                      nummoves++;
                  } else if (Xturn == false) {
                      square.value = &quot;O&quot;;
                      return Xturn = true;
                      nummoves++;
                  }
              } else {
                  alert(&quot;That square has been clicked.&quot;);
              }
          }
      &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>Note the whole concept isn't mine I admit, but i did kind of it the A.O. part which is slightly messed up.</p>