checkhu 第一稿

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Collections;

 

namespace CheckHu

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        // public int zuizongZhuheIndex = 0;

        public string Pub_Tmp_Zhuhe;

        public string Pub_Tmp_ZhuheAll;

        public string Pub_Tmp_YupaiZhuheAll;

        public string Pub_Tmp_YupaiZhuhe;

        public int Pub_BaidaNum;

        public bool Pub_IsHu;

        public bool Pub_IsDaHU;

        public bool Pub_IsJiangNeed;

        public string JiangNeedValue;

 

 

        private void button1_Click(object sender, EventArgs e)

        {

            List<int> Pailist = new List<int>();

            Pailist.Add(51);

            Pailist.Add(52);

            Pailist.Add(53);

            Pailist.Add(54);

            Pailist.Add(55);

            Pailist.Add(56);

            Pailist.Add(56);

            Pailist.Add(56);

            Pailist.Add(58);

            Pailist.Add(58);

            Pailist.Add(59);

            Pailist.Add(59);

            Pailist.Add(59);

            Pailist.Add(1000);

 

            DateTime beforDT = System.DateTime.Now;

 

            //耗时巨大的代码

            for (int i = 0; i < 1; i++)//测算胡牌运行时间

            {

                CheckHU_Normal_Baida(Pailist);

            }

 

            DateTime afterDT = System.DateTime.Now;

            TimeSpan ts = afterDT.Subtract(beforDT);

            label1.Text = ts.TotalMilliseconds.ToString();

 

            //Pailist.Add(51);

            //Pailist.Add(53);

            //Pailist.Add(55);

            //Pailist.Add(55);

            //Pailist.Add(58);

            //Pailist.Add(58);

            //Pailist.Add(1000);

 

            //Pub_Tmp_YupaiZhuhe = "";

            // Pub_Tmp_YupaiZhuheAll = "";

            //GetXX(Pailist, Baidanum);

        }

 

        bool  CheckHU_Normal_Baida(List<int> _pailist)

        {

            Pub_Tmp_Zhuhe = "";

            Pub_Tmp_ZhuheAll = "";

            Pub_Tmp_YupaiZhuheAll = "";

            Pub_Tmp_YupaiZhuhe = "";

            Pub_IsHu = false;

            Pub_BaidaNum = 0;

            //JiangNeed = "2,5,8";

            for (int i = _pailist.Count - 1; i >= 0; i--)

            {

                if (_pailist[i] == 1000) { Pub_BaidaNum++; _pailist.RemoveAt(i); }

            }

            GetXXX(_pailist);

            return Pub_IsHu;

        }

 

        void GetXXX(List<int> _pailist)

        {

            //if (Pub_IsHu) { return; }

 

            string Yupai = "";//输出用

 

            _pailist.Sort();

            List<int> Tlist = new List<int>(_pailist.ToArray());//复制副本

            List<int> Vlist = new List<int>(_pailist.ToArray());//复制副本

                                                                //_pailist.ForEach(lx => Vlist.Add(lx));

 

            for (int i = Vlist.Count - 1; i > 0; i--)//去重复,减少循环次数

            {

                if (Vlist[i].Equals(Vlist[i - 1])) { Vlist.RemoveAt(i); }

            }

            //string tmp = "";

            //for (int i = 0; i < Vlist.Count; i++)

            //{

            //    tmp += Vlist[i];

            //}

 

            List<string> found_Zhuhe = new List<string>();

            int maxshun = 0;

            for (int i = 0; i < Vlist.Count; i++)

            {

                int TT = (int)Vlist[i];

 

                if (Tlist.Contains(TT + 1) && Tlist.Contains(TT + 2) && TT < 60) //发现顺子;小于60指字牌和花牌不计顺

                {

                    if (maxshun == 0 || (TT - maxshun) < 3)//优化递归

                    {

                        found_Zhuhe.Add("S" + (TT).ToString());

                        maxshun = TT;

                    }

                }

                else if (Tlist.Count(x => x == TT) >= 3)//发现刻子

                {

                    found_Zhuhe.Add("K" + (TT).ToString());

                }

            }

 

            if (found_Zhuhe.Count == 0)//如果已经提取组合完毕

            {

                Yupai = "";

                for (int ii = 0; ii < Tlist.Count; ii++)//输出每一种组合后最终的余牌

                {

                    Yupai += Tlist[ii] + ",";

                }

                bool isIn_ZhuheAll = true;

                string[] zhuheAllSplit = Pub_Tmp_ZhuheAll.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);

                string[] zhuheSplit = Pub_Tmp_Zhuhe.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);

                for (int i = 0; i < zhuheSplit.Length; i++)

                {

                    if (!zhuheAllSplit.Contains(zhuheSplit[i]))

                    {

                        Pub_Tmp_ZhuheAll += "|" + zhuheSplit[i];

                        isIn_ZhuheAll = false;

                    }

                }

                if (isIn_ZhuheAll == false || Pub_Tmp_ZhuheAll.Count() == 0)

                {

                    // listBox1.Items.Add(zuizongZhuheIndex.ToString() + ":" + Pub_Tmp_Zhuhe);

                    // listBox2.Items.Add(zuizongZhuheIndex.ToString() + ":" + Yupai );

                    Pub_Tmp_YupaiZhuheAll = "";

                    GetXX(Tlist);

                }

 

            }

            else//如果还有组合可以提取

            {

                for (int i = 0; i < found_Zhuhe.Count; i++)

                {

                    if (found_Zhuhe[i].Substring(0, 1) == "K")

                    {

                        string k = found_Zhuhe[i].Substring(1);

                        Tlist.Remove(int.Parse(k)); Tlist.Remove(int.Parse(k)); Tlist.Remove(int.Parse(k));

                        Pub_Tmp_Zhuhe += "|K" + k;

                        GetXXX(Tlist);

                        Pub_Tmp_Zhuhe = Pub_Tmp_Zhuhe.Remove(Pub_Tmp_Zhuhe.LastIndexOf("|"));

                        Tlist.Add(int.Parse(k)); Tlist.Add(int.Parse(k)); Tlist.Add(int.Parse(k));

                        Tlist.Sort();

                    }

                    else if (found_Zhuhe[i].Substring(0, 1) == "S")

                    {

                        string s = found_Zhuhe[i].Substring(1);

                        Tlist.Remove(int.Parse(s)); Tlist.Remove(int.Parse(s) + 1); Tlist.Remove(int.Parse(s) + 2);

                        Pub_Tmp_Zhuhe += "|S" + int.Parse(s).ToString();

                        GetXXX(Tlist);

                        Pub_Tmp_Zhuhe = Pub_Tmp_Zhuhe.Remove(Pub_Tmp_Zhuhe.LastIndexOf("|"));

                        Tlist.Add(int.Parse(s)); Tlist.Add(int.Parse(s) + 1); Tlist.Add(int.Parse(s) + 2);

                        Tlist.Sort();

                    }

                }

            }

        }

 

        void GetXX(List<int> _Yupailist)

        {

          //  if (Pub_IsHu) { return; }

 

            string ShengPai = "";

            //int Jiang = 0;

            //if ((_Yupailist.Count + _Pub_BaidaNum) == 2) { return; }

 

            List<string> found_Zhuhe = new List<string>();

 

            List<int> Tlist = new List<int>(_Yupailist.ToArray());//复制副本

            List<int> Vlist = new List<int>(_Yupailist.ToArray());//复制副本

                                                                  //_pailist.ForEach(lx => Vlist.Add(lx));

 

            for (int i = Vlist.Count - 1; i > 0; i--)//去重复,减少循环次数

            {

                if (Vlist[i].Equals(Vlist[i - 1])) { Vlist.RemoveAt(i); }

            }

 

            for (int i = 0; i < Vlist.Count; i++)

            {

                int TT = (int)Vlist[i];

                if (Tlist.Count(x => x == TT) > 1)//发现对子

                {

                    found_Zhuhe.Add("D" + (TT).ToString());

                }

                if (Tlist.Contains(TT + 1))//发现相邻

                {

                    found_Zhuhe.Add("Z" + TT.ToString());

                }

                if (Tlist.Contains(TT + 2))//发现隔一

                {

                    found_Zhuhe.Add("z" + TT.ToString());

                }

            }

 

            if (found_Zhuhe.Count == 0)//递归出口

            {

                ShengPai = "";

                for (int ii = 0; ii < Tlist.Count; ii++)//输出每一种组合后最终的剩牌

                {

                    ShengPai += "|X" + Tlist[ii];

                }

 

                bool isIn_ZhuheAll = true;

                string[] zhuheAllSplit = Pub_Tmp_YupaiZhuheAll.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);

                string[] zhuheSplit = Pub_Tmp_YupaiZhuhe.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);

                for (int i = 0; i < zhuheSplit.Length; i++)//只要出现过的组合不再重复列出

                {

                    if (!zhuheAllSplit.Contains(zhuheSplit[i]))

                    {

                        Pub_Tmp_YupaiZhuheAll += "|" + zhuheSplit[i];

                        isIn_ZhuheAll = false;

                    }

                }

                #region  //计算百搭个数是否满足胡牌条件

                if (isIn_ZhuheAll == false || Pub_Tmp_YupaiZhuheAll == "")//当新的组合时

                {

                    bool tmp_IsHu=false;

                    int Xnum = Tlist.Count;

                    int Dnum = Pub_Tmp_YupaiZhuhe.Count(num => num == 'D');

                    int Znum = Pub_Tmp_YupaiZhuhe.Count(num => num == 'Z') + Pub_Tmp_YupaiZhuhe.Count(num => num == 'z');

                    if (Xnum == 0 && Dnum == 0)//当有单张或一对时

                    {

                        if (Pub_BaidaNum >= (Znum + 2))

                        {

                            tmp_IsHu = true;

                        }

                    }

                    else

                    {

                        if (Pub_BaidaNum >= (Znum + Dnum - 1 + Xnum * 2)) { tmp_IsHu = true; }

                    }

                    if (tmp_IsHu) { Pub_IsHu = true; }//只要有一种组合方案可以胡牌,该牌型即算胡了

 

                    listBox1.Items.Add(Pub_Tmp_Zhuhe);//输出XXX组合的提取情况

                    listBox2.Items.Add(Pub_Tmp_YupaiZhuhe + ShengPai + ";" + Pub_BaidaNum.ToString() + tmp_IsHu.ToString());//输出剩牌情况

                }

                #endregion 

            }

 

            else//继续找下一层的组合

            {

                for (int i = 0; i < found_Zhuhe.Count; i++)

                {

                    if (found_Zhuhe[i].Substring(0, 1) == "D")

                    {

                        string D = found_Zhuhe[i].Substring(1);

                        Tlist.Remove(int.Parse(D)); Tlist.Remove(int.Parse(D));

                        Pub_Tmp_YupaiZhuhe += "|D" + D;

                        GetXX(Tlist);

                        Pub_Tmp_YupaiZhuhe = Pub_Tmp_YupaiZhuhe.Remove(Pub_Tmp_YupaiZhuhe.LastIndexOf("|"));

                        Tlist.Add(int.Parse(D)); Tlist.Add(int.Parse(D));

                        Tlist.Sort();

                    }

                    else if (found_Zhuhe[i].Substring(0, 1) == "Z")

                    {

                        string Z = found_Zhuhe[i].Substring(1);

                        Tlist.Remove(int.Parse(Z)); Tlist.Remove(int.Parse(Z) + 1);

                        Pub_Tmp_YupaiZhuhe += "|Z" + int.Parse(Z).ToString();

                        GetXX(Tlist);

                        Pub_Tmp_YupaiZhuhe = Pub_Tmp_YupaiZhuhe.Remove(Pub_Tmp_YupaiZhuhe.LastIndexOf("|"));

                        Tlist.Add(int.Parse(Z)); Tlist.Add(int.Parse(Z) + 1);

                        Tlist.Sort();

                    }

                    else if (found_Zhuhe[i].Substring(0, 1) == "z")

                    {

                        string z = found_Zhuhe[i].Substring(1);

                        Tlist.Remove(int.Parse(z)); Tlist.Remove(int.Parse(z) + 2);

                        Pub_Tmp_YupaiZhuhe += "|z" + int.Parse(z).ToString();

                        GetXX(Tlist);

                        Pub_Tmp_YupaiZhuhe = Pub_Tmp_YupaiZhuhe.Remove(Pub_Tmp_YupaiZhuhe.LastIndexOf("|"));

                        Tlist.Add(int.Parse(z)); Tlist.Add(int.Parse(z) + 2);

                        Tlist.Sort();

                    }

                }

            }

        }

 

        //int IsJiang(int _pai1, int _pai2)//暂时用不到

        //{

        //    if (_pai1 == _pai2 && _pai1 == 1000) { return 1000; }

        //    if (_pai1 == _pai2 && _pai1 != 1000) { return _pai1; }

        //    return 0;

        //}

 

    }

}

 

 

转载于:https://my.oschina.net/u/3516766/blog/916497

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值