Randomize Switch Case Order

https://medium.com/front-end-hacking/tic-tac-toe-javascript-game-b0cd6e98edd9#.x950xdtnd

I’m looking at his solution to the Tic Tac Toe challenge and the ‘AI’ is super easy to beat because it just plays the first available move… is grid space 1 is available…? play it,
if grid space 2 is available… ? play it… I thought the next level up of difficulty would be to at least randomize the order of the switch so that it’s at least starting it’s search for an available square isn’t just the first available square every time… So how would I randomize the switch statement each time before calling it? Or am I missing something in best practice for a better way to do this…?

function comp() {
  switch (true) {
    case $('#first').html() != game.user && $('#first').html() != game.computer:
      icon('first');
      break;
    case $('#second').html() !== game.user && $('#second').html() !== game.computer:
      icon('second');
      break;
    case $('#third').html() !== game.user && $('#third').html() !== game.computer:
      icon('third');
      break;
    case $('#fourth').html() !== game.user && $('#fourth').html() !== game.computer:
      icon('fourth');
      break;
    case $('#fifth').html() !== game.user && $('#fifth').html() !== game.computer:
      icon('fifth');
      break;
    case $('#sixth').html() !== game.user && $('#sixth').html() !== game.computer:
      icon('sixth');
      break;
    case $('#seventh').html() !== game.user && $('#seventh').html() !== game.computer:
      icon('seventh');
      break;
    case $('#eight').html() !== game.user && $('#eight').html() !== game.computer:
      icon('eight');
      break;
    case $('nineth').html() !== game.user && $('#nineth').html() !== game.computer:
      icon('nineth');
      break;
  }
};

I was playing around with a few variotions here if you want to fork a sample for explanation demo…
I’m calling a random number to generate the background… but that doesn’t seem like the way to go for the tic tac toe game to check where it should play…
http://codepen.io/TurtleWolf/pen/evvyPQ?editors=1011

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.