lua牛牛算法和大小比较

棋牌 lua牛牛算法和大小比较

local Logic = {}

local Define = {}
Define.CardData = {
	0x0D, 0x1D, 0x2D, 0x3D,  --K --方块,梅花,红桃,黑桃 --0x0F,--大王 --0x0E,--小王
	0x0C, 0x1C, 0x2C, 0x3C,  --Q
	0x0B, 0x1B, 0x2B, 0x3B,  --J
	0x0A, 0x1A, 0x2A, 0x3A,  --10
	0x09, 0x19, 0x29, 0x39,  --9
	0x08, 0x18, 0x28, 0x38,  --8
	0x07, 0x17, 0x27, 0x37,  --7
	0x06, 0x16, 0x26, 0x36,  --6
	0x05, 0x15, 0x25, 0x35,  --5
	0x04, 0x14, 0x24, 0x34,  --4
	0x03, 0x13, 0x23, 0x33,  --3
	0x02, 0x12, 0x22, 0x32,  --2
	0x01, 0x11, 0x21, 0x31,  --1
}

--牛牛牌型
Define.CardType = {
	NoNiu       = 0, 	-- 无牛
	NiuOne      = 1, 	-- 牛一
	NiuTwo      = 2, 	-- 牛二
	NiuThree    = 3, 	-- 牛三
	NiuFour     = 4, 	-- 牛四
	NiuFive     = 5, 	-- 牛五
	NiuSix      = 6, 	-- 牛六
	NiuSeven    = 7, 	-- 牛七
	NiuEight    = 8, 	-- 牛八
	NiuNine     = 9, 	-- 牛九
	NiuNiu      = 10, 	-- 牛牛
	Niu4Color   = 11,   -- 四花牛
	Niu5Color   = 12, 	-- 五花牛
	NiuBomb     = 13, 	-- 炸弹牛
	Niu5Little  = 14,   -- 五小牛
}

--洗牌函数
local function Shuff()
    local cards = {}
    table.move(Define.cardData, 1, #Define.cardData, #cards+1, cards)
    local len = #cards
    for i = 1, len do
        local m = math.random(1, len)
        cards[i], cards[m] = cards[m], cards[i]
    end
    return cards
end

Logic.random_cards = Shuff() --洗牌

--获取牌点数
local function GetCardNum(card)
	return card % 16
end

--获取花色
local function GetCardColor(card)
	return card / 16
end

 --对每个玩家的手牌进行排序
function Logic.SortCard(cards)
	table.sort(cards,function(first,second)
		if not first or not second then return false end
		if first % 16 > second % 16 then return true end
		if first % 16 < second % 16 then return false end
		if first / 16 > second / 16 then return true end
		if first / 16 < second / 16 then return false end
		return false
	end)
end

--五小牛
function Logic.IsFiveSmallNiu(nCards)
    if GetCardNum(nCards[1]) >= 5 then
        return 0
    end
    local addvalue = 0
    for _,card in ipairs(nCards) do
        addvalue = addvalue + GetCardNum(card)
        if addvalue > 10 then
            return false
        end
    end
    return true
end

-- 炸弹牛
function Logic.IsBombNiu(nCards)
    if GetCardNum(nCards[1]) == GetCardNum(nCards[4]) then
        return true
    elseif GetCardNum(nCards[2]) == GetCardNum(nCards[5]) then
        return true
    end
    return false
end

-- 五花牛
function Logic.IsFiveColorNiu(nCards)
    if GetCardNum(nCards[5]) <= 10 then
        return false
    end
    return true
end

-- 四花牛
function Logic.IsFourColorNiu(nCards)
    if GetCardNum(nCards[5]) < 10 or GetCardNum(nCards[4]) <= 10 then
        return false
    end
    return true
end

function Logic.GetCard(card)
    local value = GetCardNum(card)
    if value > 10 then
        return 10
    end
    return value
end

--其它牛
function Logic.IsOtherNiu(nCards)
	local ModNum = 0
    local nCardNum = #nCards
    for i = 1,nCardNum do
        ModNum = ModNum + Logic.GetCard(nCards[i])
    end
    ModNum = ModNum % 10  --5张牌的余数
    for i = 1,nCardNum - 1 do
        for j = i + 1,nCardNum do
            --2张牌的余数和5张牌的余数比较,如果相等,则剩下的3张余数必然是0,这样剩下的3张牌就能组成牛
            if(Logic.GetCard(nCards[i]) + Logic.GetCard(nCards[j]))%10 == ModNum then
                if ModNum == 0 then
                    return 10
                else
                    return ModNum
                end
            end
        end
    end
    return 0
end

-- 获取牌类型
function Logic.GetCardType(nCards)
    assert(#nCards == 5)
    Logic.SortCard(nCards) --对每个玩家的手牌进行排序
	if Logic.IsFiveSmallNiu(nCards) then --五小牛判断
		return Define.CardType.Niu5Little
	end
    if Logic.IsBombNiu(nCards) then --炸弹牛判断
        return Define.CardType.NiuBomb
	end
	if Logic.IsFiveColorNiu(nCards) then --五花牛判断
        return Define.CardType.Niu5Color
	end
	if Logic.IsFourColorNiu(nCards) then --四花牛判断
        return Define.CardType.Niu5Color
    end
    return Logic.IsOtherNiu(nCards) --其他牛判断
end

--计算牌值,用于比较牌大小
function Logic.GetCmpValue(cards, cardType)
    Logic.SortCard(cards) --对每个玩家的手牌进行排序
	local maxCard = GetCardNum(cards[1])*100 + GetCardColor(cards[1]) * 10
	return cardType * 10000 + maxCard
end


--[[
    tValue 的结构
    tValue = {
        nPlayId = 玩家id,
        CmpValue = 玩家的牌值,   --该值通过  Logic.GetCmpValue  获取
    }
]]
--通过该接口,可以把所有玩家的牌进行从大到小排序
function Logic.SortAllUsers(tValue)
    table.sort(tValue,function(first,second)
        if not first or not second then return false end
        return (first.CmpValue > second.CmpValue)
    end)
end

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值