Exemple #1
0
        /// <summary>
        /// 判断牌组是否为连对
        /// </summary>
        /// <param name="PG">牌组</param>
        /// <returns>是否为连对</returns>
        public static bool IsLinkPair(PokerGroup PG)
        {
            bool IsLinkPair = false;

            foreach (Poker poker in PG) //不能包含2、小王、大王
            {
                if (poker == PokerNum.P2 || poker == PokerNum.小王 || poker == PokerNum.大王)
                {
                    IsLinkPair = false;
                    return(IsLinkPair);
                }
            }
            for (int i = 0; i < PG.Count - 2; i += 2)  //首先比较是否都为对子,再比较第一个对子的点数-1是否等于第二个对子,最后检察最小的两个是否为对子(这里的for循环无法检测到最小的两个,所以需要拿出来单独检查)
            {
                if (PG[i] == PG[i + 1] && PG[i].pokerNum - 1 == PG[i + 2].pokerNum && PG[i + 2] == PG[i + 3])
                {
                    IsLinkPair = true;
                }
                else
                {
                    IsLinkPair = false;
                    break;
                }
            }
            return(IsLinkPair);
        }
Exemple #2
0
        /// <summary>
        /// 发送指定牌组和头信息给客户端
        /// </summary>
        /// <param name="head">头信息</param>
        /// <param name="pg">牌组</param>
        /// <param name="client">客户端编号,只能是1,2</param>
        /// <returns>是否发送成功</returns>
        public bool SendDataForClient(string head, PokerGroup pg, int client)
        {
            NetworkStream Ns = null;

            if (client == 1)
            {
                Ns = this.client1.GetStream();
            }
            if (client == 2)
            {
                Ns = this.client2.GetStream();//得到客户端的网络流对象
            }
            if (client != 1 && client != 2)
            {
                return(false);
            }
            byte[] bytePg    = pg.GetBuffer();                        //通过2个 MemoryStream对象获取代表2组牌的 比特流对象
            string strPg     = Encoding.Default.GetString(bytePg);
            string strSender = head + strPg;                          //组合两个字符串

            byte[] byteSender = Encoding.Default.GetBytes(strSender); //把要发送的数据转换为byte[]
            Ns.Write(byteSender, 0, byteSender.Length);               //把2个比特流对象写入server与client的连接管道中
            Thread.Sleep(sleep);
            return(true);
        }
Exemple #3
0
        /// <summary>
        /// 判断牌组是否为顺子
        /// </summary>
        /// <param name="PG">牌组</param>
        /// <returns>是否为顺子</returns>
        public static bool IsStraight(PokerGroup PG)
        {
            bool IsStraight = false;

            foreach (Poker poker in PG)//不能包含2、小王、大王
            {
                if (poker == PokerNum.P2 || poker == PokerNum.小王 || poker == PokerNum.大王)
                {
                    IsStraight = false;
                    return(IsStraight);
                }
            }
            for (int i = 0; i < PG.Count - 1; i++)
            {
                if (PG[i].pokerNum - 1 == PG[i + 1].pokerNum)
                {
                    IsStraight = true;
                }
                else
                {
                    IsStraight = false;
                    break;
                }
            }
            return(IsStraight);
        }
Exemple #4
0
        public bool SendDataForServer(PokerGroup pg)
        {
            NetworkStream Ns = this.client.GetStream();

            byte[] bytes = pg.GetBuffer();
            Ns.Write(bytes, 0, bytes.Length);
            Thread.Sleep(sleep);
            return(true);
        }
Exemple #5
0
        public bool SendDataForServer(string head, PokerGroup pg)
        {
            NetworkStream Ns = client.GetStream();

            byte[] bytePg    = pg.GetBuffer();                        //通过2个 MemoryStream对象获取代表2组牌的 比特流对象
            string strPg     = Encoding.Default.GetString(bytePg);
            string strSender = head + strPg;                          //组合两个字符串

            byte[] byteSender = Encoding.Default.GetBytes(strSender); //把要发送的数据转换为byte[]
            Ns.Write(byteSender, 0, byteSender.Length);               //把2个比特流对象写入server与client的连接管道中
            Thread.Sleep(sleep);
            return(true);
        }
