和socket.io和uci一起下棋

时间:2013-12-23 12:01:12

标签: node.js sockets socket.io

我想创建一个可以与UCI-Chessengine对战的webapp。我发现https://github.com/imor/uci在命令行上运行良好。所以我“只”需要一个websocket来评估这些动作。

但是我无法让它运行......我尝试过(基于uci示例):

io.sockets.on('connection',function(socket){
  socket.on('start', function(data) { 
    var uci = new UCI();
    var game = new Chess();
    uci.on('ready', function () {
        //Start a new 10 minute game with engine as black, use the first found
        //engine and the first found polyglot book
        uci.startNewGame(uci.getAvailableEngines()[0], 'black', 10,
            uci.getAvailableBooks()[0]);
    }).on('newgame', function () {
        console.log("A new 10 minute game has started.");
        console.log("Enter your moves in algebraic notation. E.g. e2e4<Enter>");
        console.log(game.ascii());
    }).on('moved', function (move) {
        game.move(move);
        console.log(move.from + move.to + (move.promotion ? move.promotion : ''));
        console.log(game.ascii());
    }).on('error', function (message) {
        console.log('Error:' + message);
    }).on('exit', function (message) {
        console.log('Exiting:' + message);
    }).on('gameends', function (result, reason) {
        console.log('Game ends with result ' + result + ' because ' + reason);
        uci.shutdown();
        process.exit();
    });
  })

  socket.on('m',function(data){
      uci.move(data.move);
  });
});

开始游戏正在运行:socket.emit('start',{“bar”:“foo”}) 但是当我尝试使用socket.emit('m':“e2e4”)进行移动时,它不知道uci.move

这没关系,因为它是在socket.on('start')中定义的......所以他无法知道,但我无法让它运行。我尝试了一些愚蠢的想法,例如将socket.on('m')放入socket.on('start')......

有人可以帮我这个吗?如何将移动发送到创建的uci-connection?或者这不可能吗?

非常感谢。

1 个答案:

答案 0 :(得分:1)

尝试在范围层次结构中向上移动var uci

io.sockets.on('connection',function(socket){
  var uci;
  socket.on('start', function(data) { 
    uci = new UCI();
    var game = new Chess();
    ...
  })

  socket.on('m',function(data){
    uci.move(data.move);
  });
});