zoukankan      html  css  js  c++  java
  • lua 斗地主癞子牌型检测中使用递归

    斗地主的检测 根据张数先拆分成多个小检测函数,然后开始对可能的类型进行检测。单张对子这些基础的检测就不必说了,

    现在写下对三带N这种应用递归来获取所有的出牌牌型和值的方式

    function CardsGroupCheck_DouDiZhu.SanDai(args,laiziNum,need3Num,need2Num,need1Num,start) --需要控制,癞子不能带王
        local SaveSanDaiCards = {};
        if need3Num == 0 and need2Num == 0 and need1Num == 0 then
            table.insert(SaveSanDaiCards,args)
        else
            local targetGroup = {};
            for i= start,3,-1 do 
    --            if args[i] == 4 and need3Num > 0 then --4个的不能拆成3个的
    --                local temp = copyTab(args);
    --                temp[i] = 1;
    --                table.insert(temp.Group,i);
    --                table.insert(temp.Group,i);
    --                table.insert(temp.Group,i);
    --                temp.Value = i;
    --                CardsGroupCheck_DouDiZhu.AddAll(SaveSanDaiCards,CardsGroupCheck_DouDiZhu.SanDai(temp,laiziNum,need3Num-1,need2Num,need1Num,i));
    --            else
                if args[i] == 3 and need3Num > 0 then
                    local temp = copyTab(args);
                    temp[i] = 0;
                    table.insert(temp.Group,i);
                    table.insert(temp.Group,i);
                    table.insert(temp.Group,i);
                     temp.Value = i;
                    CardsGroupCheck_DouDiZhu.AddAll(SaveSanDaiCards,CardsGroupCheck_DouDiZhu.SanDai(temp,laiziNum,need3Num-1,need2Num,need1Num,i-1));
                elseif args[i] == 2 then
                    if need3Num > 0 then
                        if laiziNum > 0 then
                            local temp = copyTab(args);
                            temp[i] = 0;
                            table.insert(temp.Group,i);
                            table.insert(temp.Group,i);
                            table.insert(temp.Group,i);
                            temp.Value = i;
                            CardsGroupCheck_DouDiZhu.AddAll(SaveSanDaiCards,CardsGroupCheck_DouDiZhu.SanDai(temp,laiziNum-1,need3Num-1,need2Num,need1Num,i-1));
                        end
                    end
                    if need2Num > 0 then
                        local temp = copyTab(args);
                        temp[i] = 0;
                        table.insert(temp.Group,i);
                        table.insert(temp.Group,i);
                        CardsGroupCheck_DouDiZhu.AddAll(SaveSanDaiCards,CardsGroupCheck_DouDiZhu.SanDai(temp,laiziNum,need3Num,need2Num-1,need1Num,i-1));
                    end
    
                    if need1Num > 0 then
                        local temp = copyTab(args);
                        temp[i] = 1;
                        table.insert(temp.Group,i);
                        CardsGroupCheck_DouDiZhu.AddAll(SaveSanDaiCards,CardsGroupCheck_DouDiZhu.SanDai(temp,laiziNum,need3Num,need2Num,need1Num-1,i));
                    end
                elseif args[i] == 1 then
                    if need3Num > 0 then
                        if laiziNum >= 2 and i <= 15 then
                            local temp = copyTab(args);
                            temp[i] = 0;
                            table.insert(temp.Group,i);
                            table.insert(temp.Group,i);
                            table.insert(temp.Group,i);
                            temp.Value = i;
                            CardsGroupCheck_DouDiZhu.AddAll(SaveSanDaiCards,CardsGroupCheck_DouDiZhu.SanDai(temp,laiziNum-2,need3Num-1,need2Num,need1Num,i-1));
                        end
                    end
                    if need2Num > 0 then
                        if laiziNum >= 1 and i <= 15 then --王不能因为一张然后补成对子
                            local temp = copyTab(args);
                            temp[i] = 0;
                            table.insert(temp.Group,i);
                            table.insert(temp.Group,i);
                            CardsGroupCheck_DouDiZhu.AddAll(SaveSanDaiCards,CardsGroupCheck_DouDiZhu.SanDai(temp,laiziNum-1,need3Num,need2Num-1,need1Num,i-1));
                        end
                    end
                    if need1Num > 0 then
                        local temp = copyTab(args);
                        temp[i] = 0;
                        table.insert(temp.Group,i);
                        CardsGroupCheck_DouDiZhu.AddAll(SaveSanDaiCards,CardsGroupCheck_DouDiZhu.SanDai(temp,laiziNum,need3Num,need2Num,need1Num-1,i-1));
                    end  
                end
            end
        end
      return SaveSanDaiCards;
    end
    
    function CardsGroupCheck_DouDiZhu.AddAll(target,st)
        for k, v in pairs(st or {}) do
            table.insert(target,v);
        end
    end

    这样我最后通过sandai函数得到的数据就是我所有的可能性,当然对多个癞子要对不同的癞子数来得到所有的集合。对于多种癞子就需要重新对癞子的个数来出。这个在递归的外层进行循环就可以得到所有。

  • 相关阅读:
    set.end()和lower_bound使用记录
    自学图论的码队弟弟(dfs)
    打字训练
    (回归2.0)A
    斐波那契串 新疆省赛
    异或的路径 新疆省赛 (按位亦或)
    E. Product Oriented Recurrence (矩阵快速幂新模板)
    C. Beautiful Lyrics (模拟)构造
    O(n!)新疆省赛 d (贪心)
    Babelfish 分类: 哈希 2015-08-04 09:25 2人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/wility/p/7763312.html
Copyright © 2011-2022 走看看