From 324bb279a74ab7d74849a4a55e44fba652a190aa Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Fri, 23 Oct 2015 14:20:28 +0200 Subject: Client improvements --- interactor/common.js | 19 +++++++++++++++++++ interactor/index.html | 4 ++++ interactor/interactor.js | 30 ++++++++++++++++++++++++++++-- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/interactor/common.js b/interactor/common.js index 65313ce..af7c462 100644 --- a/interactor/common.js +++ b/interactor/common.js @@ -16,6 +16,24 @@ function bdcopy(bd){ }); } +function checkwin(bd){ + var wincolour=-1,i; + for(i=0;iPlayer "+(player+1)+" won"); +}); diff --git a/interactor/interactor.js b/interactor/interactor.js index 2d38cbb..843bf2c 100755 --- a/interactor/interactor.js +++ b/interactor/interactor.js @@ -36,6 +36,16 @@ app.get("/",function(req,res){ }); }); +function stopproc(proc){ + proc.stdin.write("Quit\n"); + var timeout=setTimeout(function(){ + proc.kill(); + },500); + proc.on("exit",function(){ + clearTimeout(timeout); + }); +} + io.on("connection",function(conn){ var id=uniqid(); console.log("New IO connection id "+id); @@ -57,16 +67,24 @@ io.on("connection",function(conn){ bd[y][x].c=1-aiplayer; bd[y][x].n++; bd=stabilise(bd); + if(countballs(bd)>2){ + var win=checkwin(bd); + if(win!=-1){ + conn.emit("win",1-aiplayer); + stopproc(proc); + return; + } + } proc.stdin.write(x+" "+y+"\n"); }); var proc=spawn("sh",["-c",aicmd],{ - stdio:["pipe","pipe","inherit"] + stdio:["pipe","pipe",process.stderr] }); var buffer=""; proc.stdout.on("data",function(data){ - var idx,line,mv; + var idx,line,mv,win; buffer+=data; while((idx=buffer.indexOf("\n"))!=-1){ line=buffer.slice(0,idx); @@ -82,6 +100,14 @@ io.on("connection",function(conn){ bd[mv[1]][mv[0]].n++; bd=stabilise(bd); conn.emit("applymove",{index:W*mv[1]+mv[0],player:aiplayer}); + if(countballs(bd)>2){ + win=checkwin(bd); + if(win!=-1){ + conn.emit("win",aiplayer); + stopproc(proc); + return; + } + } conn.emit("setonturn",1-aiplayer); conn.emit("getusermove"); } -- cgit v1.2.3-70-g09d2