-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproof-of-concept.js
51 lines (38 loc) · 1.46 KB
/
proof-of-concept.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict';
/**
* A proof of concept script showing the bot logic
* that guesses a valid random bulls and cows number
*
* The script is configured to use only 4 digit numbers,
* from 1023 to 9876
*/
const GameEngine = require('./lib/GameEngine');
// Random number to be guessed
const secret = GameEngine.pickRandom();
// A set of possible numbers containing the secret number
let set = GameEngine.getDefaultSet();
// Number of attempts at guessing the corrent number
let attempt = 0;
let guess, bulls, cows, score;
console.log('-------------------------------------------------');
console.log(`Secret number to guess: ${secret}`);
console.log('-------------------------------------------------');
while (true) {
// get next guess from set of possiblities
guess = GameEngine.pickRandom(set);
attempt++;
console.log(`Attempt ${attempt}: ${guess} remaining possibilities: ${set.length}`);
// get the bulls & cows score of the guess versus the secret
score = GameEngine.compareNumbers(guess, secret);
// 4 bulls === win
if (score.bulls === 4) {
console.log('-------------------------------------------------');
console.log(`Number found in ${attempt} attempts!\n`);
break;
}
// based on the score, filter the possibilities set
set = GameEngine.filterSet(set, guess, score);
bulls = score.bulls !== 1 ? score.bulls + ' bulls' : 'a bull';
cows = score.cows !== 1 ? score.cows + ' cows' : 'a cow';
console.log(`Found: ${bulls} and ${cows}\n`);
}