diff options
Diffstat (limited to 'index.js')
-rwxr-xr-x | index.js | 24 |
1 files changed, 18 insertions, 6 deletions
@@ -30,19 +30,27 @@ app.get("/",function(req,res){ function Game(_np){ if(!(this instanceof Game))return new Game(_np); - this.bd=emptyboard(); - this.np=np; + this.bd=C.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(x<0||y<0||x>=C.W||y>=C.H){ + console.log("applymove asked to apply on pos ("+x+","+y+")"); + return false; + } 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; + this.onturn=(this.onturn+1)%this.np; + if(C.countballs(this.bd)>=this.np){ + while(C.countballs(this.bd,this.onturn)==0){ + this.onturn=(this.onturn+1)%this.np; + } + } return true; }; Game.prototype.checkwin=function(){ @@ -166,7 +174,7 @@ io.on("connection",function(conn){ }); function guardroomid(roomid){ if(obj.rooms.indexOf(roomid)==-1){ - conn.emit("message","No member of that room!"); + conn.emit("message","Not a member of that room!"); return false; } return true; @@ -234,7 +242,11 @@ io.on("connection",function(conn){ conn.on("roomgameplace",function(roomid,pos){ if(!guardroomid(roomid)||!guardroomgame(roomid))return; var playeridx=rooms[roomid].ids.indexOf(obj.id); - if(!rooms[roomid].game.applymove(pos,playeridx)){ + if(playeridx!=rooms[roomid].game.onturn){ + conn.emit("message","Not your turn! (You are player "+(playeridx+1)+", on turn: "+(rooms[roomid].game.onturn+1)+")"); + return; + } + if(!rooms[roomid].game.applymove(playeridx,pos)){ conn.emit("message","Invalid move!"); return; } |