From 1954ed67ef61c509560418a9d2da58083b822996 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sat, 5 Dec 2015 20:40:32 +0100 Subject: Initial enzo --- index.js | 248 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 248 insertions(+) create mode 100755 index.js (limited to 'index.js') diff --git a/index.js b/index.js new file mode 100755 index 0000000..d2432ac --- /dev/null +++ b/index.js @@ -0,0 +1,248 @@ +#!/usr/bin/env node + +"use strict"; + +var app=require("express")(), + http=require("http").Server(app), + io=require("socket.io")(http), + fs=require("fs"), + spawn=require("child_process").spawn, + mkdirp=require("mkdirp"), + C=require("./common.js"); + + +var HTTPPORT=8090; + +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); + }); +}); + +function Game(_np){ + if(!(this instanceof Game))return new Game(_np); + this.bd=emptyboard(); + this.np=np; + this.onturn=0; +} +Game.prototype.applymove=function(player,pos){ + if(this.onturn!=player)return false; + var x=pos%C.W,y=~~(pos/C.W); + if(this.bd[y][x].n&&this.bd[y][x].c!=player)return false; + this.bd[y][x].c=player; + this.bd[y][x].n++; + this.bd=C.stabilise(this.bd); + do this.onturn=(this.onturn+1)%this.np; + while C.countballs(this.bd,this.onturn)==0; + return true; +}; +Game.prototype.checkwin=function(){ + if(C.countballs(this.bd): {ids:[id's], game:Game}} + +function findid(id){ + var lo=0,mid,hi=connlist.length-1; + if(connlist[lo].id==id)return lo; + if(connlist[hi].id==id)return hi; + while(lo