Exemple #6
0
 public static void PaintPlayer3LeadPoker(PokerGroup pg)
 {
     RightTempLeadedPoker = pg;
     gPlayer3LeadPoker.Clear(backColor);
     for (int i = 0; i < pg.Count; i++)
     {
         int       y  = i * 20;
         Rectangle rt = new Rectangle(0, y, 95, 50);
         gPlayer3LeadPoker.FillRectangle(Brushes.White, rt);
         gPlayer3LeadPoker.DrawRectangle(Pens.Black, rt);
         gPlayer3LeadPoker.DrawString(pg[i].ToString(), new Font("宋体", 12), Brushes.Black, 40, y + 5);
     }
 }
Exemple #7
0
        /// <summary>
        /// 发牌
        /// </summary>
        public static void deal()
        {
            PokerGroup player2Pokers = new PokerGroup();
            PokerGroup player3Pokers = new PokerGroup();

            player1.pokers.Clear();
            for (int i = 0; i < 17; i++)
            {
                player1.pokers.Add(allPoker[i]);
            }
            for (int i = 17; i < 34; i++)
            {
                player2Pokers.Add(allPoker[i]);
            }
            for (int i = 34; i < 51; i++)
            {
                player3Pokers.Add(allPoker[i]);
            }
            LandLordNum = new Random().Next(1, 4);
            PokerGroup landLordPokers = new PokerGroup();

            for (int i = 51; i < 54; i++)
            {
                landLordPokers.Add(allPoker[i]);
            }
            LandLordPokers = landLordPokers;
            player1.sort();
            if (server.SendDataForClient("StartPokers", player2Pokers, 1) && server.SendDataForClient("StartPokers", player3Pokers, 2))
            {
                DConsole.Write("[系统消息]发牌成功!");
                server.SendOrder(LandLordNum);
            }
            //if (server.SendDataForClient(player2.pokers, 1) && server.SendDataForClient(player3.pokers, 2))
            //{
            //    DConsole.Write("[系统消息]发牌成功!");
            //    server.SendOrder(DConsole.LandLordNum);
            //}
            else
            {
                DConsole.Write("[系统消息]发牌失败!");
            }

#if DEBUG //调试时在Console上显示的信息
            Console.WriteLine("玩家一的牌");
            foreach (Poker onePoker in player1.pokers)
            {
                Console.WriteLine(onePoker.pokerColor.ToString() + onePoker.ToString());
            }
#endif
        }
Exemple #8
0
        /// <summary>
        /// 判断牌组是否为连续三张牌,飞机,飞机带翅膀
        /// </summary>
        /// <param name="PG">牌组</param>
        /// <returns>是否为连续三张牌</returns>
        public static bool IsThreeLinkPokers(PokerGroup PG) //判断三张牌方法为判断两两相邻的牌,如果两两相邻的牌相同,则count自加1.最后根据count的值判断牌的类型为多少个连续三张
        {
            bool IsThreeLinkPokers = false;
            int  HowMuchLinkThree  = 0;        //飞机的数量

            PG = SameThreeSort(PG);            //排序,把飞机放在前面
            for (int i = 2; i < PG.Count; i++) //得到牌组中有几个飞机
            {
                if (PG[i] == PG[i - 1] && PG[i] == PG[i - 2])
                {
                    HowMuchLinkThree++;
                }
            }
            if (HowMuchLinkThree > 0)                                                         //当牌组里面有三个时
            {
                if (HowMuchLinkThree > 1)                                                     //当牌组为飞机时
                {
                    for (int i = 0; i < HowMuchLinkThree * 3 - 3; i += 3)                     //判断飞机之间的点数是否相差1
                    {
                        if (PG[i] != PokerNum.P2 && PG[i].pokerNum - 1 == PG[i + 3].pokerNum) //2点不能当飞机出
                        {
                            IsThreeLinkPokers = true;
                        }
                        else
                        {
                            IsThreeLinkPokers = false;
                            break;
                        }
                    }
                }
                else
                {
                    IsThreeLinkPokers = true; //牌组为普通三个,直接返回true
                }
            }
            else
            {
                IsThreeLinkPokers = false;
            }
            if (HowMuchLinkThree == 4)
            {
                PG.type = PokerGroupType.四连飞机;
            }
            if (HowMuchLinkThree == 3 && PG.Count == 12)
            {
                PG.type = PokerGroupType.连飞机带翅膀;
            }
            return(IsThreeLinkPokers);
        }
