function cpuThink(board, who, level) {
  var bestScore = -Infinity;
  var bestX, bestY;
  var score;
  for (var x = 0; x < WIDTH; x++) {
    for (var y = 0; y < HEIGHT; y++) {
      var mySquare = board.getSquare(x, y);
      if (mySquare.bombs !== 0 && mySquare.owner !== who) {
        continue;
      }
      var clone = new Field(board);
      if (clone.simulate(x, y, who)) {
        // We win.
        return {score: Infinity, x: x, y: y};
      }
      if (level === 0) {
        score = clone.score();
        // Score: Positive means player 0 is winning, negative means player 1 is winning.
        // We want to find the best move for 'who'.
        if (who === 1) {
          score *= -1;
        }
        score += Math.random();  // Add some non-deterministic noise.
      } else {
        score = -cpuThink(clone, 1 - who, level - 1).score;
      }
      if (score >= bestScore) {
        bestScore = score;
        bestX = x;
        bestY = y;
      }
    }
  }
  return {score: bestScore, x: bestX, y: bestY};
}
// Run a min-max analysis for all possible moves.  Level controls the depth.
Run a min-max analysis for all possible moves.  Level controls the depth.
Run a min - max analysis for all possible moves . Level controls the depth .
function cpuThink(board, who, level) {
  var bestScore = -Infinity;
  var bestX, bestY;
  var score;
  for (var x = 0; x < WIDTH; x++) {
    for (var y = 0; y < HEIGHT; y++) {
      var mySquare = board.getSquare(x, y);
      if (mySquare.bombs !== 0 && mySquare.owner !== who) {
        continue;
      }
      var clone = new Field(board);
      if (clone.simulate(x, y, who)) {
        return {score: Infinity, x: x, y: y};
      }
      if (level === 0) {
        score = clone.score();
        if (who === 1) {
          score *= -1;
        }
        score += Math.random();  
      } else {
        score = -cpuThink(clone, 1 - who, level - 1).score;
      }
      if (score >= bestScore) {
        bestScore = score;
        bestX = x;
        bestY = y;
      }
    }
  }
  return {score: bestScore, x: bestX, y: bestY};
}
function cpuThink ( board , who , level ) { var bestScore = - Infinity ; var bestX , bestY ; var score ; for ( var x = 0 ; x < WIDTH ; x ++ ) { for ( var y = 0 ; y < HEIGHT ; y ++ ) { var mySquare = board . getSquare ( x , y ) ; if ( mySquare . bombs !== 0 && mySquare . owner !== who ) { continue ; } var clone = new Field ( board ) ; if ( clone . simulate ( x , y , who ) ) { return { score : Infinity , x : x , y : y } ; } if ( level === 0 ) { score = clone . score ( ) ; if ( who === 1 ) { score *= - 1 ; } score += Math . random ( ) ; } else { score = - cpuThink ( clone , 1 - who , level - 1 ) . score ; } if ( score >= bestScore ) { bestScore = score ; bestX = x ; bestY = y ; } } } return { score : bestScore , x : bestX , y : bestY } ; }
Run a min-max analysis for all possible moves.
Run a min - max analysis for all possible moves .
// We win. // Score: Positive means player 0 is winning, negative means player 1 is winning. // We want to find the best move for 'who'. // Add some non-deterministic noise.