牛牛算法的实现

/*牛牛规则:

方块A,2,3---K:  
对应编码顺序 101,102,103-113
梅花A,2,3---K:	
对应编码顺序 201,202,203-213
红桃A,2,3---K:	
对应编码顺序 301,302,303-313
黑桃A,2,3---K:	
对应编码顺序 401,402,403-413
小鬼:对应编码 500大鬼:对应编码 600
游戏使用54张牌,最多可6人同时游戏,每手牌发5张牌。
将自己手上的5张牌进行3+2的模式组合,特殊牌型无需组牌。
鬼牌可以代替其他任意牌
有牛:
如果其中3张牌数值相加是10的倍数,则剩余2张牌相加之和取个位数即为牛X。
例如:4/9/8/7/K,(4+7+9=20)(K+8=18),此牌型为牛八;
无牛:
    5张牌中任意3张之和都不能为10的倍数,则判定为无牛;牛牛
    如果在有牛的情况下,剩余2张牌相加之和也是10的倍数即为牛牛牌型,例如:4/9/7/10/Q,10+Q=20,则为牛牛;
特殊牛:
    特殊牌型:五小牛>四炸>五花牛
五小牛:所有单张<5,点数总和<=10
四炸:有4张相同牌
五花牛:5张单牌均为JQK*/
class Program
{
    static void Main(string[] args)
    {
        int[] handCardIDList = { 102, 204, 304, 401, 401 };
        Console.WriteLine(onStart(handCardIDList));
        Console.ReadKey();
    }
    static String onStart(int[] handCardIDList)
    {
        Boolean is_fiveLittleNiuNum = true;
        Boolean is_FowerNiuNum = true;
        int[] numList = new int[5];
        for (int i = 0; i < 5; i++)
        {
            int num = handCardIDList[i] % 100;
            numList[i] = num;
            if (num > 5)
            {
                is_fiveLittleNiuNum = false;
            }
            if (num < 11 && num != 0)
            {
                is_FowerNiuNum = false;
            }
        }
        if (is_fiveLittleNiuNum)
        {
            if (s1_FiveLittleNiu(numList))
            {
                return "五小牛";
            };
        }
        else if (s2_BoomNiu(numList))
        {
            return "四炸";
        }
        else if (is_FowerNiuNum)
        {
            return "五花牛";
        };
        String result = treat(numList);
        return result;
    }
    //在不是五小牛,五花牛,四炸的基础上判断牛数
    static String treat(int[] numList)
    {
        int[] treamNumList = new int[5];
        int less_than10_num = 0;
        int totalNum = 0;
        int kingNum = 0;//鬼牌数量
        String result = null;
        foreach (int i in numList)
        {
            if (i < 10)
            {
                if (i == 0)
                {
                    kingNum += 1;
                }
                else
                {
                    treamNumList[less_than10_num] = i;
                    totalNum += i;
                    less_than10_num += 1;
                }
            }
        }
        if (kingNum == 2)
        {
            return "牛牛";
        }
        switch (less_than10_num)
        {
            case 0:
                result = "牛牛";
                break;
            case 1:
                if (kingNum == 1)
                {
                    result = "牛牛";
                }
                else
                {
                    result = "牛" + treamNumList[0];
                }
                break;
            case 2:
                int case2_niu_niuNum = (treamNumList[0] + treamNumList[1]) % 10;
                if (case2_niu_niuNum == 0 || kingNum == 1)
                {
                    result = "牛牛";
                }
                else
                {
                    result = "牛" + case2_niu_niuNum;
                }
                break;
            case 3:
                int case3_niu_num = 0;
                if (kingNum == 1)
                {
                    int case3_treatnum = 0;
                    for (int i = 0; i < less_than10_num; i++)
                    {
                        case3_treatnum = treamNumList[i] % 10;
                        if (case3_treatnum > case3_niu_num)
                            case3_niu_num = case3_treatnum;
                    }
                }
                else
                {
                    for (int i = 0; i < less_than10_num - 1; i++)
                    {
                        for (int j = i + 1; j < less_than10_num; j++)
                        {
                            if ((treamNumList[i] + treamNumList[j]) % 10 == 0)
                            {
                                case3_niu_num = totalNum - treamNumList[i] - treamNumList[j];
                            }
                        }
                    }
                }
                if (case3_niu_num > 0)
                {
                    result = "牛" + case3_niu_num;
                }
                else
                {
                    result = "没牛";
                }
                break;
            case 4:
                int case4_niu_num = 0;
                if (kingNum == 1)
                {
                    int case4_treatnum = 0;
                    for (int i = 0; i < less_than10_num - 1; i++)
                    {
                        for (int j = i + 1; j < less_than10_num; j++)
                        {
                            case4_treatnum = (treamNumList[i] + treamNumList[j]) % 10;
                            if (case4_treatnum > case4_niu_num)
                            {
                                case4_niu_num = case4_treatnum;
                            }
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < less_than10_num - 1; i++)
                    {
                        for (int j = i + 1; j < less_than10_num; j++)
                        {
                            if ((treamNumList[i] + treamNumList[j]) % 10 == 0)
                            {
                                case4_niu_num = totalNum - treamNumList[i] - treamNumList[j];
                            }
                        }
                    }
                }
                if (case4_niu_num > 0)
                {
                    result = "牛" + case4_niu_num;
                }
                else
                {
                    result = "没牛";
                }
                break;
            default:
                int case5_niu_num = 0;
                for (int i = 0; i < less_than10_num - 1; i++)
                {
                    for (int j = i + 1; j < less_than10_num; j++)
                    {
                        if ((totalNum - treamNumList[i] - treamNumList[j]) % 10 == 0)
                        {
                            case5_niu_num = (treamNumList[i] + treamNumList[j]) % 10;
                        }
                    }
                }
                if (case5_niu_num > 0)
                {
                    result = "牛" + case5_niu_num;
                }
                else
                {
                    result = "没牛";
                }
                break;
        }
        return result;
    }


    //判断是否为五小牛
    static Boolean s1_FiveLittleNiu(int[] fiveLittleNiuList)
    {
        int num = 0;
        for (int i = 0; i < fiveLittleNiuList.Length; i++)
        {
            num += fiveLittleNiuList[i];
            if (num > 10)
            {
                return false;
            }
        }
        return true;
    }
    //判断是否为四炸
    static Boolean s2_BoomNiu(int[] boomNiuList)
    {
        int compareNum1 = boomNiuList[0];
        int compareNum2 = boomNiuList[1];
        if (compareNum1 == compareNum2 && compareNum1 == 0)
        {
            for (int i = 2; i < boomNiuList.Length - 1; i++)
            {
                for (int j = i + 1; j < boomNiuList.Length; j++)
                {
                    if (boomNiuList[i] == boomNiuList[j])
                    {
                        return true;
                    }
                }
            }
            if (compareNum1 == compareNum2)
            {
                int num = 0;
                for (int i = 2; i < boomNiuList.Length && num < 2; i++)
                {
                    if (compareNum1 != boomNiuList[i] && boomNiuList[i] != 0)
                    {
                        num++;
                    }
                }
                if (num <= 1)
                {
                    return true;
                }
            }
            else if (compareNum1 == boomNiuList[2])
            {
                int num = 0;
                for (int i = 3; i < boomNiuList.Length; i++)
                {
                    if (compareNum1 != boomNiuList[i] && boomNiuList[i] != 0)
                    {
                        num++;
                    }
                }
                if (boomNiuList[2] == 0)
                {
                    num -= 1;
                }
                if (num < 1)
                {
                    return true;
                }
            }
            else if (compareNum2 == boomNiuList[2])
            {
                int num = 0;
                for (int i = 3; i < boomNiuList.Length; i++)
                {
                    if (compareNum2 != boomNiuList[i] && boomNiuList[i] != 0)
                    {
                        num++;
                    }
                }
                if (boomNiuList[1] == 0)
                {
                    num -= 1;
                }
                if (num <= 1)
                {
                    return true;
                }
            }
            else if (compareNum1 == 0 || compareNum2 == 0)
            {
                if (boomNiuList[2] == boomNiuList[3] && boomNiuList[3] == boomNiuList[4])
                {
                    return true;
                }
            }
        }
        return false;
    }
}

}

你可能感兴趣的:(unity3d,C#)