Exemple #9
0
        public static void sort(PokerGroup Pokers)  //从大到小排序算法
        {
            int i;
            int j;

            for (i = 0; i < Pokers.Count; i++)
            {
                for (j = i + 1; j < Pokers.Count; j++)
                {
                    if (Pokers[j] > Pokers[i])
                    {
                        Poker temp = Pokers[i];
                        Pokers[i] = Pokers[j];
                        Pokers[j] = temp;
                    }
                }
            }
        }
Exemple #10
0
        /// <summary>
        /// 发送牌组给客户端
        /// </summary>
        /// <param name="pg">牌组</param>
        /// <param name="client">客户端的编号,只能是1,2</param>
        /// <returns>是否发送成功</returns>
        public bool SendDataForClient(PokerGroup pg, int client)
        {
            NetworkStream Ns = null;

            if (client == 1)
            {
                Ns = this.client1.GetStream();
            }
            if (client == 2)
            {
                Ns = this.client2.GetStream();//得到客户端的网络流对象
            }
            if (client != 1 && client != 2)
            {
                return(false);
            }
            byte[] bytePg = pg.GetBuffer();     //通过2个 MemoryStream对象获取代表2组牌的 比特流对象
            Ns.Write(bytePg, 0, bytePg.Length); //把2个比特流对象写入server与client的连接管道中
            Thread.Sleep(sleep);
            return(true);
        }
