Created
November 28, 2015 19:41
-
-
Save anoopelias/7f795457a093607939b9 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
/** | |
* __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