-- 检测牌是否存在 local function checkCard(eventCard, cardList, num) num = num == nil and 1 or num local result = 0 for i = 1, #cardList do if (cardList[i] == eventCard) then result = result + 1 if (result == num) then return true end end 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 zhongid = 0 local M = { pair_count = 0, cardList = nil, stack = nil, zhong_count = 0, qidui = false, hongzhong_count = 0, qidui_pari_count = 0 } function M:push(cardGroup) self.stack[#self.stack + 1] = cardGroup end function M:rollBack() local cardGroup = self.stack[#self.stack] table.remove(self.stack, #self.stack) for _, card in ipairs(cardGroup) do if (card == zhongid) then self.zhong_count = self.zhong_count + 1 else self.cardList[#self.cardList + 1] = card end end table.sort(self.cardList) end function M:tryShunzi(card) if card > 400 or card % 100 > 7 then return false end if (checkCard(card + 1, self.cardList) and checkCard(card + 2, self.cardList)) then removeCard(self.cardList, card, 1) removeCard(self.cardList, card + 1, 1) removeCard(self.cardList, card + 2, 1) local cardGroup = { card, card + 1, card + 2 } self:push(cardGroup) return true end return false end --特殊牌,风牌也就是东南西北的成顺逻辑,中发白逻辑在普通顺子里 function M:tryFengShunzi(card) --只有牌里有东南风的时候才能成顺,401东风,402南方 if self._flag_zikechengshun ~= 0 then return false end local canShunziFeng = { 401, 402 } for i = 1, #canShunziFeng do if card == canShunziFeng[i] then local tempFengList = {} for j = 1, 4 - i do if checkCard(405 - j, self.cardList) then table.insert(tempFengList, 405 - j) end end if #tempFengList >= 2 then removeCard(self.cardList, card, 1) removeCard(self.cardList, tempFengList[1], 1) removeCard(self.cardList, tempFengList[2], 1) local cardGroup = { card, tempFengList[1], tempFengList[2] } self:push(cardGroup) return true end end end if card < 500 then return false end if (checkCard(card + 1, self.cardList) and checkCard(card + 2, self.cardList)) then removeCard(self.cardList, card, 1) removeCard(self.cardList, card + 1, 1) removeCard(self.cardList, card + 2, 1) local cardGroup = { card, card + 1, card + 2 } self:push(cardGroup) return true end return false end function M:tryKezi(card) if (checkCardAndRomve(card, self.cardList, 3)) then local cardGroup = { card, card, card } self:push(cardGroup) return true end return false end function M:tryPair(card) if (self.pair_count > 0) then return false end if (checkCardAndRomve(card, self.cardList, 2)) then local cardGroup = { card, card } self:push(cardGroup) self.pair_count = 1 return true end return false end function M:tryKezi1Zhong(card) if (self.zhong_count >= 1 and checkCardAndRomve(card, self.cardList, 2)) then local cardGroup = { card, card, zhongid } self:push(cardGroup) self.zhong_count = self.zhong_count - 1 return true end return false end function M:tryKezi2Zhong(card) if (self.zhong_count >= 2 and checkCardAndRomve(card, self.cardList, 1)) then local cardGroup = { card, zhongid, zhongid } self:push(cardGroup) self.zhong_count = self.zhong_count - 2 return true end return false end function M:tryShunzi1Zhong(card) if (card % 100 > 8) then return false end if (self.zhong_count < 1) then return false end if (checkCard(card + 1, self.cardList)) then removeCard(self.cardList, card, 1) removeCard(self.cardList, card + 1, 1) self.zhong_count = self.zhong_count - 1 local cardGroup = { card, card + 1, zhongid } self:push(cardGroup) return true end if (checkCard(card + 2, self.cardList) and ((card + 1) % 100 ~= 0)) then removeCard(self.cardList, card, 1) removeCard(self.cardList, card + 2, 1) self.zhong_count = self.zhong_count - 1 local cardGroup = { card, zhongid, card + 2 } self:push(cardGroup) return true end return false end function M:tryPair1Zhong(card) if (self.pair_count > 0) then return false end if (self.zhong_count < 1) then return false end removeCard(self.cardList, card, 1) local cardGroup = { card, zhongid } self:push(cardGroup) self.zhong_count = self.zhong_count - 1 self.pair_count = 1 return true end function M:tryPair2Zhong() if (self.pair_count > 0) then return false end if (self.zhong_count < 2) then return false end local cardGroup = { zhongid, zhongid } self:push(cardGroup) self.zhong_count = self.zhong_count - 2 self.pair_count = 1 return true end function M:tryWin() if (self.zhong_count == 4 and not self.eight_laizi) or (self.zhong_count == 8 and self.eight_laizi) then return true end if (#self.cardList == 0 and self.pair_count == 1) then return true end if (#self.cardList == 0 and self.pair_count == 0) then return self:tryPair2Zhong() end if (#self.cardList == 0) then return false end local activeCard = self.cardList[1] if (self:tryPair(activeCard)) then if (self:tryWin()) then return true end self.pair_count = 0 self:rollBack() end if (self:tryKezi(activeCard)) then if (self:tryWin()) then return true end self:rollBack() end if (self:tryShunzi(activeCard)) then if (self:tryWin()) then return true end self:rollBack() end if (self:tryFengShunzi(activeCard)) then if (self:tryWin()) then return true end self:rollBack() end if (self:tryKezi1Zhong(activeCard)) then if (self:tryWin()) then return true end self:rollBack() end if (self:tryKezi2Zhong(activeCard)) then if (self:tryWin()) then return true end self:rollBack() end if (self:tryShunzi1Zhong(activeCard)) then if (self:tryWin()) then return true end self:rollBack() end if (self:tryPair1Zhong(activeCard)) then if (self:tryWin()) then return true end self.pair_count = 0 self:rollBack() end return false end function M:checkQidui() if (not self.qidui) then return false end if ((#self.cardList + self.zhong_count) ~= 14) then return false end local cardList = membe_clone(self.cardList) self.qidui_pari_count = 0 self.hongzhong_count = self.zhong_count return self:isQdPari(cardList) end function M:isQdPari(cardList) if (self.qidui_pari_count == 7) then return true end if (#cardList == 0) then return true end local card = cardList[1] if (cardNum(card, cardList) >= 2) then removeCard(cardList, card, 2) self.qidui_pari_count = self.qidui_pari_count + 1 if (self:isQdPari(cardList)) then return true end end if (self.hongzhong_count > 0) then removeCard(cardList, card, 1) self.hongzhong_count = self.hongzhong_count - 1 self.qidui_pari_count = self.qidui_pari_count + 1 if (self:isQdPari(cardList)) then return true end end return false end function M:checkshisanlan() if (not self.shisanlan) then return false end if ((#self.cardList + self.zhong_count) ~= 14) then return false end local cardList = membe_clone(self.cardList) self.hongzhong_count = self.zhong_count return self:isShiSanLan(cardList, table.remove(cardList, 1)) end function M:isShiSanLan(cardList, card) if (#cardList == 0) then return true end if card < 400 then if not (checkCard(card, cardList) or checkCard(card + 1, cardList) or checkCard(card + 2, cardList)) then return self:isShiSanLan(cardList, table.remove(cardList, 1)) end else if not checkCard(card, cardList) then return self:isShiSanLan(cardList, table.remove(cardList, 1)) end end return false end --._flag_haveLaizi, data._hu_qidui, data._data_laizi local function init(self, cardInhand, addCard, data) self.stack = {} self.pair_count = 0 self.cardList = membe_clone(cardInhand) self.qidui = data._hu_qidui self.shisanlan = data._hu_shisanlan self.eight_laizi = nil self.cardList[#self.cardList + 1] = addCard if (data._flag_haveLaizi) then self.zhong_count = cardNum(zhongid, self.cardList) removeCard(self.cardList, zhongid, self.zhong_count) end table.sort(self.cardList) --printlog("添加排序====>>>") --pt(self.cardList) return self:checkQidui() or self:checkshisanlan() or self:tryWin() end local specialCardList = { 401, 402, 403, 404, 501, 502, 503 } function M.tingPai(cardInhand, data) data = data or {} local self = setmetatable({}, { __index = M }) local tingList = {} if not cardInhand or #cardInhand == 0 then return tingList end for k = 100, 300, 100 do for i = 1, 9 do local tem = k + i local result = init(self, cardInhand, tem, data) --printlog("返回结果为===>>>",result) if (result) then tingList[#tingList + 1] = tem end end end for j = 1, #specialCardList do local tem = specialCardList[j] local result = init(self, cardInhand, tem, data) if (result) then tingList[#tingList + 1] = tem end end return tingList end function M.MuiltiplteCaculateTingPai(cardInhand, isZhong, qidui, eightLaizi) local room = DataManager.CurrenRoom if room.laiziInfo and #room.laiziInfo > 0 then zhongid = room.laiziInfo[1] local tempTingList2 = {} local tempTingList1 = M.tingPai(cardInhand, isZhong, qidui, eightLaizi) if room.laiziInfo[2] then zhongid = room.laiziInfo[2] tempTingList2 = M.tingPai(cardInhand, isZhong, qidui, eightLaizi) zhongid = room.laiziInfo[1] end local currentTingList = {} if #tempTingList1 > 0 and #tempTingList2 > 0 then currentTingList = CombineDictionaryAndRemoveSomeItem(tempTingList1, tempTingList2) elseif #tempTingList1 > 0 then currentTingList = tempTingList1 elseif #tempTingList2 > 0 then currentTingList = tempTingList2 end return currentTingList else M._flag_zikechengshun = room.room_config.config.zikechengshun or 0 zhongid = 0 return M.tingPai(cardInhand, isZhong, qidui, eightLaizi) end end return M