From b5077ff9a36c89c84c04f33dce3a2a0ff45e9184 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sun, 18 Oct 2015 21:20:18 +0200 Subject: Initial --- .gitignore | 1 + common.js | 43 ++++++++++++ index.html | 213 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ interactor.js | 96 ++++++++++++++++++++++++++ package.json | 15 +++++ 5 files changed, 368 insertions(+) create mode 100644 .gitignore create mode 100644 common.js create mode 100644 index.html create mode 100755 interactor.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/common.js b/common.js new file mode 100644 index 0000000..dbc3484 --- /dev/null +++ b/common.js @@ -0,0 +1,43 @@ +var W=7,H=8; + +function emptyboard(){ + return new Array(H).fill(0).map(function(){ + return new Array(W).fill(0).map(function(){ + return {n:0,c:0}; + }); + }); +} + +function bdcopy(bd){ + return bd.map(function(r){ + return r.map(function(c){ + return {n:c.n,c:c.c}; + }); + }); +} + +function stabilise(bd){ + var newbd; + var changes; + var x,y,nnei,quo; + do { + changes=false; + newbd=bdcopy(bd); + for(y=0;y0)+(x>0)+(y=nnei){ + quo=~~(bd[y][x].n/nnei); + newbd[y][x].n-=quo*nnei; + if(y>0) {newbd[y-1][x].n+=quo;newbd[y-1][x].c=bd[y][x].c;} + if(x>0) {newbd[y][x-1].n+=quo;newbd[y][x-1].c=bd[y][x].c;} + if(y + + + +Interactor for Chain Reaction + + + + + +

Interactor for Chain Reaction

+
+ + + \ No newline at end of file diff --git a/interactor.js b/interactor.js new file mode 100755 index 0000000..2d38cbb --- /dev/null +++ b/interactor.js @@ -0,0 +1,96 @@ +#!/usr/bin/env node + +var aicmd; + +if(process.argv.length!=3){ + console.log("Give AI command to run as command line argument."); + process.exit(1); +} +aicmd=process.argv[2]; +console.log("Using AI command '"+aicmd+"'"); + + +var app=require("express")(), + http=require("http").Server(app), + io=require("socket.io")(http), + fs=require("fs"), + spawn=require("child_process").spawn; + +eval(String(fs.readFileSync("common.js"))); + + +var HTTPPORT=8080; + +var uniqid=(function(){ + var id=0; + return function(){return id++;}; +})(); + +app.get("/",function(req,res){ + res.sendFile(__dirname+"/index.html"); +}); + +["index.html","common.js"].forEach(function(fname){ + app.get("/"+fname,function(req,res){ + res.sendFile(__dirname+"/"+fname); + }); +}); + +io.on("connection",function(conn){ + var id=uniqid(); + console.log("New IO connection id "+id); + + var bd=emptyboard(); + var aiplayer=0; + + conn.on("disconnect",function(){ + console.log("Disconnect id "+id); + }); + conn.on("usermove",function(idx){ + idx=+idx; + if(idx<0||idx>=W*H){ + conn.emit("alert","You sent an invalid move, index "+idx); + conn.emit("getusermove"); + return; + } + var x=idx%W,y=~~(idx/W); + bd[y][x].c=1-aiplayer; + bd[y][x].n++; + bd=stabilise(bd); + proc.stdin.write(x+" "+y+"\n"); + }); + + + var proc=spawn("sh",["-c",aicmd],{ + stdio:["pipe","pipe","inherit"] + }); + var buffer=""; + proc.stdout.on("data",function(data){ + var idx,line,mv; + buffer+=data; + while((idx=buffer.indexOf("\n"))!=-1){ + line=buffer.slice(0,idx); + buffer=buffer.slice(idx+1); + mv=line.split(" ").map(function(s){return parseInt(s,10);}); + if(mv.length!=2||isNaN(mv[0])||isNaN(mv[1])){ + console.log("Invalid move written by AI: '"+line+"'"); + conn.emit("alert","Invalid move written by AI: '"+line+"'"); + mv[0]=0; + mv[1]=0; + } + bd[mv[1]][mv[0]].c=aiplayer; + bd[mv[1]][mv[0]].n++; + bd=stabilise(bd); + conn.emit("applymove",{index:W*mv[1]+mv[0],player:aiplayer}); + conn.emit("setonturn",1-aiplayer); + conn.emit("getusermove"); + } + }); + proc.stdin.write("A\n-1 -1\n"); + conn.emit("emptyboard"); + conn.emit("setonturn",0); +}); + +http.listen(HTTPPORT,function(){ + console.log("Listening on http://localhost:"+HTTPPORT); +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..38f7446 --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "interactor", + "version": "1.0.0", + "description": "", + "main": "interactor.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Tom Smeding (http://tomsmeding.com)", + "license": "MIT", + "dependencies": { + "express": "^4.13.3", + "socket.io": "^1.3.7" + } +} -- cgit v1.2.3-54-g00ecf