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

  • 相关阅读:
    SQL Server 阻止了对组件 'Ole Automation Procedures' 的 过程'sys.sp_OACreate' 的访问
    谷歌浏览器扩展程序manifest.json参数详解
    获取天气api
    UVA 10385 Duathlon
    UVA 10668 Expanding Rods
    UVALIVE 3891 The Teacher's Side of Math
    UVA 11149 Power of Matrix
    UVA 10655 Contemplation! Algebra
    UVA 11210 Chinese Mahjong
    UVA 11384 Help is needed for Dexter
  • 原文地址:https://www.cnblogs.com/wility/p/7763312.html
Copyright © 2011-2022 走看看