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函数得到的数据就是我所有的可能性,当然对多个癞子要对不同的癞子数来得到所有的集合。对于多种癞子就需要重新对癞子的个数来出。这个在递归的外层进行循环就可以得到所有。

 

转载于:https://www.cnblogs.com/wility/p/7763312.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值