-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_player.js
69 lines (59 loc) · 1.59 KB
/
run_player.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const readline = require('readline');
function main() {
const playerImplementation = process.argv[2] ? process.argv[2] : 'random';
runPlayer(playerImplementation);
}
function runPlayer(playerImplementation) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Load player's code
const PlayerImplementation = require(`./players/${playerImplementation}.js`);
let player = new PlayerImplementation(1);
rl.on('line', function (input) {
const parts = input.split(' ');
const action = parts[0];
let next, coords;
console.log('RECEIVED MESSAGE', input);
switch (action) {
case 'init':
player.init();
break;
case 'move':
try {
coords = player.getMove();
// player.addMove(coords.board, coords.move);
writeMove(coords);
} catch (e) {
console.error('Player Error: Failed to get a move', e);
}
break;
case 'opponent':
// the move will be in the format row,col format
const moveCoords = parts[1].split(',').map((coord) => parseInt(coord, 10));
player.onOpponentMove(
[
moveCoords[0],
moveCoords[1]
]
);
if (!player.game.isFinished()) {
coords = player.getMove();
writeMove(coords);
}
break;
}
});
}
function writeMove(coords) {
console.log('write move', coords);
const move = coords[0] + ',' + coords[1];
write(move);
}
function write(output) {
if (output) {
console.log("send:", output);
}
}
main();