Skip to content

Instantly share code, notes, and snippets.

@anoopelias
Created November 28, 2015 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anoopelias/7f795457a093607939b9 to your computer and use it in GitHub Desktop.
Save anoopelias/7f795457a093607939b9 to your computer and use it in GitHub Desktop.
/**
* __main__
*/
var bot;
var botId;
var lenColumns;
var lenRows;
var round;
var field;
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
rl.on('line', function(line) {
log('line : ' + line);
try {
processLine(line);
} catch (err) {
log('Error : ' + err);
}
});
function log(message) {
console.error('Log : ' + message);
}
function processLine(line) {
if (!line || line.length === 0)
return;
var splits = line.split(' ');
var params = splits.slice(1);
switch(splits[0]) {
case 'settings' :
processSettings.apply(null, params);
break;
case 'update' :
processUpdate.apply(null, params);
break;
case 'action' :
processAction.apply(null, params);
break;
}
}
function processSettings(type, value) {
log('settings : type : ' + type + ' value : ' + value);
switch(type) {
case 'your_bot' :
bot = value;
break;
case 'your_botid' :
botId = value;
break;
case 'field_columns' :
lenColumns = parseInt(value);
break;
case 'field_rows' :
lenRows = parseInt(value);
break;
}
}
function processUpdate(updateType, type, value) {
log('update : type : ' + type + ' value : ' + value);
switch(type) {
case 'round' :
round = parseInt(value);
break;
case 'field' :
processField(value);
break;
}
}
function processField(strField) {
var rows = strField.split(';');
field = [];
for(var i in rows) {
var vals = rows[i].split(',');
field[i] = vals.map(function(strVal) {
return parseInt(strVal);
});
}
log(field);
}
function processAction(type, value) {
log('update : type : ' + type + ' value : ' + value);
switch(type) {
case 'move' :
log('remaining time : ' + value);
processMove();
break;
}
}
function processMove() {
var topField = field[0];
var index = -1;
do {
index = Math.round(Math.random() * (topField.length - 1));
} while (topField[index] != 0)
log('index ' + index);
process.stdout.write('place_disk ' + index + '\n');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment