lua——赢三张牌型处理相关算法(中)——牌型判定


赢三张的牌型大题分为6种:单牌<对子<顺子<同花<同花顺<豹子。

CardType =
{
    UNDEFINE=0,        --单牌
    DUI_ZI  =1,          --对子
    SHUN_ZI =2,         --顺子
    TONG_HUA=3,   --同花
    TONG_HUA_SHUN = 4, --同花顺
    BAO_ZI = 5,        --豹子
}

首先为了方便我们的牌型判定逻辑,我们先将3张牌数据cards按牌值大小进行排序

function compByCardsValue(a, b)

    return a.card_value < b.card_value
   
end


function cardTool.sortByLaiziAsc(cards)
    table.sort(cards, compByCardsValue);
end


接下来我们从大到小的写出各牌型的判断逻辑

豹子:三张牌值相同的牌型,由于是有序的,故只需判断第一张与第三张牌值是否相等即可

function cardTool.isBaozi( cards)

    if cards[1].card_value==cards[3].card_value then
        return true
    else
        return false
    end

end

同花顺:即满足同花又满足顺子的牌型


function cardTool.isTongHuaShun(cards)
 
    local TagTongHua
    local TagShunZi
    TagTongHua = cardTool.isTongHua(cards)
    TagShunZi  = cardTool.isShunZi(cards)
    if TagTongHua and TagShunZi then --既满足同花也满足顺子的时候,就是同花顺,包含23A
        return true
    else
        return false
    end

end

同花:三张牌花色相同

function cardTool.isTongHua (cards)
 
    if cards[1].card_Color==cards[2].card_Color and cards[1].card_Color==cards[3].card_Color then
        return true
    else
        return false
    end

end

顺子:三张牌牌值依次递增1,同时还包括A23特殊牌型

function cardTool.isShunZi(cards)

    if isA32(cards) then
        return true
    end

    if cards[3].card_value - cards[2].card_value == 1 and cards[2].card_value - cards[1].card_value == 1 then
        return true
    else
        return false
    end
end

对子:两张牌牌值相等,但第一张与第三张不能相等,否则就是豹子了

function cardTool.isDuiZi(cards)
   
    if cards[1].card_value ~= cards[3].card_value then
        if cards[1].card_value == cards[2].card_value then
            return true
        end

        if cards[2].card_value == cards[3].card_value then
            return true
        end
        return false
    else
        return false
    end

end


若以上牌型都不满足,即为单牌牌型。


封装获取牌型函数:

function cardTool.getCardType(cards)
    cardTool.sortByCardsValue(cards)

    local card_type = CardType.UNDEFINE
    local ret

    if (cards) then
        --《豹子
        ret = cardTool.isBaozi(cards)
        if ret == true then
            card_type = CardType.BAO_ZI
            return card_type 
        end

        --《同花顺
        ret = cardTool.isTongHuaShun(cards)
        if (ret == true) then
            card_type = CardType.TONG_HUA_SHUN;
            return card_type

        end

        --《同花
        ret = cardTool.isTongHua(cards)
        if (ret == true) then
            card_type = CardType.TONG_HUA;
            return card_type
        end

        --《顺子
        ret = cardTool.isShunZi(cards)
        if (ret == true) then
            card_type = CardType.SHUN_ZI;
            return card_type
        end
        
        -- 《对子
        ret = cardTool.isDuiZi(cards)
        if (ret == true) then
            card_type = CardType.DUI_ZI;
            return card_type
        end
    end

    return card_type  --UNDEFINE=0,--初始单牌
end


另外存在特殊牌型,A23及235(A23算最小的顺子,非同花的235可以管豹子)

--》A23
function isA32(cards)
    if cards[1].card_value == 2 and cards[2].card_value == 3 and cards[3].card_value == 14 then
        return true
    else
        return false
    end
end

--》235
function is235(cards)
    if cards[1].card_value == 2 and cards[2].card_value == 3 and cards[3].card_value == 5 then
        return true
    else
        return false
    end
end

翻译函数:

function cardTool.getCardTypeNamebyType(CardType)
    if CardType==0 then
        return "单牌"
    end
    if CardType==1 then
        return "对子"
    end
    if CardType==2 then
        return "顺子"
    end
    if CardType==3 then
        return "同花"
    end
    if CardType==4 then
        return "同花顺"
    end
    if CardType==5 then
        return "豹子"
    end
    return "异常牌型"

end

按照上一章的测试函数,我们输出一下牌的类型

local cardBuffer =cardTool.RandCardList()


--cardBuffer[1]=6
--cardBuffer[2]=7+16
--cardBuffer[3]=7

--cardBuffer[4]=5+16
--cardBuffer[5]=7+32
--cardBuffer[6]=7+48


local cards1={}
local cards2={}

for i=1,6,1 do
    local cardColor = luabit.band(cardBuffer[i] , 0xF0)
    local cardValue = luabit.band(cardBuffer[i] , 0x0F)


    local cardinfo =
    {
        card_value = cardValue;
        card_Color = cardColor;
    }
    if i >3 then
        cards2[i-3] = cardinfo
    else
        cards1[i] = cardinfo
    end


end


print_t(cardTool.getCardTypeNamebyType(cardTool.getCardType(cards1)))
print_t(cardTool.getCardNamebyCards(cards1))

print_t(cardTool.getCardTypeNamebyType(cardTool.getCardType(cards2)))
print_t(cardTool.getCardNamebyCards(cards2))

PS:print_t是我自己写的实现输出table类型的输出函数

输出:

  对子

  方块9红桃K梅花K

  单牌

  方块3黑桃J红桃Q


以上就是牌型判定相关的处理函数,下一章我们实现牌型比较的相关逻辑。






你可能感兴趣的:(游戏算法,lua,nine_sun算法专栏)