dezhou_client/lua_probject/extend_project/extend/majiang/changsha/CardCheck.lua

295 lines
5.8 KiB
Lua
Raw Permalink Normal View History

2025-12-17 21:08:27 +08:00
-- 检测牌是否存在
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 = 412
local M = {
pair_count = 0,
cardList = nil,
stack = nil,
jiang = true
}
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
self.cardList[#self.cardList + 1] = card
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 (self.jiang) then
if (not (card % 100 == 2 or card % 100 == 5 or card % 100 == 8)) then
return false
end
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:tryWin()
if (#self.cardList == 0 and self.pair_count == 1) then
return true
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
return false
end
local function init(self,cardInhand,addCard,jiang)
self.stack = {}
self.pair_count = 0
self.jiang = jiang
self.cardList = membe_clone(cardInhand)
self.cardList[#self.cardList+1] = addCard
table.sort(self.cardList)
return self:tryWin()
end
local function quanqiuren(cardInhand,drawCard)
if (#cardInhand > 1) then
return false
end
if (cardInhand[1] ~= drawCard) then
return false
end
return true
end
local function qingyise(fzList, cardInhand, drawCard)
local se = math.floor(cardInhand[1] / 100)
if (math.floor(drawCard / 100) ~= se) then
return false
end
for i=1,#fzList do
local fz = fzList[i]
if (se ~= math.floor(fz.card / 100)) then
return false
end
end
for i=1,#cardInhand do
if (se ~= math.floor(cardInhand[i] / 100)) then
return false
end
end
return true
end
local function jiangjiang(fzList, cardInhand,drawCard)
if (drawCard % 100 ~= 2 and drawCard % 100 ~= 5 and drawCard % 100 ~= 8) then
return false
end
for i=1,#fzList do
local fz = fzList[i]
if (fz.type == FZType.Chi) then
return false
end
if (fz.card % 100 ~= 2 and fz.card % 100 ~= 5 and fz.card % 100 ~= 8) then
return false
end
end
for i=1,#cardInhand do
local card = cardInhand[i]
if (card % 100 ~= 2 and card % 100 ~= 5 and card % 100 ~= 8) then
return false
end
end
return true
end
local function checkQidui(cardList)
if (#cardList== 0) then
return true
end
local card = cardList[1]
if (cardNum(card, cardList) >= 2) then
removeCard(cardList, card, 2)
if checkQidui(cardList) then
return true
end
end
end
local function qixiaodui(cardInhand, drawCard)
if (#cardInhand ~= 13) then
return false
end
local cardlist = membe_clone(cardInhand)
cardlist[#cardlist+1] = drawCard
return checkQidui(cardlist)
end
local function CheckPengPeng(cardList, jiang)
if (#cardList== 0) then
return true
end
local card = cardList[1]
if (cardNum(card, cardList) >= 3) then
removeCard(cardList, card, 3)
return CheckPengPeng(cardList, jiang)
elseif (cardNum(card, cardList) == 2) and not jiang then
removeCard(cardList, card, 3)
jiang = true
return CheckPengPeng(cardList, jiang)
end
end
local function pengpenghu(cardInhand, fzList, drawCard)
for i = 1, #fzList do
local tem = fzList[i]
if tem.type == FZType.Chi then
return false
end
end
local cardlist = membe_clone(cardInhand)
cardlist[#cardlist+1] = drawCard
return CheckPengPeng(cardlist, false)
end
function M.tingPai(cardInhand,fzList)
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 = false
result = qixiaodui(cardInhand,tem)
if not result then
result = quanqiuren(cardInhand,tem)
end
if not result then
result = jiangjiang(fzList,cardInhand,tem)
end
if not result then
if qingyise(fzList,cardInhand,tem) then
result = init(self,cardInhand,tem,false)
else
result = init(self,cardInhand,tem,true)
end
end
if not result then
result = pengpenghu(cardInhand, fzList, tem)
end
if(result) then
tingList[#tingList + 1] = tem
end
end
end
return tingList
end
return M