(德州撲克誕生記)3、我的心哪!我是怎麼沒滴?

核心類庫講習


using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace 德州撲克.Lib
{
    class DeZhouPuKe
    {
        //紀錄用戶和權值,你知道SortedList是幹什麼的嗎?百度吧
        public static System.Collections.SortedList stData = new SortedList();
       
        #region 函數庫
        /// <summary>
        /// 排序函數,針對整形數據
        /// </summary>
        /// <param name="list"></param>
        public static void Sort(int[] list)
        {
            int i, j, temp;
            bool done = false;
            j = 1;
            while ((j < list.Length) && (!done))
            {
                done = true;
                for (i = 0; i < list.Length - j; i++)
                {
                    if (list[i] > list[i + 1])
                    {
                        done = false;
                        temp = list[i];
                        list[i] = list[i + 1];
                        list[i + 1] = temp;
                    }
                }
                j++;
            }
        }


        /// <summary>
        /// 排序函數,針對與權值
        /// </summary>
        /// <param name="list"></param>
        public static void Sort(double[] list)
        {
            int i, j;
            double temp;
            bool done = false;
            j = 1;
            while ((j < list.Length) && (!done))
            {
                done = true;
                for (i = 0; i < list.Length - j; i++)
                {
                    if (list[i] > list[i + 1])
                    {
                        done = false;
                        temp = list[i];
                        list[i] = list[i + 1];
                        list[i + 1] = temp;
                    }
                }
                j++;
            }
        }

        /// <summary>
        /// 爲對子判斷進行數據準備
        /// 返回>1則存在重複的字符串,並返回值,如果小於等於1則不存在重複的字符串返回
        /// 查找某一字符串 在目標字符串裏出現的次數   思想:首先把目標字符串賦給一個字符串,然後把賦值後的字符串
        /// 把源目標字符串替換成空值,這樣把源字符串的總長度減去賦值後的字符後的總長度便是目標字符串的倍數,
        /// 如果小於等於1,則不存在,如果>1則存在,這個函數你會寫嗎?很多陷阱,不信你試試。
        /// </summary>
        /// <param name="Enstr">源字符串</param>
        /// <param name="Destr">目標字符串</param>
        /// <returns></returns>
        public static int StringFindStringCount(string Enstr, string Destr,string spliter)
        {
            int result = 0;

            if (Enstr == null || Enstr.ToString().Trim().Replace("'", "") == "")//當源字符串爲空時
            {
                result = 0;
            }
            else
            {
                if (Destr == null || Destr.ToString().Trim().Replace("'", "") == "")//當目標字符串爲空時
                {
                    result = 0;
                }
                else
                {
                    if (Enstr.Length < Destr.Length)//當源字符串長度小於目標字符串長度時
                    {
                        result = 0;
                    }
                    else
                    {
                        string str = Enstr;
                        //算法安全性處理
                        str = str.Replace(spliter, spliter + spliter);//分隔符號加倍,爲了保險起見,你知道爲什麼嗎?
                        str = spliter + spliter+str + spliter + spliter;//尾部繼續加,爲什麼?
                        int iOldLength = str.Length;

                        str = str.Replace(spliter+Destr + spliter, "");
                        int count = (iOldLength - str.Length);
                        if (count > 0)
                        {
                            result = count / (Destr.Length+2);//如果此值大於1,則說明存在重複字符串,否則不存在重複的字符串,妙處!!!
                        }
                        else
                        {
                            result = 0;
                        }
                    }
                }
            }

            return result;
        }

        #endregion

 

       //下面這個函數是精華,要仔細看,看看算法是如何搭建的?
        public static string checkPaiType(string s)
        {
            //s = "P,a13,b13,a9,a11,c13";
            string[] arr1 = s.Split(',');
            string UID = arr1[0];                           //獲取UID,每一幅牌的唯一標識
            string sPai = s.Substring(arr1[0].Length + 1);  //牌,帶有花色和點數
            string sDianshu = "";                           //點數字符串序列
            int[] iValue = new int[5];                      //點數數組
            int[] iValueTime = new int[5];                  //點數重複次數數組
            string sValueTimestring = "";                   //重複次數序列;
            string[] sHuase = new string[5];                //花色數組
            string Type = "高牌";                           //類型,默認高牌


            bool isTonghua = false;
            bool isShunzi = false;

            string sReturnNumber1 = "";

            //判斷出現的次數
            string temp = "";
            //第一列是用戶名,第二列到第六列是牌,其餘的忽略
            for (int i = 1; i < 6; i++)
            {
                if (arr1[i].Trim().Length > 0)
                {
                    sHuase[i - 1] = arr1[i].Substring(0, 1);
                    iValue[i - 1] = int.Parse(arr1[i].Substring(1));
                    sDianshu = sDianshu + "," + iValue[i - 1];
                }
            }

            if (sHuase[0] == sHuase[1] && sHuase[3] == sHuase[2] && sHuase[2] == sHuase[3] && sHuase[3] == sHuase[4])
            {
                isTonghua = true;
                Type = "同花";
            }
            //找出對子,3條,葫蘆、四條
            temp = sDianshu+",";

            temp = temp.Replace(",", ",,");//爲什麼要修改成爲雙分隔符,因爲replace的機制。例如"P,a13,b13,a9,a11,c13"

            for (int i = 0; i < iValue.Length; i++)
            {
                iValueTime[i] = StringFindStringCount(temp ,  Convert.ToString(iValue[i]), ",");
                temp = temp.Replace("," + Convert.ToString(iValue[i])+",", ",");
                sValueTimestring = sValueTimestring + "," + iValueTime[i].ToString();
            }
            //四條
            if (sValueTimestring.IndexOf(",4") > -1)
            {
                Type = "四條";
            }
            else
            {
                if (sValueTimestring.IndexOf(",3") > -1)
                {
                    Type = "三條";
                    if (sValueTimestring.IndexOf(",2") > -1)
                    {
                        Type = "葫蘆";
                    }
                }
                else
                {
                    if (sValueTimestring.IndexOf(",2") > -1)
                    {
                        //看對子,如果出現兩個對子,叫兩對,否則叫一對

                        if (StringFindStringCount(sValueTimestring, "2",".") == 2)
                        {
                            Type = "兩對";
                        }
                        else
                        {
                            Type = "一對";
                        }
                    }
                    else
                    {
                        Type = "高牌";
                    }
                }
            }
            //排序處理     
            Sort(iValue);
            //順子
            if ((iValue[4] - iValue[3] == 1) && (iValue[3] - iValue[2] == 1) && (iValue[2] - iValue[1] == 1) && (iValue[1] - iValue[0] == 1))
            {
                isShunzi = true;

            }
            if (Type == "高牌")
            {
                if (isTonghua && isShunzi)
                {
                    if (iValue[4] == 14)
                    {
                        Type = "皇家同花順";
                        sReturnNumber1 = "9";
                    }
                    else
                    {
                        Type = "同花順";
                        sReturnNumber1 = "8";
                    }
                }
                else if (isTonghua && !isShunzi)
                {
                    Type = "同花";
                    sReturnNumber1 = "5";
                }
                else if (!isTonghua && isShunzi)
                {
                    Type = "順子";
                    sReturnNumber1 = "4";
                }
                else
                {
                    Type = "高牌";
                    sReturnNumber1 = "0";
                }
            }
            else if (Type == "一對")
            {
                Type = "一對";
                sReturnNumber1 = "1";
            }
            else if (Type == "兩對")
            {
                Type = "兩對";
                sReturnNumber1 = "2";
            }
            else if (Type == "葫蘆")
            {
                Type = "葫蘆";
                sReturnNumber1 = "6";
            }
            else if (Type == "三條")
            {
                Type = "三條";
            }
            else if (Type == "四條")
            {
                Type = "四條";
                sReturnNumber1 = "7";
            }

            return Type;
        }
        /// <summary>
        /// 權值計算,這個適合今後做成網絡對戰
        /// </summary>
        /// <param name="s"></param>
        /// <returns>返回權值:小數點前爲類型,小數點後經過排序之後的數字組合</returns>
        public static double checkDataType(string s)
        {
            string[] arr1 = s.Split(',');
            string UID = arr1[0];                           //獲取UID,每一幅牌的唯一標識
            string sPai = s.Substring(arr1[0].Length + 1);  //牌,帶有花色和點數
            string sDianshu = "";                           //點數字符串序列
            int[] iValue = new int[5];                      //點數數組
            int[] iValueTime = new int[5];                  //點數重複次數數組
            string sValueTimestring = "";                   //重複次數序列;
            string[] sHuase = new string[5];                //花色數組
            string Type = "高牌";                           //類型,默認高牌


            bool isTonghua = false;
            bool isShunzi = false;
           
            string sReturnNumber1 = "";
           
            //判斷出現的次數
            string temp = "";
            //第一列是用戶名,第二列到第六列是牌,其餘的忽略
            for (int i = 1; i < 6; i++)
            {
                if (arr1[i].Trim().Length > 0)
                {
                    sHuase[i - 1] = arr1[i].Substring(0, 1);
                    iValue[i - 1] = int.Parse(arr1[i].Substring(1));
                    sDianshu = sDianshu + "," + iValue[i - 1];
                }
            }

            if (sHuase[0] == sHuase[1] && sHuase[3] == sHuase[2] && sHuase[2] == sHuase[3] && sHuase[3] == sHuase[4])
            {
                isTonghua = true;
                Type = "同花";
            }
            //找出對子,3條,葫蘆、四條
            temp = sDianshu + ",";

            temp = temp.Replace(",", ",,");//爲什麼要修改成爲雙分隔符,因爲replace的機制。例如"P,a13,b13,a9,a11,c13"

            for (int i = 0; i < iValue.Length; i++)
            {
                iValueTime[i] = StringFindStringCount(temp, Convert.ToString(iValue[i]), ",");
                temp = temp.Replace("," + Convert.ToString(iValue[i]) + ",", ",");
                sValueTimestring = sValueTimestring + "," + iValueTime[i].ToString();
            }
            //四條
            if (sValueTimestring.IndexOf(",4") > -1)
            {
                Type = "四條";
            }
            else
            {
                if (sValueTimestring.IndexOf(",3") > -1)
                {
                    Type = "三條";
                    if (sValueTimestring.IndexOf(",2") > -1)
                    {
                        Type = "葫蘆";
                    }
                }
                else
                {
                    if (sValueTimestring.IndexOf(",2") > -1)
                    {
                        //看對子,如果出現兩個對子,叫兩對,否則叫一對

                        if (StringFindStringCount(sValueTimestring, ",2",".") == 2)
                        {
                            Type = "兩對";
                        }
                        else
                        {
                            Type = "一對";
                        }
                    }
                    else
                    {
                        Type = "高牌";
                    }
                }
            }


            //排序處理     
            Sort(iValue);
            //順子
            if ((iValue[4] - iValue[3] == 1) && (iValue[3] - iValue[2] == 1) && (iValue[2] - iValue[1] == 1) && (iValue[1] - iValue[0] == 1))
            {
                isShunzi = true;

            }
            if (Type == "高牌")
            {
                if (isTonghua && isShunzi)
                {
                    if (iValue[4] == 14)
                    {
                        Type = "皇家同花順";
                        sReturnNumber1 = "9";
                    }
                    else
                    {
                        Type = "同花順";
                        sReturnNumber1 = "8";
                    }
                }
                else if (isTonghua && !isShunzi)
                {
                    Type = "同花";
                    sReturnNumber1 = "5";
                }
                else if (!isTonghua && isShunzi)
                {
                    Type = "順子";
                    sReturnNumber1 = "4";
                }
                else
                {
                    Type = "高牌";
                    sReturnNumber1 = "0";
                }

            }
            else if (Type == "一對")
            {
                Type = "一對";
                sReturnNumber1 = "1";
            }
            else if (Type == "兩對")
            {
                Type = "兩對";
                sReturnNumber1 = "2";
            }
            else if (Type == "葫蘆")
            {
                Type = "葫蘆";
                sReturnNumber1 = "6";
            }
            else if (Type == "三條")
            {
                Type = "三條";
            }
            else if (Type == "四條")
            {
                Type = "四條";
                sReturnNumber1 = "7";
            }

            sReturnNumber1 = sReturnNumber1 + ".";
            for (int i = 0; i < iValue.Length; i++)
            {
                sReturnNumber1 = sReturnNumber1 + string.Format("{0:00}", iValue[i]);// 001234

            }
            //重複性判斷,如果有權值相同的,直接補後綴01,如果有後綴,直接後綴加1

            if (!stData.ContainsKey(Convert.ToDouble(sReturnNumber1)))
            {
                stData.Add(Convert.ToDouble(sReturnNumber1), s+"#%1");
            }
            else
            {
                temp = stData[Convert.ToDouble(sReturnNumber1)].ToString();
                temp = temp.Substring(temp.IndexOf("#%") + 2);          //取出出現次數
                temp = Convert.ToString(Convert.ToInt64(temp) + 1);     //重新計算出現次數


                stData.Add(Convert.ToDouble(sReturnNumber1 + string.Format("{0:00}", temp)), s+"#%0");//添加新元素
                //更新原是值中的出現次數
                stData[Convert.ToDouble(sReturnNumber1)] = stData[Convert.ToDouble(sReturnNumber1)].ToString().Substring(0, stData[Convert.ToDouble(sReturnNumber1)].ToString().Length - temp.Length-2)+"#%"+temp;
            }
            return Convert.ToDouble(sReturnNumber1);
        }
      
    }
}

如果您需要所要代碼,請發送郵件給我:[email protected]

也歡迎志同道合的朋友交流技術。

 

本系列到此結束。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章