Ejemplo n.º 1
0
        public void onPlayerMove(object msg, ConnectedPlayer p)
        {
            if (p != currentPlayer() || gameOver)
            {
                return;
            }

            //Parse msg
            string words = Convert.ToString(msg);

            int[] move   = new int[4];
            int[] start  = new int[2];
            int[] target = new int[2];
            try{
                start  = new int[] { int.Parse(words[0].ToString()), int.Parse(words [1].ToString()) };
                target = new int[] { int.Parse(words[2].ToString()), int.Parse(words [3].ToString()) };
            }catch (Exception e) {
                print("Corrupt incoming msg: " + Convert.ToString(msg));
                protocol.requestMove(currentPlayer().peerID, theGame.ToString());
                return;
            }

            //Check if Valid move
            Move playedMove = MoveHandler.isValidMove(theGame, p.color == Game.PlayerColor.White, start, target);

            if (playedMove == null)
            {
                print("Not valid Move: " + Convert.ToString(msg));
                protocol.requestMove(currentPlayer().peerID, theGame.ToString());
                return;
            }
            //Play move & Broadcast
            theGame = theGame.playMove(playedMove);
            RPCMove rpcMove = new RPCMove(start, target, p.color);

            foreach (ConnectedPlayer pl in players)
            {
                protocol.sendRPCMove(pl.peerID, rpcMove);
            }


            //Check if gameOver
            nextPlayer();
            BoardStatus status = MoveHandler.getGameState(theGame, currentPlayer().color == Game.PlayerColor.White);

            if (status != BoardStatus.normal)
            {
                gameOver = true;
                Game.PlayerColor winColor;
                winColor = status == BoardStatus.draw ? Game.PlayerColor.None : p.color;
                broadcastGameOver(winColor);
            }
            else
            {
                protocol.requestMove(currentPlayer().peerID, theGame.ToString());
            }
        }