(德州扑克诞生记)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);
        }
      
    }
}

如果您需要所要代码,请发送邮件给我:jjkcom@sina.com

也欢迎志同道合的朋友交流技术。

 

本系列到此结束。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值