721
社区成员
发帖
与我相关
我的任务
分享
handCard[4]={}
for i=1,3 do
for j=1,9 do
for h=1,card[i][j] do
local cardData ={}
cardData.color = i
cardData.point = j
card[4][#card[4]+1]= cardData
end
end
end
local cardData={}
self.twoCardAry={}
self.threeCardAry={}
self.fourCardAry={}
self.cloneCardList = {}
for i=1, 3 do
for j=1,9 do
cardData={}
if tempCardList[i][j] == 2 then
cardData.color = i
cardData.point = j
self.twoCardAry[#self.twoCardAry+1] = cardData
elseif tempCardList[i][j] == 3 then
cardData.color = i
cardData.point = j
self.threeCardAry[#self.threeCardAry+1] = cardData
elseif tempCardList[i][j] == 4 then
cardData.color = i
cardData.point = j
self.fourCardAry[#self.fourCardAry+1] = cardData
end
end
end
最后依次把每张牌能组成的牌型都遍历一次,看最后能否满足我们之前定义的胡牌公式。
---是否能胡牌 参数 1牌的列表 2 打的那张牌 3自己缺的花色
function Algorithm:PlayerIs_Hu(cardList,nSendCardValue,nQueSe)
local tempCardList = {}
tempCardList[4]={}
for i=1,3 do
tempCardList[i]={}
for j=1,9 do
tempCardList[i][j] = cardList[i][j]
end
end
tempCardList[nSendCardValue.color][nSendCardValue.point] = tempCardList[nSendCardValue.color][nSendCardValue.point] + 1
self:SortPlayerCards(tempCardList)
--判断自己缺的花色打完没有
for i=1,9 do
if tempCardList[nQueSe][i]>0 then return false end
end
local cardData={}
self.twoCardAry={}
self.threeCardAry={}
self.fourCardAry={}
self.cloneCardList = {}
for i=1, 3 do
for j=1,9 do
cardData={}
if tempCardList[i][j] == 2 then
cardData.color = i
cardData.point = j
self.twoCardAry[#self.twoCardAry+1] = cardData
elseif tempCardList[i][j] == 3 then
cardData.color = i
cardData.point = j
self.threeCardAry[#self.threeCardAry+1] = cardData
elseif tempCardList[i][j] == 4 then
cardData.color = i
cardData.point = j
self.fourCardAry[#self.fourCardAry+1] = cardData
end
end
end
if #self.twoCardAry == 7 then return true end --7对
if #self.twoCardAry == 5 and #self.fourCardAry ==1 then return true end --龙七对
local tempFour = 0
local tempThree = 0
local tempTwo = 0
if next(self.twoCardAry)~=nil then tempTwo = #self.twoCardAry end
if next(self.threeCardAry)~=nil then tempThree = #self.threeCardAry end
if next(self.fourCardAry)~=nil then tempFour = #self.fourCardAry end
--要胡牌 必须满足 AAA ABC AA 必须要有一对将牌
local isHuCard = false
for i=0,tempFour do
for j=0,tempThree do
for k=0,tempTwo do
self.cloneCardList ={}
for g=1,#tempCardList[4] do
self.cloneCardList[g] = tempCardList[4][g]
end
isHuCard = self:CalcIsHuCard(i,j,k)
if isHuCard == true then return true end
end
end
end
return false
end
function Algorithm:CalcIsHuCard(nFourIndex,nThreeIndex,nTwoIndex)
local FourCardData = nil
local ThreeCardData = nil
local TwoCardData = nil
if nFourIndex>0 then FourCardData = self.fourCardAry[nFourIndex] end
if nThreeIndex>0 then ThreeCardData = self.threeCardAry[nThreeIndex] end
if nTwoIndex>0 then TwoCardData = self.twoCardAry[nTwoIndex] end
self:DelCardOfCardList(self.cloneCardList,FourCardData,4)
self:DelCardOfCardList(self.cloneCardList,ThreeCardData,3)
self:DelCardOfCardList(self.cloneCardList,TwoCardData,2)
--剔除所有的连牌
local nCntIndex = #self.cloneCardList
local i=1
local j=1
local h=1
while i<=nCntIndex do
j=i+1
while j<=nCntIndex do
h=j+1
while h<=nCntIndex do
if self.cloneCardList[i].color == self.cloneCardList[j].color and self.cloneCardList[i].color == self.cloneCardList[h].color
and self.cloneCardList[i].point+1 == self.cloneCardList[j].point and self.cloneCardList[j].point+1 == self.cloneCardList[h].point then
local card1 = self.cloneCardList[i]
local card2 = self.cloneCardList[j]
local card3 = self.cloneCardList[h]
self:DelCardOfCardList(self.cloneCardList,card1,1)
self:DelCardOfCardList(self.cloneCardList,card2,1)
self:DelCardOfCardList(self.cloneCardList,card3,1)
nCntIndex = #self.cloneCardList
i=1 j=2 h=3
else
h=h+1
end
end
j=j+1
end
i=i+1
end
if nTwoIndex>0 then --如果有一对 剩余的牌必须为0 才能胡牌
if nCntIndex == 0 then
return true
end
else ---如果没有一对 最后必须没有单牌 和有且只有一对
local twoAry={}
local OneAry={}
local cardAry={}
for i=1,3 do
cardAry[i]={}
for j=1,9 do
cardAry[i][j]=0
end
end
for i=1,nCntIndex do
local data = self.cloneCardList[i]
cardAry[data.color][data.point] = cardAry[data.color][data.point] + 1
end
local nIndex = 1
for i=1,3 do
for j=1,9 do
if cardAry[i][j]==1 then
return false
elseif cardAry[i][j]==2 then
nIndex = nIndex +1
if nIndex == 3 then
return false
end
end
end
end
return true
end
return false
end