From 864bce4ebdc625468969891b36784490448ceea3 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Fri, 8 May 2015 21:14:22 +0200 Subject: Bug fixing and win checking in gameserver --- gameserver/gameserver.html | 42 ++++++++++++++++++++++++++++++++---------- gameserver/gameserver.js | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 13 deletions(-) diff --git a/gameserver/gameserver.html b/gameserver/gameserver.html index d8528ab..7fc27fc 100644 --- a/gameserver/gameserver.html +++ b/gameserver/gameserver.html @@ -74,19 +74,33 @@ function preinit(){ MOVES.push(arg[1]); addmovelistrow(arg[1]); nextmove(); - } else alert(arg[1]); - } else if(cmd=="your_turn"){ - canAlreadyTurn=true; - if(hasInited){ + } else { + alert(arg[1]); enableClicking(true,isFirstMove&&imPlayer==1); - isFirstMove=false; } + } else if(cmd=="other_turn"){ if(arg){ MOVES.push(arg); addmovelistrow(arg); nextmove(); } - } + } else if(cmd=="your_turn"){ + canAlreadyTurn=true; + if(hasInited){ + enableClicking(true,isFirstMove&&imPlayer==1); + isFirstMove=false; + } + } else if(cmd=="game_stop"){ + alert("Game has been stopped:\n"+arg); + enableClicking(false); + } else if(cmd=="game_win"){ + bgcell(neuidx).classList.add("win"); + if(arg==imPlayer){ + alert("You have won the game!"); + } else { + alert("You have lost the game! Better luck next time."); + } + } else alert("Unrecognised message "+msg); }); ws.addEventListener("open",function(){ ws.send("nick"); @@ -262,8 +276,8 @@ function addmovelistrow(mv,i/*optional*/){ if(moveidx==target)return; while(moveidxtarget)prevmove(true); - setselectedmovelistline(i+1); - };})(i+1)); + setselectedmovelistline(target); + };})(i-1)); movelisttbody.appendChild(tr); } @@ -339,7 +353,7 @@ function nextmove(notimeout){ fgcells[neuidx]=null; neuidx=newidx; - if(moveidx==MOVES.length-1)bgcell(neuidx).classList.add("win"); + //if(moveidx==MOVES.length-1)bgcell(neuidx).classList.add("win"); } newidx=indexafterslide(MOVES[moveidx][1],MOVES[moveidx][2]); @@ -371,7 +385,9 @@ function prevmove(notimeout){ if(moveidx==0||!canclick)return false; moveidx--; - if(MOVES[moveidx][0]!=-1&&moveidx==MOVES.length-1)winGlowIdxToRemove=neuidx; + //if(MOVES[moveidx][0]!=-1&&moveidx==MOVES.length-1)winGlowIdxToRemove=neuidx; + for(var i=0;i diff --git a/gameserver/gameserver.js b/gameserver/gameserver.js index ddc6f0b..8ab4bbc 100755 --- a/gameserver/gameserver.js +++ b/gameserver/gameserver.js @@ -127,8 +127,24 @@ function moveIsLegal(bd,mv,forpl){ return true; } +function checkWin(bd,pljustmoved){ + var i,neuidx=-1,newidx; + for(i=0;i<25;i++)if(bd[i]==3){neuidx=i;break;} + assert(neuidx!=-1); + if(neuidx<5)return 1; + if(neuidx>=20)return 2; + for(i=0;i<8;i++){ + newidx=idxAfterSlide(bd,neuidx,i); + if(newidx!=neuidx)break; + } + if(i==8){ //no shifts of the neutron possible + return pljustmoved; + } + return 0; +} + function handleMessage(conn,msg){ - var cmd=msg.split(" "),list,i,game,c2,mv; + var cmd=msg.split(" "),list,i,game,c2,mv,won; switch(cmd[0]){ case "nick": conn.ws.send("nick \""+conn.nick+"\""); @@ -203,10 +219,20 @@ function handleMessage(conn,msg){ game.board=applyMove(game.board,mv); console.log("new board:"); console.log(game.board); - game.onturn=3-game.onturn; conn.ws.send("game_doturn "+JSON.stringify([true,mv])); + game.onturn=3-game.onturn; c2=conns[findby(conns,"id",conn.id==game.i1?game.i2:game.i1)]; - c2.ws.send("your_turn "+JSON.stringify(mv)); + c2.ws.send("other_turn "+JSON.stringify(mv)); + won=checkWin(game.board,3-game.onturn); //player that just moved + if(won){ + conn.ws.send("game_win "+won); + c2.ws.send("game_win "+won); + games.splice(findby(games,"id",conn.gameid),1); + conn.gameid=null; + c2.gameid=null; + break; + } + c2.ws.send("your_turn true"); game.firstmove=false; break; default: @@ -234,6 +260,7 @@ new WebSocketServer({port:WSPORT}).on("connection",function(ws){ conns[otherplayeridx].gameid=null; conn.gameid=null; games.splice(gameidx,1); + conns[otherplayeridx].ws.send("game_stop \"Other player quit\""); } }); }); -- cgit v1.2.3-54-g00ecf