-- 检测牌是否存在 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 and 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: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: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:checkLanPai() local cardList = membe_clone(self.cardList) local narr = self:removeRepeat(cardList) if #narr > #cardList or #narr < #cardList then return false end local zf = 0 local wz = 0 --万字 local wzcard = 0 local bz = 0 --筒子 local bzcard = 0 local tz = 0 --条 local tzcard = 0 for k, v in pairs(cardList) do if v>=400 then zf = zf+1 end if v > 100 and v<200 then wz = wz+1 if wzcard==0 then wzcard = v else if v ~= wzcard+3 and v~= wzcard+6 then return false end end end if v > 200 and v<300 then bz = bz+1 if bzcard==0 then bzcard = v else if v ~= bzcard+3 and v~= bzcard+6 then return false end end end if v > 300 and v<400 then tz = tz+1 if tzcard==0 then tzcard = v else if v ~= tzcard+3 and v ~= tzcard+6 then return false end end end end if zf>=5 and wz<4 and bz<4 and tz<4 then return true end return false end function M:removeRepeat(a) local b = {} pt(a) for k,v in ipairs(a) do if(#b == 0) then b[1]=v; else local index = 0 for i=1,#b do if(v == b[i]) then break end index = index + 1 end if(index == #b) then b[#b + 1] = v; end end end return b 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 local function init(self,cardInhand,addCard,isZhong,qidui,eightLaizi) self.stack = {} self.pair_count = 0 self.cardList = membe_clone(cardInhand) self.qidui = qidui self.eight_laizi = eightLaizi self.cardList[#self.cardList+1] = addCard --if (isZhong) 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:checkLanPai() or self:checkQidui() or self:tryWin() end local specialCardList={400,403,406,409,412,415,418} function M.tingPai(cardInhand,isZhong,qidui,eightLaizi) printlog("isZhong",isZhong) printlog("qidui",qidui) printlog("eightLaizi",eightLaizi) pt(cardInhand) 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,isZhong,qidui,eightLaizi) --printlog("返回结果为===>>>",result) if(result) then tingList[#tingList + 1] = tem end end end printlog("ddddtt") pt(tingList) for j=1,#specialCardList do local tem = specialCardList[j] local result = init(self,cardInhand,tem,isZhong,qidui,eightLaizi) if(result) then tingList[#tingList + 1] = tem end end return tingList end function M.MuiltiplteCaculateTingPai(cardInhand,isZhong,qidui,eightLaizi) if DataManager.CurrenRoom.laiziInfo and #DataManager.CurrenRoom.laiziInfo>0 then zhongid=DataManager.CurrenRoom.laiziInfo[1] local tempTingList2={} local tempTingList1=M.tingPai(cardInhand,isZhong,qidui,eightLaizi) if DataManager.CurrenRoom.laiziInfo[2] then zhongid=DataManager.CurrenRoom.laiziInfo[2] tempTingList2=M.tingPai(cardInhand,isZhong,qidui,eightLaizi) zhongid=DataManager.CurrenRoom.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 zhongid=0 return M.tingPai(cardInhand,isZhong,qidui,eightLaizi) end end return M