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

 

posted on 2017-10-31 19:44  wility  阅读(1556)  评论(0编辑  收藏  举报

导航