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函數得到的數據就是我所有的可能性,當然對多個癩子要對不同的癩子數來得到所有的集合。對於多種癩子就需要重新對癩子的個數來出。這個在遞歸的外層進行循環就可以得到所有。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM