Created
September 25, 2018 18:08
-
-
Save lavelle/ab007be74d1d6f2cc5e2a6f285e41de7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const net = require('net'); | |
| const _ = require('lodash'); | |
| const state = [1, 1, 1, 1]; | |
| let index = 0; | |
| var client = new net.Socket(); | |
| function makeGuess() { | |
| const currentGuess = getNextGuess().join(''); | |
| console.log(`Guessing ${currentGuess}`); | |
| client.write(`GUESS ${currentGuess}\n`); | |
| } | |
| client.connect(18811, '0.tcp.ngrok.io', function() { | |
| client.write('START\n'); | |
| }); | |
| function getNextGuess() { | |
| let s = _.clone(state); | |
| if (state[index] < 7) { | |
| state[index]++; | |
| } | |
| if (state[index] === 6) { | |
| index++; | |
| } | |
| return s; | |
| } | |
| client.on('data', function(data) { | |
| const res = data.toString(); | |
| console.log(`Got back ${res}`); | |
| if (_.startsWith(res, 'OK')) { | |
| makeGuess(); | |
| } else { | |
| const a = _.toNumber(res.split(' ')[0]); | |
| console.log(a); | |
| // Make some changes | |
| makeGuess(); | |
| } | |
| }); | |
| client.on('close', function() { | |
| console.log('Connection closed'); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment