function lookAhead() {
  var wins = [];
  var blocks = [];
  var empty = [];
  var nextTurn = invert(turn);
  for (var i = 0; i < 9; i++) {
    if (board[i] === EMPTY) {
      empty.push(i);
      var dreamBoard = board.slice();
      dreamBoard[i] = turn;
      var state = boardState(dreamBoard);
      if (state === turn) {
        // Winning move found.
        wins.push(i);
      }
      if (DIFFICULTY === 1) {
        dreamBoard = board.slice();
        dreamBoard[i] = nextTurn;
        var state = boardState(dreamBoard);
        if (state === nextTurn) {
          // Blocking move found.
          blocks.push(i);
        }
      }
    }
  }
  if (wins.length > 0) {
    return randomElement(wins);
  } else if (blocks.length > 0) {
    return randomElement(blocks);
  }
  return randomElement(empty);
}
// Look for a winning move, and optionally a blocking move.
Look for a winning move, and optionally a blocking move.
Look for a winning move and optionally a blocking move .
function lookAhead() {
  var wins = [];
  var blocks = [];
  var empty = [];
  var nextTurn = invert(turn);
  for (var i = 0; i < 9; i++) {
    if (board[i] === EMPTY) {
      empty.push(i);
      var dreamBoard = board.slice();
      dreamBoard[i] = turn;
      var state = boardState(dreamBoard);
      if (state === turn) {
        wins.push(i);
      }
      if (DIFFICULTY === 1) {
        dreamBoard = board.slice();
        dreamBoard[i] = nextTurn;
        var state = boardState(dreamBoard);
        if (state === nextTurn) {
          blocks.push(i);
        }
      }
    }
  }
  if (wins.length > 0) {
    return randomElement(wins);
  } else if (blocks.length > 0) {
    return randomElement(blocks);
  }
  return randomElement(empty);
}
function lookAhead ( ) { var wins = [ ] ; var blocks = [ ] ; var empty = [ ] ; var nextTurn = invert ( turn ) ; for ( var i = 0 ; i < 9 ; i ++ ) { if ( board [ i ] === EMPTY ) { empty . push ( i ) ; var dreamBoard = board . slice ( ) ; dreamBoard [ i ] = turn ; var state = boardState ( dreamBoard ) ; if ( state === turn ) { wins . push ( i ) ; } if ( DIFFICULTY === 1 ) { dreamBoard = board . slice ( ) ; dreamBoard [ i ] = nextTurn ; var state = boardState ( dreamBoard ) ; if ( state === nextTurn ) { blocks . push ( i ) ; } } } } if ( wins . length > 0 ) { return randomElement ( wins ) ; } else if ( blocks . length > 0 ) { return randomElement ( blocks ) ; } return randomElement ( empty ) ; }
Look for a winning move, and optionally a blocking move.
Look for a winning move and optionally a blocking move .
// Winning move found. // Blocking move found.