Exemple #11
0
        /// <summary>
        /// 判断一个牌组指定数量相邻的牌是否两两相同
        /// </summary>
        /// <param name="PG">牌组对象</param>
        /// <param name="amount">指定数量的相邻牌组</param>
        /// <returns>指定数量的相邻牌是否两两相同</returns>
        public static bool IsSame(PokerGroup PG, int amount)
        {
            bool IsSame1 = false;
            bool IsSame2 = false;

            for (int i = 0; i < amount - 1; i++) //从大到小比较相邻牌是否相同
            {
                if (PG[i] == PG[i + 1])
                {
                    IsSame1 = true;
                }
                else
                {
                    IsSame1 = false;
                    break;
                }
            }
            for (int i = PG.Count - 1; i > PG.Count - amount; i--)  //从小到大比较相邻牌是否相同
            {
                if (PG[i] == PG[i - 1])
                {
                    IsSame2 = true;
                }
                else
                {
                    IsSame2 = false;
                    break;
                }
            }
            if (IsSame1 || IsSame2)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemple #12
0
        /// <summary>
        /// 接收服务器发送的数据
        /// </summary>
        public void AcceptServerData()
        {
            NetworkStream Ns  = client.GetStream();
            string        str = "";

            while (true)
            {
                byte[] bytes = new byte[108];
                Ns.Read(bytes, 0, 108);
                str = Encoding.Default.GetString(bytes);
                if (str.StartsWith("StartPokers"))
                {
                    DConsole.IsStart = true;
                    str = str.Replace("StartPokers", "");
                    str.Trim();
                    byte[]     bytePokers = Encoding.Default.GetBytes(str);
                    PokerGroup pokers     = new PokerGroup(bytePokers);
                    if (pokers.Count == 17)
                    {
                        DConsole.player1.pokers.Clear();
                        DConsole.player1.pokers = pokers;
                        DConsole.player1.sort();
                        DConsole.player1.Paint();
                    }
                    DConsole.PaintLandLord();
                    continue;
                }
                if (str.StartsWith("CName"))
                {
                    str = str.Replace("CName", "");
                    DConsole.OtherClientName = str;
                    continue;
                }
                if (str.StartsWith("SPokerCount"))
                {
                    str = str.Replace("SPokerCount", "");
                    int pokerCount = Convert.ToInt32(str);
                    DConsole.PaintServer(pokerCount);
                    continue;
                }
                if (str.StartsWith("PokerCount"))
                {
                    str = str.Replace("PokerCount", "");
                    int pokerCount = Convert.ToInt32(str);
                    DConsole.PaintClient(pokerCount);
                    continue;
                }
                if (str.StartsWith("YouAreClient1"))
                {
                    DConsole.ChangePlace();
                    continue;
                }
                if (str.StartsWith("EveryOneIsOk"))
                {
                    this.everyIsOk = true;
                    continue;
                }
                if (str.StartsWith("server"))
                {
                    PokerGroup pokers = new PokerGroup();
                    str = str.Replace("server", "");
                    byte[] bytePg = Encoding.Default.GetBytes(str);
                    pokers.GetPokerGroup(bytePg);
                    DConsole.leadedPokerGroups.Add(pokers);
                    DConsole.WriteLeadedPokers();
                    DConsole.PaintPlayer2LeadPoker(pokers);
                    continue;
                }
                if (str.StartsWith("client"))
                {
                    PokerGroup pokers = new PokerGroup();
                    str = str.Replace("client", "");
                    byte[] bytePg = Encoding.Default.GetBytes(str);
                    pokers.GetPokerGroup(bytePg);
                    DConsole.leadedPokerGroups.Add(pokers);
                    DConsole.WriteLeadedPokers();
                    DConsole.PaintPlayer3LeadPoker(pokers);
                    continue;
                }
                if (str.StartsWith("Order"))
                {
                    DConsole.player1.haveOrder = true;
                    continue;
                }
                if (str.StartsWith("ClientPass"))
                {
                    DConsole.gPlayer3LeadPoker.Clear(DConsole.backColor);
                    DConsole.gPlayer3LeadPoker.DrawString("不要", new System.Drawing.Font("宋体", 20), System.Drawing.Brushes.Red, 5, 5);
                    continue;
                }
                if (str.StartsWith("ServerPass"))
                {
                    DConsole.gPlayer2LeadPoker.Clear(DConsole.backColor);
                    DConsole.gPlayer2LeadPoker.DrawString("不要", new System.Drawing.Font("宋体", 20), System.Drawing.Brushes.Red, 5, 5);
                    continue;
                }
                if (str.StartsWith("IsBiggest"))
                {
                    DConsole.player1.isBiggest = true;
                    continue;
                }
                if (str.StartsWith("NoBiggest"))
                {
                    DConsole.player1.isBiggest = false;
                    continue;
                }
                if (str.StartsWith("AreYouLandLord"))
                {
                    DConsole.player1.areYouLandLord = true;
                    continue;
                }
                if (str.StartsWith("LandLordPokers"))
                {
                    PokerGroup pokers = new PokerGroup();
                    str = str.Replace("LandLordPokers", "");
                    byte[] bytePg = Encoding.Default.GetBytes(str);
                    pokers.GetPokerGroup(bytePg);
                    DConsole.LandLordPokers = pokers;
                    DConsole.player1.SelectLandLordEnd();
                    continue;
                }
                if (str.StartsWith("ClientIsLandLord"))
                {
                    DConsole.lblClient2Name.Text     += "(地主)";
                    DConsole.lblClient2Name.ForeColor = System.Drawing.Color.Red;
                    DConsole.PaintClient(20);
                    continue;
                }
                if (str.StartsWith("ServerIsLandLord"))
                {
                    DConsole.lblClient1Name.Text     += "(地主)";
                    DConsole.lblClient1Name.ForeColor = System.Drawing.Color.Red;
                    DConsole.PaintServer(20);
                    continue;
                }
                if (str.StartsWith("ReStart"))
                {
                    DConsole.leadedPokerGroups.Clear();
                    DConsole.leadPokers.Clear();
                    DConsole.player1.pokers.Clear();
                    DConsole.player1.areYouLandLord   = false;
                    DConsole.player1.isBiggest        = false;
                    DConsole.player1.isLandLord       = false;
                    DConsole.player1.haveOrder        = false;
                    DConsole.lblClient1Name.Text      = DConsole.lblClient1Name.Text.Replace("(地主)", "");
                    DConsole.lblClient2Name.Text      = DConsole.lblClient2Name.Text.Replace("(地主)", "");
                    DConsole.lblClient1Name.ForeColor = System.Drawing.Color.Black;
                    DConsole.lblClient2Name.ForeColor = System.Drawing.Color.Black;
                    DConsole.PaintLandLord(false);
                    DConsole.IsRestart = true;
                    continue;
                }
                if (str.StartsWith("YouScore"))
                {
                    str = str.Replace("YouScore", "");
                    DConsole.serverScore = Convert.ToInt32(str);
                    continue;
                }
                if (str.StartsWith("ClientScore"))
                {
                    str = str.Replace("ClientScore", "");
                    DConsole.client2Score = Convert.ToInt32(str);
                    continue;
                }
                if (str.StartsWith("ServerScore"))
                {
                    str = str.Replace("ServerScore", "");
                    DConsole.client1Score = Convert.ToInt32(str);
                    continue;
                }
            }
        }
Exemple #13
0
        /// <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);
                if (str1.StartsWith("Name"))
                {
                    str1             = str1.Replace("Name", "");
                    this.client1Name = str1;
                    this.SendDataForClient("CName" + str1, 2);
                    continue;
                }
                if (str1.StartsWith("OK"))
                {
                    this.client1IsOk = true;
                    continue;
                }
                if (str1.StartsWith("PokerCount"))
                {
                    Thread.Sleep(sleep);
                    SendDataForClient(str1, 2);
                    str1 = str1.Replace("PokerCount", "");
                    int PokerCount = Convert.ToInt32(str1);
                    if (PokerCount == 0 && DConsole.IsStart)
                    {
                        DConsole.Winer = 2;
                        DConsole.Restart();
                    }
                    DConsole.PaintClient(PokerCount, 1);
                    continue;
                }
                if (str1.StartsWith("client"))
                {
                    SendDataForClient(str1, 2);
                    Thread.Sleep(sleep);
                    str1 = str1.Replace("client", "");
                    pg.GetPokerGroup(Encoding.Default.GetBytes(str1));
                    DConsole.leadedPokerGroups.Add(pg);
                    DConsole.PaintPlayer2LeadPoker(pg);
                    DConsole.WriteLeadedPokers();
                    if (!DConsole.IsRestart)
                    {
                        DConsole.player1.haveOrder = true;  //client1出牌后归server出牌,前提是没有Restart,Restart后出牌权限消失
                    }
                    else
                    {
                        DConsole.IsRestart = 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);
                    continue;
                }
                if (str1.StartsWith("IamIsBiggest"))
                {
                    DConsole.player1.isBiggest = false;
                    this.SendDataForClient("NoBiggest", 2);
                    continue;
                }
                if (str1.StartsWith("AreYouLandLord"))
                {
                    if (DConsole.LandLordNum == 1)
                    {
                        DConsole.Restart();
                        continue;
                    }
                    DConsole.player1.areYouLandLord = true;
                    continue;
                }
                if (str1.StartsWith("IamLandLord"))
                {
                    DConsole.lblClient1Name.Text     += "(地主)";
                    DConsole.lblClient1Name.ForeColor = System.Drawing.Color.Red;
                    SendDataForClient("ClientIsLandLord", 2);
                    Thread.Sleep(sleep);
                    DConsole.PaintClient(20, 1);
                    SendDataForClient("LandLordPokers", DConsole.LandLordPokers, 1);
                    Thread.Sleep(sleep);
                    SendDataForClient("LandLordPokers", DConsole.LandLordPokers, 2);
                    DConsole.player1.SelectLandLordEnd();
                    continue;
                }
            }
        }
