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

375 lines
8.3 KiB
Lua
Raw Normal View History

2025-04-01 10:48:36 +08:00
-- 检测牌是否存在
2025-04-10 12:25:45 +08:00
local function checkCard(eventCard, cardList, num)
2025-04-01 10:48:36 +08:00
num = num == nil and 1 or num
local result = 0
2025-04-10 12:25:45 +08:00
for i = 1, #cardList do
2025-04-01 10:48:36 +08:00
if (cardList[i] == eventCard) then
result = result + 1
2025-04-10 12:25:45 +08:00
if (result == num) then
2025-04-01 10:48:36 +08:00
return true
end
end
end
return false
end
-- 移除指定数量的牌
2025-04-10 12:25:45 +08:00
local function removeCard(cardList, card, count)
for i = 1, count do
list_remove(cardList, card)
2025-04-01 10:48:36 +08:00
end
end
2025-04-10 12:25:45 +08:00
local function checkCardAndRomve(eventCard, cardList, num)
if (checkCard(eventCard, cardList, num)) then
removeCard(cardList, eventCard, num)
2025-04-01 10:48:36 +08:00
return true
end
return false
end
-- 获取列表中牌数量
2025-04-10 12:25:45 +08:00
local function cardNum(eventCard, cardList)
2025-04-01 10:48:36 +08:00
local result = 0
2025-04-10 12:25:45 +08:00
for i = 1, #cardList do
2025-04-01 10:48:36 +08:00
local card = cardList[i]
if (card == eventCard) then
result = result + 1
end
end
return result
end
2025-04-10 12:25:45 +08:00
local zhongid = 0
2025-04-01 10:48:36 +08:00
local M = {
pair_count = 0,
cardList = nil,
stack = nil,
zhong_count = 0,
qidui = false,
hongzhong_count = 0,
qidui_pari_count = 0
}
2025-04-10 12:25:45 +08:00
function M:push(cardGroup)
self.stack[#self.stack + 1] = cardGroup
2025-04-01 10:48:36 +08:00
end
function M:rollBack()
local cardGroup = self.stack[#self.stack]
2025-04-10 12:25:45 +08:00
table.remove(self.stack, #self.stack)
for _, card in ipairs(cardGroup) do
2025-04-01 10:48:36 +08:00
if (card == zhongid) then
2025-04-10 12:25:45 +08:00
self.zhong_count = self.zhong_count + 1
2025-04-01 10:48:36 +08:00
else
self.cardList[#self.cardList + 1] = card
end
end
table.sort(self.cardList)
end
2025-04-10 12:25:45 +08:00
function M:tryShunzi(card)
if (card < 400 and card % 100 > 7) then
2025-04-01 10:48:36 +08:00
return false
end
2025-04-10 12:25:45 +08:00
if (checkCard(card + 1, self.cardList) and checkCard(card + 2, self.cardList)) then
2025-04-01 10:48:36 +08:00
removeCard(self.cardList, card, 1)
removeCard(self.cardList, card + 1, 1)
removeCard(self.cardList, card + 2, 1)
2025-04-10 12:25:45 +08:00
local cardGroup = { card, card + 1, card + 2 }
2025-04-01 10:48:36 +08:00
self:push(cardGroup)
return true
end
return false
end
2025-04-10 12:25:45 +08:00
function M:tryKezi(card)
2025-04-01 10:48:36 +08:00
if (checkCardAndRomve(card, self.cardList, 3)) then
2025-04-10 12:25:45 +08:00
local cardGroup = { card, card, card }
2025-04-01 10:48:36 +08:00
self:push(cardGroup)
return true
end
return false
end
2025-04-10 12:25:45 +08:00
function M:tryPair(card)
2025-04-01 10:48:36 +08:00
if (self.pair_count > 0) then
return false
end
if (checkCardAndRomve(card, self.cardList, 2)) then
2025-04-10 12:25:45 +08:00
local cardGroup = { card, card }
2025-04-01 10:48:36 +08:00
self:push(cardGroup)
self.pair_count = 1
return true
end
return false
end
2025-04-10 12:25:45 +08:00
function M:tryKezi1Zhong(card)
if (self.zhong_count >= 1 and checkCardAndRomve(card, self.cardList, 2)) then
local cardGroup = { card, card, zhongid }
2025-04-01 10:48:36 +08:00
self:push(cardGroup)
2025-04-10 12:25:45 +08:00
self.zhong_count = self.zhong_count - 1
2025-04-01 10:48:36 +08:00
return true
end
return false
2025-04-10 12:25:45 +08:00
end
2025-04-01 10:48:36 +08:00
function M:tryKezi2Zhong(card)
2025-04-10 12:25:45 +08:00
if (self.zhong_count >= 2 and checkCardAndRomve(card, self.cardList, 1)) then
local cardGroup = { card, zhongid, zhongid }
2025-04-01 10:48:36 +08:00
self:push(cardGroup)
2025-04-10 12:25:45 +08:00
self.zhong_count = self.zhong_count - 2
2025-04-01 10:48:36 +08:00
return true
end
return false
end
2025-04-10 12:25:45 +08:00
function M:tryShunzi1Zhong(card)
2025-04-01 10:48:36 +08:00
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)
2025-04-10 12:25:45 +08:00
self.zhong_count = self.zhong_count - 1
local cardGroup = { card, card + 1, zhongid }
2025-04-01 10:48:36 +08:00
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)
2025-04-10 12:25:45 +08:00
self.zhong_count = self.zhong_count - 1
local cardGroup = { card, zhongid, card + 2 }
2025-04-01 10:48:36 +08:00
self:push(cardGroup)
return true
end
return false
end
2025-04-10 12:25:45 +08:00
function M:tryPair1Zhong(card)
2025-04-01 10:48:36 +08:00
if (self.pair_count > 0) then
return false
end
if (self.zhong_count < 1) then
return false
end
removeCard(self.cardList, card, 1)
2025-04-10 12:25:45 +08:00
local cardGroup = { card, zhongid }
2025-04-01 10:48:36 +08:00
self:push(cardGroup)
2025-04-10 12:25:45 +08:00
self.zhong_count = self.zhong_count - 1
2025-04-01 10:48:36 +08:00
self.pair_count = 1
return true
end
2025-04-10 12:25:45 +08:00
function M:tryPair2Zhong()
2025-04-01 10:48:36 +08:00
if (self.pair_count > 0) then
return false
end
if (self.zhong_count < 2) then
return false
end
2025-04-10 12:25:45 +08:00
local cardGroup = { zhongid, zhongid }
2025-04-01 10:48:36 +08:00
self:push(cardGroup)
2025-04-10 12:25:45 +08:00
self.zhong_count = self.zhong_count - 2
2025-04-01 10:48:36 +08:00
self.pair_count = 1
return true
end
2025-04-10 12:25:45 +08:00
function M:tryWin()
2025-04-01 10:48:36 +08:00
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
2025-04-10 13:36:22 +08:00
if (self:tryShunzi(activeCard)) then
if (self:tryWin()) then
return true
end
self:rollBack()
end
2025-04-01 10:48:36 +08:00
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
2025-04-10 12:25:45 +08:00
function M:checkQidui()
2025-04-01 10:48:36 +08:00
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
2025-04-10 12:25:45 +08:00
function M:isQdPari(cardList)
if (self.qidui_pari_count == 7) then
2025-04-01 10:48:36 +08:00
return true
end
2025-04-10 12:25:45 +08:00
if (#cardList == 0) then
2025-04-01 10:48:36 +08:00
return true
end
local card = cardList[1]
if (cardNum(card, cardList) >= 2) then
removeCard(cardList, card, 2)
2025-04-10 12:25:45 +08:00
self.qidui_pari_count = self.qidui_pari_count + 1
2025-04-01 10:48:36 +08:00
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
2025-04-10 12:25:45 +08:00
self.qidui_pari_count = self.qidui_pari_count + 1
2025-04-01 10:48:36 +08:00
if (self:isQdPari(cardList)) then
return true
end
end
return false
end
2025-04-10 12:25:45 +08:00
local function init(self, cardInhand, addCard, isZhong, qidui, eightLaizi)
2025-04-01 10:48:36 +08:00
self.stack = {}
self.pair_count = 0
self.cardList = membe_clone(cardInhand)
self.qidui = qidui
self.eight_laizi = eightLaizi
2025-04-10 12:25:45 +08:00
self.cardList[#self.cardList + 1] = addCard
2025-04-01 10:48:36 +08:00
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
2025-04-10 12:25:45 +08:00
local specialCardList = { 401, 402, 403, 404, 405, 406, 407 }
function M.tingPai(cardInhand, isZhong, qidui, eightLaizi)
printlog("isZhong", isZhong)
printlog("qidui", qidui)
printlog("eightLaizi", eightLaizi)
pt(cardInhand)
local self = setmetatable({}, { __index = M })
2025-04-01 10:48:36 +08:00
local tingList = {}
if not cardInhand or #cardInhand == 0 then
return tingList
end
2025-04-10 12:25:45 +08:00
for k = 100, 300, 100 do
for i = 1, 9 do
2025-04-01 10:48:36 +08:00
local tem = k + i
2025-04-10 12:25:45 +08:00
local result = init(self, cardInhand, tem, isZhong, qidui, eightLaizi)
2025-04-01 10:48:36 +08:00
--printlog("返回结果为===>>>",result)
2025-04-10 12:25:45 +08:00
if (result) then
tingList[#tingList + 1] = tem
2025-04-01 10:48:36 +08:00
end
end
end
2025-04-10 12:25:45 +08:00
for j = 1, #specialCardList do
2025-04-01 10:48:36 +08:00
local tem = specialCardList[j]
2025-04-10 12:25:45 +08:00
local result = init(self, cardInhand, tem, isZhong, qidui, eightLaizi)
if (result) then
tingList[#tingList + 1] = tem
2025-04-01 10:48:36 +08:00
end
end
2025-04-10 12:25:45 +08:00
2025-04-01 10:48:36 +08:00
return tingList
end
2025-04-10 12:25:45 +08:00
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)
2025-04-01 10:48:36 +08:00
if DataManager.CurrenRoom.laiziInfo[2] then
2025-04-10 12:25:45 +08:00
zhongid = DataManager.CurrenRoom.laiziInfo[2]
tempTingList2 = M.tingPai(cardInhand, isZhong, qidui, eightLaizi)
zhongid = DataManager.CurrenRoom.laiziInfo[1]
2025-04-01 10:48:36 +08:00
end
2025-04-10 12:25:45 +08:00
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
2025-04-01 10:48:36 +08:00
end
2025-04-10 12:25:45 +08:00
2025-04-01 10:48:36 +08:00
return currentTingList
else
2025-04-10 12:25:45 +08:00
zhongid = 0
return M.tingPai(cardInhand, isZhong, qidui, eightLaizi)
2025-04-01 10:48:36 +08:00
end
end
2025-04-10 12:25:45 +08:00
return M