-- 检测牌是否存在 local function checkCard(eventCard, cardList, num) if num == nil then num = 1 end local result = 0 for i = 1, #cardList do if (cardList[i] == eventCard) then result = result + 1 end end if (result == num) then return true end return false end -- 移除指定数量的牌 local function removeCard(cardList, card, count) for i = 1, count do list_remove(cardList, card) end end local function checkCardAndRomve(eventCard, cardList, num) if (checkCard(eventCard, cardList, num)) then removeCard(cardList, eventCard, num) return true end return false end -- 获取列表中牌数量 local function cardNum(eventCard, cardList) local result = 0 for i = 1, #cardList do local card = cardList[i] if (card == eventCard) then result = result + 1 end end return result end local M = { cardList = nil, stack = nil } function M:push(cardGroup) self.stack[#self.stack + 1] = cardGroup local obj1 = {} --小牌 local obj2 = {} --大牌 for i = 1, #self.stack do local card = self.stack[i][1] if card > 200 then --大牌 obj2[#obj2 + 1] = self.stack[i] else --小牌 obj1[#obj1 + 1] = self.stack[i] end end self.stack = {} if #obj1 > 0 then list_concat(self.stack, obj1) end if #obj2 > 0 then list_concat(self.stack, obj2) end end function M:rollBack() local cardGroup = self.stack[#self.stack] table.remove(self.stack, #self.stack) for _, card in ipairs(cardGroup) do self.cardList[#self.cardList + 1] = card end table.sort(self.cardList) end function M:tryShunzi1(card, type1) if card < 300 and card % 100 > 8 then return false end if (checkCard(card, self.cardList)) and (checkCard(card + 1, self.cardList) and checkCard(card + 2, self.cardList)) then if type1 ~= nil then return true end removeCard(self.cardList, card + 1, 1) removeCard(self.cardList, card + 2, 1) removeCard(self.cardList, card, 1) local cardGroup = {card, card + 1, card + 2} self:push(cardGroup) return true end return false end --Aaa aaA function M:tryShunzi2(card, type1) if card - card % 100 == 100 then if self:tryShunzi1(card + 100, 0) then return false end if checkCard(card + 100, self.cardList, 1) and checkCard(card, self.cardList, 2) then if type1 ~= nil then return true end removeCard(self.cardList, card, 2) removeCard(self.cardList, card + 100, 1) local cardGroup = {card, card, card + 100} self:push(cardGroup) return true end if (checkCard(card + 100, self.cardList, 2)) and (checkCard(card, self.cardList, 1)) then if type1 ~= nil then return true end removeCard(self.cardList, card, 1) removeCard(self.cardList, card + 100, 2) local cardGroup = {card + 100, card + 100, card} self:push(cardGroup) return true end else if self:tryShunzi1(card - 100) then return false end if (checkCard(card - 100, self.cardList, 1)) and checkCard(card, self.cardList, 2) then if type1 ~= nil then return true end removeCard(self.cardList, card, 2) removeCard(self.cardList, card - 100, 1) local cardGroup = {card, card, card - 100} self:push(cardGroup) return true end if (checkCard(card - 100, self.cardList, 2)) and checkCard(card, self.cardList, 1) then if type1 ~= nil then return true end removeCard(self.cardList, card, 1) removeCard(self.cardList, card - 100, 2) local cardGroup = {card - 100, card - 100, card} self:push(cardGroup) return true end end return false end function M:tryKezi(card) if (checkCard(card, self.cardList, 3)) then removeCard(self.cardList, card, 3) local cardGroup = {card, card, card} self:push(cardGroup) return true end return false end function M:tryTi(card) if (checkCard(card, self.cardList, 4)) then removeCard(self.cardList, card, 4) local cardGroup = {card, card, card, card} self:push(cardGroup) return true end return false end function M:tryPair(card) if (checkCard(card, self.cardList, 2)) then removeCard(self.cardList, card, 2) local cardGroup = {card, card} self:push(cardGroup) return true end return false end function M:tryShunzi4(card) if card - card % 100 == 100 then if self:tryShunzi1(card + 100, 0) then return false end if checkCard(card + 100, self.cardList, 1) and checkCard(card, self.cardList, 1) then removeCard(self.cardList, card, 1) removeCard(self.cardList, card + 100, 1) local cardGroup = {card, card + 100} self:push(cardGroup) return true end if (checkCard(card + 100, self.cardList, 1)) and (checkCard(card, self.cardList, 1)) then removeCard(self.cardList, card, 1) removeCard(self.cardList, card + 100, 1) local cardGroup = {card, card + 100} self:push(cardGroup) return true end else if self:tryShunzi1(card - 100, 0) then return false end if (checkCard(card - 100, self.cardList, 1)) and checkCard(card, self.cardList, 1) then removeCard(self.cardList, card, 1) removeCard(self.cardList, card - 100, 1) local cardGroup = {card - 100, card} self:push(cardGroup) return true end if (checkCard(card - 100, self.cardList, 1)) and checkCard(card, self.cardList, 1) then removeCard(self.cardList, card, 1) removeCard(self.cardList, card - 100, 1) local cardGroup = {card, card - 100} self:push(cardGroup) return true end end return false end function M:tryMenzi1(card) if (checkCard(card, self.cardList, 1)) and (checkCard(card + 1, self.cardList, 1)) then if self:tryShunzi1(card + 1, 0) then return false end removeCard(self.cardList, card, 1) removeCard(self.cardList, card + 1, 1) local cardGroup = {card, card + 1} self:push(cardGroup) return true end if (checkCard(card, self.cardList, 1)) and (checkCard(card + 2, self.cardList, 1)) then if self:tryShunzi1(card + 2, 0) then return false end removeCard(self.cardList, card, 1) removeCard(self.cardList, card + 2, 1) local cardGroup = {card, card + 2} self:push(cardGroup) return true end return false end function M:tryOneCard(card) if (checkCard(card, self.cardList, 1)) then removeCard(self.cardList, card, 1) local cardGroup = {card} self:push(cardGroup) return true end return false end -- 摆牌规则 先四后三 对子 顺子 单个 function M:tryPendulumRule() for i = 1, 10 do for k = 100, 200, 100 do local tem = k + i local x = true if x and self:tryTi(tem) then x = false end if x and self:tryKezi(tem) then x = false end -- if x and self:tryShunzi2(tem) then -- x = false -- end if x and self:tryPair(tem) then x = false end if x and self:tryShunzi1(tem) then x = false end -- if x and self:tryShunzi4(tem) then -- x = false -- end if x and self:tryMenzi1(tem) then x = false end if x and self:tryOneCard(tem) then x = false end end end if #self.stack > 10 then --变成10列--这里牌多了也不会报错了 local removeitem = 0 local card = 0 for i = #self.stack, 1, -1 do if #self.stack[i] == 1 and removeitem == 0 then removeitem = i card = self.stack[i][1] list_remove(self.stack[i], self.stack[i][1]) end end if card ~= 0 then list_remove(self.stack, self.stack[removeitem]) for i = #self.stack, 1, -1 do if #self.stack[i] > 0 and #self.stack[i] < 3 then self.stack[i][#self.stack[i] + 1] = card break end end end end if self.stack ~= nil then if #self.stack then -- statements end end return self.stack end --摆牌规则 优先胡息 function M:tryPendulumRule2() local card_count = #self.cardList local cards_map = {} local CountCards = {} for i = 1, #self.cardList do CountCards[self.cardList[i]] = CountCards[self.cardList[i]] == nil and 1 or CountCards[self.cardList[i]] + 1 end --find4 for k, v in pairs(CountCards) do if (v == 4) then local cs = {} cs[1] = k cs[2] = k cs[3] = k cs[4] = k cards_map[#cards_map + 1] = cs CountCards[k] = 0 card_count = card_count - 4 end end --find3 for k, v in pairs(CountCards) do if (v >= 3) then local cs = {} cs[1] = k cs[2] = k cs[3] = k cards_map[#cards_map + 1] = cs CountCards[k] = CountCards[k] - 3 card_count = card_count - 3 end end --find 大 123 local countA = CountCards[201] if countA ~= nil and countA > 0 then for i = 1, countA do if CountCards[201] ~= nil and CountCards[202] ~= nil and CountCards[203] ~= nil and CountCards[201] > 0 and CountCards[202] > 0 and CountCards[203] > 0 then local cs = {} cs[1] = 201 cs[2] = 202 cs[3] = 203 cards_map[#cards_map + 1] = cs CountCards[201] = CountCards[201] - 1 CountCards[202] = CountCards[202] - 1 CountCards[203] = CountCards[203] - 1 card_count = card_count - 3 end end end --find 小 123 local counta = CountCards[101] if counta ~= nil and counta > 0 then for i = 1, counta do if CountCards[101] ~= nil and CountCards[102] ~= nil and CountCards[103] ~= nil and CountCards[101] > 0 and CountCards[102] > 0 and CountCards[103] > 0 then local cs = {} cs[1] = 101 cs[2] = 102 cs[3] = 103 cards_map[#cards_map + 1] = cs CountCards[101] = CountCards[101] - 1 CountCards[102] = CountCards[102] - 1 CountCards[103] = CountCards[103] - 1 card_count = card_count - 3 end end end --find abc for i = 101, 110 do if (CountCards[i] ~= nil and CountCards[i + 1] ~= nil and CountCards[i + 2] ~= nil) and CountCards[i] == CountCards[i + 1] and CountCards[i] == CountCards[i + 2] then if CountCards[i] > 0 then local cs = {} cs[1] = i cs[2] = i + 1 cs[3] = i + 2 cards_map[#cards_map + 1] = cs CountCards[i] = CountCards[i] - 1 CountCards[i + 1] = CountCards[i + 1] - 1 CountCards[i + 2] = CountCards[i + 2] - 1 card_count = card_count - 3 end end end --find ABC for i = 201, 210 do if (CountCards[i] ~= nil and CountCards[i + 1] ~= nil and CountCards[i + 2] ~= nil) and CountCards[i] == CountCards[i + 1] and CountCards[i] == CountCards[i + 2] then if CountCards[i] > 0 then local cs = {} cs[1] = i cs[2] = i + 1 cs[3] = i + 2 cards_map[#cards_map + 1] = cs CountCards[i] = CountCards[i] - 1 CountCards[i + 1] = CountCards[i + 1] - 1 CountCards[i + 2] = CountCards[i + 2] - 1 card_count = card_count - 3 end end end -- --find AAa -- for i = 201, 210 do -- if CountCards[i]~=nil and CountCards[i] >= 2 then -- if CountCards[i-100]~=nil and CountCards[i-100] == 1 then -- local cs = {} -- cs[1]= i -- cs[2]= i -- cs[3]= i-100 -- cards_map[#cards_map+1]=cs -- CountCards[i] = CountCards[i]- 2; -- CountCards[i - 100] = CountCards[i - 100]-1 -- card_count = card_count-3; -- end -- end -- end -- --find aaA -- for i = 101, 110 do -- if CountCards[i]~=nil and (CountCards[i] >= 2) then -- if CountCards[i + 100]~=nil and (CountCards[i + 100] == 1) then -- local cs = {} -- cs[1]= i -- cs[2]= i -- cs[3]= i+100 -- cards_map[#cards_map+1]=cs -- CountCards[i] = CountCards[i]- 2; -- CountCards[i +100] = CountCards[i +100] - 1 -- card_count = card_count-3; -- end -- end -- end --find2 for k, v in pairs(CountCards) do if (v >= 2) then local cs = {} cs[1] = k cs[2] = k cards_map[#cards_map + 1] = cs CountCards[k] = CountCards[k] - 2 card_count = card_count - 2 end end -- --find Aa -- for i = 201, 210 do -- if CountCards[i] ~= nil and CountCards[i] == 1 then -- if CountCards[i - 100] ~= nil and CountCards[i - 100] == 1 then -- local cs = {} -- cs[1] = i -- cs[2] = i - 100 -- cards_map[#cards_map + 1] = cs -- CountCards[i] = CountCards[i] - 1 -- CountCards[i - 100] = CountCards[i - 100] - 1 -- card_count = card_count - 2 -- end -- end -- end --find ab for i = 101, 110 do if (CountCards[i] ~= nil and CountCards[i + 1] ~= nil) and (CountCards[i] > 0 and CountCards[i + 1] > 0) then local cs = {} cs[1] = i cs[2] = i + 1 cards_map[#cards_map + 1] = cs CountCards[i] = CountCards[i] - 1 CountCards[i + 1] = CountCards[i + 1] - 1 card_count = card_count - 2 end end --find AB for i = 201, 210 do if (CountCards[i] ~= nil and CountCards[i + 1] ~= nil) and (CountCards[i] > 0 and CountCards[i + 1] > 0) then local cs = {} cs[1] = i cs[2] = i + 1 cards_map[#cards_map + 1] = cs CountCards[i] = CountCards[i] - 1 CountCards[i + 1] = CountCards[i + 1] - 1 card_count = card_count - 2 end end local singleList = {} --find else for k, v in pairs(CountCards) do if (v == 1) then singleList[#singleList + 1] = k CountCards[k] = CountCards[k] - 1 end end local index = 3 for i = 1, #singleList, index do local cs = {} cs[#cs + 1] = singleList[i] if i < #singleList then cs[#cs + 1] = singleList[i + 1] end if i < #singleList - 1 then cs[#cs + 1] = singleList[i + 2] end cards_map[#cards_map + 1] = cs end --变成9列--这里牌多了也不会报错了 for i = 10, 1, -1 do for j = #cards_map, 10, -1 do if #cards_map[i] < 3 then cards_map[i][#cards_map[i] + 1] = cards_map[j][1] list_remove(cards_map[j], cards_map[j][1]) end end end return cards_map end --摆牌规则 优先顺子 -AAa- 对子 - 单牌 function M:tryPendulumRule3() local card_count = #self.cardList local cards_map = {} local CountCards = {} for i = 1, #self.cardList do CountCards[self.cardList[i]] = CountCards[self.cardList[i]] == nil and 1 or CountCards[self.cardList[i]] + 1 end --find4 for k, v in pairs(CountCards) do if (v == 4) then local cs = {} cs[1] = k cs[2] = k cs[3] = k cs[4] = k cards_map[#cards_map + 1] = cs CountCards[k] = 0 card_count = card_count - 4 end end --find3 for k, v in pairs(CountCards) do if (v >= 3) then local cs = {} cs[1] = k cs[2] = k cs[3] = k cards_map[#cards_map + 1] = cs CountCards[k] = CountCards[k] - 3 card_count = card_count - 3 end end --find abc for i = 101, 110 do if (CountCards[i] ~= nil and CountCards[i + 1] ~= nil and CountCards[i + 2] ~= nil) and CountCards[i] > 0 and CountCards[i + 1] > 0 and CountCards[i + 2] > 0 then if CountCards[i] > 0 then local cs = {} cs[1] = i cs[2] = i + 1 cs[3] = i + 2 cards_map[#cards_map + 1] = cs CountCards[i] = CountCards[i] - 1 CountCards[i + 1] = CountCards[i + 1] - 1 CountCards[i + 2] = CountCards[i + 2] - 1 card_count = card_count - 3 end end end --find ABC for i = 201, 210 do if (CountCards[i] ~= nil and CountCards[i + 1] ~= nil and CountCards[i + 2] ~= nil) and CountCards[i] > 0 and CountCards[i + 1] > 0 and CountCards[i + 2] > 0 then if CountCards[i] > 0 then local cs = {} cs[1] = i cs[2] = i + 1 cs[3] = i + 2 cards_map[#cards_map + 1] = cs CountCards[i] = CountCards[i] - 1 CountCards[i + 1] = CountCards[i + 1] - 1 CountCards[i + 2] = CountCards[i + 2] - 1 card_count = card_count - 3 end end end --find AAa for i = 201, 210 do if CountCards[i] ~= nil and CountCards[i] >= 2 then if CountCards[i - 100] ~= nil and CountCards[i - 100] == 1 then local cs = {} cs[1] = i cs[2] = i cs[3] = i - 100 cards_map[#cards_map + 1] = cs CountCards[i] = CountCards[i] - 2 CountCards[i - 100] = CountCards[i - 100] - 1 card_count = card_count - 3 end end end --find aaA for i = 101, 110 do if CountCards[i] ~= nil and (CountCards[i] >= 2) then if CountCards[i + 100] ~= nil and (CountCards[i + 100] == 1) then local cs = {} cs[1] = i cs[2] = i cs[3] = i + 100 cards_map[#cards_map + 1] = cs CountCards[i] = CountCards[i] - 2 CountCards[i + 100] = CountCards[i + 100] - 1 card_count = card_count - 3 end end end --find2 for k, v in pairs(CountCards) do if (v >= 2) then local cs = {} cs[1] = k cs[2] = k cards_map[#cards_map + 1] = cs CountCards[k] = CountCards[k] - 2 card_count = card_count - 2 end end --find Aa for i = 201, 210 do if CountCards[i] ~= nil and CountCards[i] == 1 then if CountCards[i - 100] ~= nil and CountCards[i - 100] == 1 then local cs = {} cs[1] = i cs[2] = i - 100 cards_map[#cards_map + 1] = cs CountCards[i] = CountCards[i] - 1 CountCards[i - 100] = CountCards[i - 100] - 1 card_count = card_count - 2 end end end --find ab for i = 101, 110 do if (CountCards[i] ~= nil and CountCards[i + 1] ~= nil) and (CountCards[i] > 0 and CountCards[i + 1] > 0) then local cs = {} cs[1] = i cs[2] = i + 1 cards_map[#cards_map + 1] = cs CountCards[i] = CountCards[i] - 1 CountCards[i + 1] = CountCards[i + 1] - 1 card_count = card_count - 2 end end --find AB for i = 201, 210 do if (CountCards[i] ~= nil and CountCards[i + 1] ~= nil) and (CountCards[i] > 0 and CountCards[i + 1] > 0) then local cs = {} cs[1] = i cs[2] = i + 1 cards_map[#cards_map + 1] = cs CountCards[i] = CountCards[i] - 1 CountCards[i + 1] = CountCards[i + 1] - 1 card_count = card_count - 2 end end local singleList = {} --find else for k, v in pairs(CountCards) do if (v == 1) then singleList[#singleList + 1] = k CountCards[k] = CountCards[k] - 1 end end local index = 3 -- print(vardump(singleList)) for i = 1, #singleList, index do local cs = {} cs[#cs + 1] = singleList[i] if i < #singleList then cs[#cs + 1] = singleList[i + 1] end if i < #singleList - 1 then cs[#cs + 1] = singleList[i + 2] end cards_map[#cards_map + 1] = cs end --变成9列--这里牌多了也不会报错了 for i = 10, 1, -1 do for j = #cards_map, 10, -1 do if #cards_map[i] < 3 then cards_map[i][#cards_map[i] + 1] = cards_map[j][1] list_remove(cards_map[j], cards_map[j][1]) end end end return cards_map end local function init(self, cardInhand, index) self.cardList = {} self.stack = {} self.cardList = membe_clone(cardInhand) table.sort(self.cardList) if index == nil or index == 0 then return self:tryPendulumRule() elseif index == 1 then return self:tryPendulumRule() elseif index == 2 then return self:tryPendulumRule() elseif index == 3 then return self:tryPendulumRule() end end function M.GetHandCard(cardInhand, index) local self = setmetatable({}, {__index = M}) if not cardInhand or #cardInhand == 0 then return nil end local HandCardList = init(self, cardInhand, index) return HandCardList end return M