lua牛のアルゴリズムとサイズの比較
4476 ワード
棋牌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