AI智能
改变未来

C# 斗牛核心算法

 扑克牌(0·52)取随机数

[code]using System;using System.Collections;using System.Collections.Generic;public class BullFightTools{public static List<int> usedNumber = new List<int>();private static int[] points = new int[5];/// <summary>/// 开始游戏/// </summary>public static void StartGame(){usedNumber.Clear();}/// <summary>/// 要牌/// </summary>/// <returns></returns>public static int GetNumber(){int number = -1;do{number = new Random().Next(0, 52);} while (usedNumber.Contains(number));usedNumber.Add(number);return number;}/// <summary>/// 类型/// </summary>public enum PokerType{Boom = -1,//炸弹WuXiaoNiu = -2,//五小牛Gold = -3,//金牛Silver = -4,//银牛None = -5 //没牛}public static int CheckType(List<int> list){int sum = 0;//和int TenNum = 0;//10的个数int Count = list.Count;list.Sort();//排序//1、判断是不是炸弹牛 (/4是点数,%4是花色)if ((list[0] / 4) == (list[3] / 4) && (list[1] / 4) == (list[4] / 4)){return (int)PokerType.Boom;}//2、金牛银牛的判断不需要进行数据处理就能够得到if (list[0] >= 40){//金牛return (int)PokerType.Gold;}if (list[0] >= 36){//银牛return (int)PokerType.Silver;}        //3、换成与牌对应的数值for (int i = 0; i < Count; i++){if ((list[i] / 4) > 9){points[i] = 10;}else { points[i] = list[i] / 4 + 1; }}//4、计算 整十数 的数量for (int i = 0; i < Count; i++){if (points[i] >= 10) //10 J Q K 的数量{ TenNum++; }//Debug.Log(\"牌对应的数\" + points[i]);}//5、如果大于等于10的数量超过了3,必定有牛,直接返回牛的点数,不需要求和if (TenNum >= 3) { return (points[0] + points[1]) % 10; }//5、求和 ,五张牌的总值,判断是不是J、Q、K、值为10         for (int i = 0; i < Count; i++){ sum += points[i]; }//Debug.Log(\"sum = \" + sum);//6、判断五小牛if (sum < 10) { return (int)PokerType.WuXiaoNiu; }//7、计算其他牛的点数,有牛的一般情况  包含  (1,3,8,9,10)(5,7,8 ,10,10)类型for (int i = 0; i < Count - 1; i++){for (int j = i + 1; j < Count; j++){                //剔除两个数字之后能够整除10即有牛,返回牛的点数if ((sum - (points[i] + points[j])) % 10 == 0){//Debug.Log(\"第一种情况\");return (points[i] + points[j]) % 10;}}}//8、有一个整十数的特殊情况   2,3,4,5,10  包含2,5,7,10,10if (TenNum == 1){for (int i = 0; i < Count; i++){if (sum - 10 - points[i] % 10 == 0) //有牛的情况{//Debug.Log(\"第二种情况\");return (sum - 10) % 10; //返回牛几}}}//9、有两个整十数的特殊情况 2,8,3,10,10,for (int i = 0; i < Count - 1 - TenNum; i++){for (int j = i + 1; j < Count - TenNum; j++){if ((points[i] + points[j]) == 10){for (int k = 0; k < TenNum; k++){sum -= 10;Debug.Log(\"第三种情况\");return sum % 10; //返回牛几}}}}//8、没牛return (int)PokerType.None;}}

 

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » C# 斗牛核心算法