Skip to content

Instantly share code, notes, and snippets.

@lesleh
Created May 20, 2016 11:46
Show Gist options
  • Save lesleh/3cb29222c0e14925fe6fd6da91e10f91 to your computer and use it in GitHub Desktop.
Save lesleh/3cb29222c0e14925fe6fd6da91e10f91 to your computer and use it in GitHub Desktop.
"use strict";
const GuessTheNumber = require('./guess_the_number');
module.exports = class GuessTheNumberSolver {
constructor() {
this.game = new GuessTheNumber();
}
solve() {
let min = this.game.min;
let max = this.game.max;
let guessCount = 0;
let guess = -1;
let response = -1;
do {
guess = this.bisect(min, max);
response = game.guessAnswer(guess);
++guessCount;
} while(response != 0);
console.log(`The answer is ${guess}! Found in ${guessCount} guesses.`)
}
bisect(min, max) {
min + Math.round(min - max / 2.0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment