牛牛算法和大小比较

释放双眼,带上耳机,听听看~!

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

H5棋牌

棋牌游戏超强大的生命力的原因

2019-10-14 16:48:03

H5棋牌

湖南麻将胡牌技巧

2019-10-16 14:21:56

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索