-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
52 lines (44 loc) · 1.31 KB
/
app.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
var utils = require('../../game_logic/utils');
var http = require('http');
var express = require('express');
var app = express();
app.set('port', process.argv[2]);
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.post('/', function(req, res) {
var game = JSON.parse(req.body.data);
var moves = getMoves(game.state, game.player);
res.send(moves);
res.end();
});
app.get('/', function(req, res) {
res.send('hello');
res.end();
});
http.createServer(app).listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
process.stdin.on('data', function(data) {
var game = JSON.parse(data);
moves = getMoves(game.state, game.player);
console.log(JSON.stringify(moves));
});
function getMoves(state, player) {
var playerIndices;
var moves = [];
if (player === 'r') {
playerIndices = utils.getAllIndices(state.grid, 'r');
} else {
playerIndices = utils.getAllIndices(state.grid, 'b');
}
playerIndices.forEach(function(playerIndex) {
var adjacent = getAdjacentIndices(state, playerIndex);
var to = adjacent[Math.floor(Math.random()*adjacent.length)];
moves.push({ from: playerIndex, to: to });
});
return moves;
}