Exemple #14
0
        /// <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);
                if (str1.StartsWith("Name"))
                {
                    str1             = str1.Replace("Name", "");
                    this.client2Name = str1;
                    this.SendDataForClient("CName" + str1, 1);
                    continue;
                }
                if (str1.StartsWith("OK"))
                {
                    this.client2IsOk = true;
                    continue;
                }
                if (str1.StartsWith("PokerCount"))
                {
                    Thread.Sleep(sleep);
                    SendDataForClient(str1, 1);
                    str1 = str1.Replace("PokerCount", "");
                    int PokerCount = Convert.ToInt32(str1);
                    if (PokerCount == 0 && DConsole.IsStart)
                    {
                        DConsole.Winer = 3;
                        DConsole.Restart();
                    }
                    DConsole.PaintClient(PokerCount, 2);
                    continue;
                }
                if (str1.StartsWith("client"))
                {
                    SendDataForClient(str1, 1);
                    Thread.Sleep(sleep);
                    str1 = str1.Replace("client", "");
                    pg.GetPokerGroup(Encoding.Default.GetBytes(str1));
                    DConsole.leadedPokerGroups.Add(pg);
                    DConsole.PaintPlayer3LeadPoker(pg);
                    DConsole.WriteLeadedPokers();
                    if (!DConsole.IsRestart)
                    {
                        SendDataForClient("Order", 1);  //权限交给client1 前提是没有Restart
                    }
                    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);
                    Thread.Sleep(500);
                    SendDataForClient("Order", 1);
                    continue;
                }
                if (str1.StartsWith("IamIsBiggest"))
                {
                    DConsole.player1.isBiggest = false;
                    this.SendDataForClient("NoBiggest", 1);
                    continue;
                }
                if (str1.StartsWith("AreYouLandLord"))
                {
                    if (DConsole.LandLordNum == 2)
                    {
                        DConsole.Restart();
                        continue;
                    }
                    SendDataForClient("AreYouLandLord", 1);
                    continue;
                }
                if (str1.StartsWith("IamLandLord"))
                {
                    DConsole.lblClient2Name.Text     += "(地主)";
                    DConsole.lblClient2Name.ForeColor = System.Drawing.Color.Red;
                    SendDataForClient("LandLordPokers", DConsole.LandLordPokers, 1);
                    SendDataForClient("LandLordPokers", DConsole.LandLordPokers, 2);
                    SendDataForClient("ClientIsLandLord", 1);
                    DConsole.PaintClient(20, 2);
                    DConsole.player1.SelectLandLordEnd();
                    continue;
                }
            }
        }
