From c8a692cf39979962c72e8d3ec9108b85105feeb0 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Fri, 23 Dec 2016 21:40:46 +0100 Subject: Some changes that were lying around --- .gitignore | 3 + fiforead.cpp | 16 ++ gameserver/gameserver.html | 18 +- gameserver/gameserver.js | 1 + humanbot.cpp | 254 +++++++++++++++++++++ humanbot_index.html | 543 +++++++++++++++++++++++++++++++++++++++++++++ humanbot_server.js | 74 ++++++ 7 files changed, 903 insertions(+), 6 deletions(-) create mode 100644 fiforead.cpp create mode 100644 humanbot.cpp create mode 100644 humanbot_index.html create mode 100755 humanbot_server.js diff --git a/.gitignore b/.gitignore index d4a5d5b..8e7121c 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,9 @@ randino photon charm +fiforead +humanbot + viewcompetition *.o diff --git a/fiforead.cpp b/fiforead.cpp new file mode 100644 index 0000000..6da0ae1 --- /dev/null +++ b/fiforead.cpp @@ -0,0 +1,16 @@ +#include +#include +#include + +using namespace std; + +int main(int argc,char **argv){ + if(argc!=2){ + cerr<<"Pass fifo name as argument."< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define S (5) + +using namespace std; + +bool should_flip_board=false; + +struct Move; +class Board; + +Move protocol_get_move(istream &s); +void protocol_put_move(ostream &s,Move m); + +int index_deltas[8][2]={{0,-1},{1,-1},{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1}}; + +struct Move{ + int neudir,from,dir; + string str(void){ + stringstream ss; + ss<=S||y2<0||y2>=S||grid[S*y2+x2]!=0)return S*y+x; + x=x2; + y=y2; + } + } + bool move(int at,int dir){ + int newat=moved(at,dir); + if(newat==at)return false; + grid[newat]=grid[at]; + grid[at]=0; + if(grid[newat]==3)neuidx=newat; + return true; + } + void undomove(int at,int dir){ + int movedidx=moved(at,dir); + movedidx+=S*index_deltas[dir][1]+index_deltas[dir][0]; + grid[at]=grid[movedidx]; + grid[movedidx]=0; + if(grid[at]==3)neuidx=at; + } + bool isvalid(Move mv){ + int oldneuidx=neuidx; + //cerr<<"Called isvalid with move "; + //protocol_put_move(cerr,mv); + //print(); + if(!move(oldneuidx,mv.neudir)){ + //undomove(oldneuidx,mv.neudir); + return false; + } + if(!move(mv.from,mv.dir)){ + //undomove(mv.from,mv.dir); + undomove(oldneuidx,mv.neudir); + return false; + } + undomove(mv.from,mv.dir); + undomove(oldneuidx,mv.neudir); + return true; + } + void print(void){ + int x,y; + if(should_flip_board){ + for(y=S-1;y>=0;y--){ + for(x=S-1;x>=0;x--)cerr<>m.neudir>>m.from>>m.dir; + if(should_flip_board){ + m.neudir=flip_dir(m.neudir); + m.from=flip_index(m.from); + m.dir=flip_dir(m.dir); + } + cerr<<"Got: "; + protocol_put_move(cerr,m); + s.ignore(1024,'\n'); + return m; +} + +void protocol_put_move(ostream &s,Move m){ + if(should_flip_board){ + s< + + + +Game + + + + +

+
+ + + + + + + + + + + + + + + + +
Neu
+ + + + + + + + + + + + + + + + +
+
+
+
+ +
+
+
+ +
+
+ + diff --git a/humanbot_server.js b/humanbot_server.js new file mode 100755 index 0000000..3f38f66 --- /dev/null +++ b/humanbot_server.js @@ -0,0 +1,74 @@ +#!/usr/bin/env node +var http=require("http"),fs=require("fs"); +var PORT=+process.argv[2]; +var movequeue=[]; +var httpserver=http.createServer(function(req,res){ + var reqbody=""; + console.log("req: "+req.url); + if(req.url=="/othermove"){ + if(movequeue.length){ + res.writeHead(200,{"Content-Type":"application/json"}); + res.end(movequeue.shift()); + } else { + res.writeHead(503,{"Content-Type":"text/plain"}); + res.end("No move to unqueue"); + } + } else if(req.url=="/mynewmove"){ + req.on("data",function(data){reqbody+=data;}); + req.on("end",function(){ + var mv; + console.log("mynewmove: reqbody = "+reqbody); + try {mv=JSON.parse(reqbody);} + catch(e){mv=null;} + if(mv==null|| + !Array.isArray(mv)|| + mv.length!=3|| + !mv.map(function(v){ + return typeof(v)=="number"&&v%1==0; + }).reduce(function(a,b){return a&&b;}) + ){ + res.writeHead(400,{"Content-Type":"text/plain"}); + res.end("400 Bad request"); + return; + } + mynewmovefifo.write(mv.join(" ")+"\n",function(){ + console.log("server: ACTUALLY written new move to fifo"); + }); + res.writeHead(200,{"Content-Type":"text/plain"}); + res.end("true"); + }); + } else if(req.url=="/"){ + res.writeHead(200,{"Content-Type":"text/html"}); + res.end(String(fs.readFileSync("humanbot_index.html"))); + } else { + res.writeHead(404,{"Content-Type":"text/plain"}); + res.end("Nothing here..."); + } +}); +var httpserverlisteninterval=setInterval(function(){ + try { + httpserver.listen(PORT); + console.log("Server running at port "+PORT+". (http://localhost:"+PORT+")"); + clearInterval(httpserverlisteninterval); + } catch(e){} +},10); + +var othermovefifo,mynewmovefifo,othermovefifo_buffer=""; +othermovefifo=fs.createReadStream(".humanbot__.__othermove.fifo"); +othermovefifo.on("data",function(data){ + var idx,v; + othermovefifo_buffer+=data; + idx=othermovefifo_buffer.indexOf("\n"); + if(idx!=-1){ + v=othermovefifo_buffer.slice(0,idx); + if(v!="go"&&v!="nogo")v="["+v.replace(/ /g,",")+"]"; + movequeue.push(v); + othermovefifo_buffer=othermovefifo_buffer.slice(idx+1); + } +}); +othermovefifo.on("end",function(){ + console.log("othermovefifo closed. Exiting."); + process.exit(); +}); +mynewmovefifo=fs.createWriteStream(".humanbot__.__mynewmove.fifo",{flags:"a"}); +console.log("Fifo's opened."); -- cgit v1.2.3-70-g09d2