hengyang_client/lua_probject/extend_project/extend/majiang/qizhiba/CardCheck.lua

378 lines
8.1 KiB
Lua

-- 检测牌是否存在
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: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:checkQidui() or self:tryWin()
end
local specialCardList={515,616}
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,400,100 do
for i=1,13 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
--[[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