Exemple #15
0
        /// <summary>
        /// 对飞机和飞机带翅膀进行排序,把飞机放在前面,翅膀放在后面.
        /// </summary>
        /// <param name="PG">牌组</param>
        /// <returns>是否为连续三张牌</returns>
        public static PokerGroup SameThreeSort(PokerGroup PG)
        {
            Poker      FourPoker      = null;             //如果把4张当三张出并且带4张的另外一张,就需要特殊处理,这里记录出现这种情况的牌的点数.
            bool       FindedThree    = false;            //已找到三张相同的牌
            PokerGroup tempPokerGroup = new PokerGroup(); //记录三张相同的牌
            int        count          = 0;                //记录在连续三张牌前面的翅膀的张数
            int        Four           = 0;                // 记录是否连续出现三三相同,如果出现这种情况则表明出现把4张牌(炸弹)当中的三张和其他牌配成飞机带翅膀,并且翅膀中有炸弹牌的点数.

            // 比如有如下牌组: 998887777666 玩家要出的牌实际上应该为 888777666带997,但是经过从大到小的排序后变成了998887777666 一不美观,二不容易比较.
            for (int i = 2; i < PG.Count; i++)                //直接从2开始循环,因为PG[0],PG[1]的引用已经存储在其他变量中,直接比较即可
            {
                if (PG[i] == PG[i - 2] && PG[i] == PG[i - 1]) // 比较PG[i]与PG[i-1],PG[i]与PG[i-2]是否同时相等,如果相等则说明这是三张相同牌
                {
                    if (Four >= 1)                            //默认的Four为0,所以第一次运行时这里为false,直接执行else
                                                              //一旦连续出现两个三三相等,就会执行这里的if
                    {
                        FourPoker = PG[i];                    //当找到四张牌时,记录下4张牌的点数
                        Poker changePoker;
                        for (int k = i; k > 0; k--)           //把四张牌中的一张移动到最前面.
                        {
                            changePoker = PG[k];
                            PG[k]       = PG[k - 1];
                            PG[k - 1]   = changePoker;
                        }
                        count++; //由于此时已经找到三张牌,下面为count赋值的程序不会执行,所以这里要手动+1
                    }
                    else
                    {
                        Four++;                    //记录本次循环,因为本次循环找到了三三相等的牌,如果连续两次找到三三相等的牌则说明找到四张牌(炸弹)
                        tempPokerGroup.Add(PG[i]); //把本次循环的PG[i]记录下来,即记录下三张牌的点数
                    }
                    FindedThree = true;            //标记已找到三张牌
                }
                else
                {
                    Four = 0;         //没有找到时,连续找到三张牌的标志Four归零
                    if (!FindedThree) //只有没有找到三张牌时才让count增加.如果已经找到三张牌,则不再为count赋值.
                    {
                        count = i - 1;
                    }
                }
            }
            foreach (Poker tempPoker in tempPokerGroup) //迭代所有的三张牌点数
            {
                Poker changePoker;                      //临时交换Poker
                for (int i = 0; i < PG.Count; i++)      //把所有的三张牌往前移动
                {
                    if (PG[i] == tempPoker)             //当PG[i]等于三张牌的点数时
                    {
                        if (PG[i] == FourPoker)         //由于上面已经把4张牌中的一张放到的最前面,这张牌也会与tempPoker相匹配所以这里进行处理
                                                        // 当第一次遇到四张牌的点数时,把记录四张牌的FourPoker赋值为null,并中断本次循环.由于FourPoker已经为Null,所以下次再次遇到四张牌的点数时会按照正常情况执行.
                        {
                            FourPoker = null;
                            continue;
                        }
                        changePoker   = PG[i - count];
                        PG[i - count] = PG[i];
                        PG[i]         = changePoker;
                    }
                }
            }
            return(PG);
        }
