function dream(x_o, board) {
  var state = boardState(board);
  if (state !== EMPTY) {
    return [-1, state];
  }
  var wins = [];
  var ties = [];
  var losses = [];
  for (var i = 0; i < 9; i++) {
    if (board[i] === EMPTY) {
      var dreamBoard = board.slice();
      dreamBoard[i] = x_o;
      var result = dream(invert(x_o), dreamBoard)[1];
      if (result === x_o) {
        wins.push(i);
      } else if (result === TIE) {
        ties.push(i);
      } else {
        losses.push(i);
      }
    }
  }
  if (wins.length > 0) {
    return [randomElement(wins), x_o];
  } else if (ties.length > 0) {
    return [randomElement(ties), TIE];
  }
  return [randomElement(losses), invert(x_o)];
}
// Return a tuple: where to move, and expected result.
Return a tuple: where to move, and expected result.
Return a tuple : where to move and expected result .
function dream(x_o, board) {
  var state = boardState(board);
  if (state !== EMPTY) {
    return [-1, state];
  }
  var wins = [];
  var ties = [];
  var losses = [];
  for (var i = 0; i < 9; i++) {
    if (board[i] === EMPTY) {
      var dreamBoard = board.slice();
      dreamBoard[i] = x_o;
      var result = dream(invert(x_o), dreamBoard)[1];
      if (result === x_o) {
        wins.push(i);
      } else if (result === TIE) {
        ties.push(i);
      } else {
        losses.push(i);
      }
    }
  }
  if (wins.length > 0) {
    return [randomElement(wins), x_o];
  } else if (ties.length > 0) {
    return [randomElement(ties), TIE];
  }
  return [randomElement(losses), invert(x_o)];
}
function dream ( x_o , board ) { var state = boardState ( board ) ; if ( state !== EMPTY ) { return [ - 1 , state ] ; } var wins = [ ] ; var ties = [ ] ; var losses = [ ] ; for ( var i = 0 ; i < 9 ; i ++ ) { if ( board [ i ] === EMPTY ) { var dreamBoard = board . slice ( ) ; dreamBoard [ i ] = x_o ; var result = dream ( invert ( x_o ) , dreamBoard ) [ 1 ] ; if ( result === x_o ) { wins . push ( i ) ; } else if ( result === TIE ) { ties . push ( i ) ; } else { losses . push ( i ) ; } } } if ( wins . length > 0 ) { return [ randomElement ( wins ) , x_o ] ; } else if ( ties . length > 0 ) { return [ randomElement ( ties ) , TIE ] ; } return [ randomElement ( losses ) , invert ( x_o ) ] ; }
Return a tuple: where to move, and expected result.
Return a tuple : where to move and expected result .