WEB开发网
开发学院软件开发C语言 C#网络版斗地主——出牌权限的传递 阅读

C#网络版斗地主——出牌权限的传递

 2009-06-08 08:32:26 来源:WEB开发网   
核心提示: 来看看server类中有关出牌权限传递的相关处理程序:///<summary>///循环接收客户端1的请求数据///</summary>publicvoidAccpetClient1Data(){NetworkStreamNs1=client1.GetStream(

来看看server类中有关出牌权限传递的相关处理程序:

        /// <summary>
        /// 循环接收客户端1的请求数据
        /// </summary>
        public void AccpetClient1Data()
        {
            NetworkStream Ns1 = client1.GetStream();
            string str1 = "";
            while (true)
            {
                PokerGroup pg = new PokerGroup();
                byte[] bytes1 = new byte[108];
                Ns1.Read(bytes1, 0, 108);
                str1 = Encoding.Default.GetString(bytes1);
//(省略N行)
                if (str1.StartsWith("client")) //收到客户端1的client消息,该消息表示客户端1出的牌
                {
                    SendDataForClient(str1, 2);
                    Thread.Sleep(sleep);
                    str1 = str1.Replace("client", "");
                    pg.GetPokerGroup(Encoding.Default.GetBytes(str1));
                    DConsole.leadedPokerGroups.Add(pg); //这里在add之前会对该牌组进行验证和排序
                    DConsole.PaintPlayer2LeadPoker(pg);
                    DConsole.WriteLeadedPokers(); 
                    if (!DConsole.IsRestart)
                    {
                        DConsole.player1.haveOrder = true;  //client1出牌后归server出牌,前提是没有Restart,Restart后出牌权限消失,根据权限传递顺序client2-clinet1-server,这里接收到的是client1的出牌,说明轮到server出牌了.
                    }
                    else
                    {
                        DConsole.IsRestart = false;//当检测到已经Restart时,复位Restart使它还原为false
                    }
                    continue;
                }
                //Client放弃出牌,权限交给服务器
                if (str1.StartsWith("Pass"))
                {
                    DConsole.gPlayer2LeadPoker.Clear(DConsole.backColor);
                    DConsole.gPlayer2LeadPoker.DrawString("不要", new System.Drawing.Font("宋体", 20), System.Drawing.Brushes.Red,5,5);
                    DConsole.player1.haveOrder = true;
                    SendDataForClient("ClientPass", 2); //告诉客户端2,客户端1pass了
                    continue;
                }
                if (str1.StartsWith("IamIsBiggest"))//客户端1说他的牌是最大的,所以要把自己的IsBiggest设置为false,因为最大的牌只能有一个
                {
                    DConsole.player1.isBiggest = false;
                    this.SendDataForClient("NoBiggest", 2); //同时要转发给client2,让client2把自己的IsBiggest属性设置为false
                    continue;
                }
//(省略N行)
            }
        }
        /// <summary>
        /// 循环接收客户端2的请求数据
        /// </summary>
        public void AccpetClient2Data()
        {
            NetworkStream Ns2 = client2.GetStream();
            string str1 = "";
            while (true)
            {
                PokerGroup pg = new PokerGroup();
                byte[] bytes2 = new byte[108];
                Ns2.Read(bytes2, 0, 108);
                str1 = Encoding.Default.GetString(bytes2);
(省略N行)
                if (str1.StartsWith("client"))//收到客户端2的client消息,该消息表示客户端2出的牌

                {
                    SendDataForClient(str1, 1);
                    Thread.Sleep(sleep);
                    str1 = str1.Replace("client", "");
                    pg.GetPokerGroup(Encoding.Default.GetBytes(str1));
                    DConsole.leadedPokerGroups.Add(pg);//这里在add之前会对该牌组进行验证和排序
                    DConsole.PaintPlayer3LeadPoker(pg);
                    DConsole.WriteLeadedPokers();
                    if (!DConsole.IsRestart)
                    {
                        SendDataForClient("Order", 1);  //client2出牌后归client1出牌,前提是没有Restart,Restart后出牌权限消失,根据权限传递顺序server-client2-clinet1,这里接收到的是client2的出牌,说明轮到clinet1出牌了.
                    }
                    else
                    {
                        DConsole.IsRestart = false; //当检测到已经Restart时,复位Restart使它还原为false供下次使用
                    }
                    System.Threading.Thread.Sleep(sleep);
                    continue;
                }
                //Client2放弃出牌,权限交给Client1
                if (str1.StartsWith("Pass"))
                {
                    DConsole.gPlayer3LeadPoker.Clear(DConsole.backColor);
                    DConsole.gPlayer3LeadPoker.DrawString("不要", new System.Drawing.Font("宋体", 20), System.Drawing.Brushes.Red, 5, 5);
                    SendDataForClient("ClientPass", 1);//告诉客户端1,客户端2pass了
                    Thread.Sleep(500);
                    SendDataForClient("Order", 1);
                    continue;
                }
                if (str1.StartsWith("IamIsBiggest"))//客户端2说他的牌是最大的,所以要把自己的IsBiggest设置为false,因为最大的牌只能有一个
                {
                    DConsole.player1.isBiggest = false;
                    this.SendDataForClient("NoBiggest", 1);//同时要转发给client21,让client1把自己的IsBiggest属性设置为false

                    continue;
                }
(省略N行)
            }
        }

上一页  1 2 3 4  下一页

Tags:网络 斗地主 权限

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接