Exemple #16
0
        /// <summary>
        /// 验证所出牌组是否符合游戏规则
        /// </summary>
        public static bool IsRules(PokerGroup leadPokers) //判断所出牌组类型以及其是否符合规则
        {
            bool isRule = false;

            Player.sort(leadPokers);
            switch (leadPokers.Count)
            {
            case 0:
                isRule = false;
                break;

            case 1:
                isRule          = true;
                leadPokers.type = PokerGroupType.单张;
                break;

            case 2:
                if (IsSame(leadPokers, 2))
                {
                    isRule          = true;
                    leadPokers.type = PokerGroupType.对子;
                }
                else
                {
                    if (leadPokers[0] == PokerNum.大王 && leadPokers[1] == PokerNum.小王)
                    {
                        leadPokers.type = PokerGroupType.双王;
                        isRule          = true;
                    }
                    else
                    {
                        isRule = false;
                    }
                }
                break;

            case 3:
                if (IsSame(leadPokers, 3))
                {
                    leadPokers.type = PokerGroupType.相同;
                    isRule          = true;
                }
                else
                {
                    isRule = false;
                }
                break;

            case 4:
                if (IsSame(leadPokers, 4))
                {
                    leadPokers.type = PokerGroupType.炸弹;
                    isRule          = true;
                }
                else
                {
                    if (IsThreeLinkPokers(leadPokers))
                    {
                        leadPokers.type = PokerGroupType.带一;
                        isRule          = true;
                    }
                    else
                    {
                        isRule = false;
                    }
                }
                break;

            case 5:
                if (IsStraight(leadPokers))
                {
                    leadPokers.type = PokerGroupType.五张顺子;
                    isRule          = true;
                }
                else
                {
                    isRule = false;
                }
                break;

            case 6:
                if (IsStraight(leadPokers))
                {
                    leadPokers.type = PokerGroupType.六张顺子;
                    isRule          = true;
                }
                else
                {
                    if (IsLinkPair(leadPokers))
                    {
                        leadPokers.type = PokerGroupType.连对;
                        isRule          = true;
                    }
                    else
                    {
                        if (IsSame(leadPokers, 4))
                        {
                            leadPokers.type = PokerGroupType.四带二;
                            isRule          = true;
                        }
                        else
                        {
                            if (IsThreeLinkPokers(leadPokers))
                            {
                                leadPokers.type = PokerGroupType.二连飞机;
                                isRule          = true;
                            }
                            else
                            {
                                isRule = false;
                            }
                        }
                    }
                }
                break;

            case 7:
                if (IsStraight(leadPokers))
                {
                    leadPokers.type = PokerGroupType.七张顺子;
                    isRule          = true;
                }
                else
                {
                    isRule = false;
                }
                break;

            case 8:
                if (IsStraight(leadPokers))
                {
                    leadPokers.type = PokerGroupType.八张顺子;
                    isRule          = true;
                }
                else
                {
                    if (IsLinkPair(leadPokers))
                    {
                        leadPokers.type = PokerGroupType.四连对;
                        isRule          = true;
                    }
                    else
                    {
                        if (IsThreeLinkPokers(leadPokers))
                        {
                            leadPokers.type = PokerGroupType.飞机带翅膀;
                            isRule          = true;
                        }
                        else
                        {
                            isRule = false;
                        }
                    }
                }
                break;

            case 9:
                if (IsStraight(leadPokers))
                {
                    leadPokers.type = PokerGroupType.九张顺子;
                    isRule          = true;
                }
                else
                {
                    if (IsThreeLinkPokers(leadPokers))
                    {
                        leadPokers.type = PokerGroupType.连飞机;
                    }
                    else
                    {
                        isRule = false;
                    }
                }
                break;

            case 10:
                if (IsStraight(leadPokers))
                {
                    leadPokers.type = PokerGroupType.十张顺子;
                    isRule          = true;
                }
                else
                {
                    if (IsLinkPair(leadPokers))
                    {
                        leadPokers.type = PokerGroupType.五连对;
                        isRule          = true;
                    }
                    else
                    {
                        isRule = false;
                    }
                }
                break;

            case 11:
                if (IsStraight(leadPokers))
                {
                    leadPokers.type = PokerGroupType.十一张顺子;
                    isRule          = true;
                }
                else
                {
                    isRule = false;
                }
                break;

            case 12:
                if (IsStraight(leadPokers))
                {
                    leadPokers.type = PokerGroupType.十二张顺子;
                    isRule          = true;
                }
                else
                {
                    if (IsLinkPair(leadPokers))
                    {
                        leadPokers.type = PokerGroupType.六连对;
                        isRule          = true;
                    }
                    else
                    {
                        if (IsThreeLinkPokers(leadPokers))
                        {
                            //12有三连飞机带翅膀和四连飞机两种情况,所以在IsThreeLinkPokers中做了特殊处理,此处不用给type赋值.
                            isRule = true;
                        }
                        else
                        {
                            isRule = false;
                        }
                    }
                }
                break;

            case 13:
                isRule = false;
                break;

            case 14:
                if (IsLinkPair(leadPokers))
                {
                    leadPokers.type = PokerGroupType.七连对;
                    isRule          = true;
                }
                else
                {
                    isRule = false;
                }
                break;

            case 15:
                if (IsThreeLinkPokers(leadPokers))
                {
                    leadPokers.type = PokerGroupType.五连飞机;
                    isRule          = true;
                }
                else
                {
                    isRule = false;
                }
                break;

            case 16:
                if (IsLinkPair(leadPokers))
                {
                    leadPokers.type = PokerGroupType.八连对;
                    isRule          = true;
                }
                else
                {
                    if (IsThreeLinkPokers(leadPokers))
                    {
                        leadPokers.type = PokerGroupType.四连飞机带翅膀;
                        isRule          = true;
                    }
                    else
                    {
                        isRule = false;
                    }
                }
                break;

            case 17:
                isRule = false;
                break;

            case 18:
                if (IsLinkPair(leadPokers))
                {
                    leadPokers.type = PokerGroupType.六连对;
                    isRule          = true;
                }
                else
                {
                    if (IsThreeLinkPokers(leadPokers))
                    {
                        leadPokers.type = PokerGroupType.六连飞机;
                        isRule          = true;
                    }
                    else
                    {
                        isRule = false;
                    }
                }
                break;

            case 19:
                isRule = false;
                break;

            case 20:
                if (IsLinkPair(leadPokers))
                {
                    leadPokers.type = PokerGroupType.十连对;
                    isRule          = true;
                }
                else
                {
                    if (IsThreeLinkPokers(leadPokers))
                    {
                        leadPokers.type = PokerGroupType.五连飞机带翅膀;
                        isRule          = true;
                    }
                    else
                    {
                        isRule = false;
                    }
                }
                break;
            }
#if DEBUG
            Console.WriteLine("玩家出的牌:");
            foreach (Poker Poker in leadPokers)
            {
                Write(Poker.pokerColor.ToString() + Poker.ToString());
            }
#endif
            return(isRule);
        }