房卡棋牌游戲正如火如荼的遍地開花,迫使我也加入開發中的一員,最近完成了一套麻將棋牌游戲!今天給大家分享一下其中的演算法部分!更多房卡棋牌游戲技術,會陸續分享!!!轉載請說明出處!
博客鏈接地址:http://blog.csdn.net/weixin_41082095/article/details/79258241
LUA源代碼下載
一副麻將是否能胡牌就必須滿足以下公式(特殊牌型出外,7對,龍七對)
N*AAA+N*ABC+1*AA
看懂了嗎?如果一副牌由N個刻子,N個連牌,有且只有一對將牌組成!那么我們就可以認為這副牌胡了!
如果還沒有搞懂沒關系,下面我們依次講解什么意思!
AAA ABC AA
AAA:刻子 3張花色一樣,點數一樣的牌,俗稱一砍牌
ABC:連牌 3張花色一樣,點數彼此相差一點 如:123 345 678 俗稱一搭牌
AA:對子 2張花色一樣,點數一樣的牌,俗稱麻將
可以回憶一下我們平時打麻將胡牌之后牌型是否都滿足這樣的公式。
現在我們知道了胡牌的公式,那么在程式語言中我們是怎么來計算的呢????
工欲善其事,必先利其器!!!首先我們來定義一個麻將的資料結構
我們知道一副麻將里面有3種花色(萬,筒,條),9個點數(1----9),我們就可以定義一個三維陣列來存盤我們手牌
Local handCard=
{
{0,0,0,0,0,0,0,0,0}, --表示萬
{0,0,0,0,0,0,0,0,0}, --表示筒
{0,0,0,0,0,0,0,0,0} --表示條
}
如圖:

這副牌在陣列里面就可以這樣來定義
Local handCard=
{
{1,2,1,0,1,0,2,0,0}, --表示萬
{0,0,1,1,1,0,2,0,1}, --表示筒
{0,0,0,0,0,0,0,0,0} --表示條
}
看懂了嚒,每一個值代表每一張牌出現了幾次,我們還可以在handCard里面再多定義一個陣列用來表示每一張牌的屬性,方便我們后面來計算連牌
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這樣我們手牌牌如何用資料來表示就完成了,接下來我們就可以開始拆牌了。定義幾個陣列分別用來儲存 同一張牌出現了4次的牌,3次的牌,2次的牌。
local cardData=https://bbs.csdn.net/topics/{}
self.twoCardAry={}
self.threeCardAry={}
self.fourCardAry={}
self.cloneCardList = {}
for i=1, 3 do
for j=1,9 do
cardData=https://bbs.csdn.net/topics/{}
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=https://bbs.csdn.net/topics/{}
self.twoCardAry={}
self.threeCardAry={}
self.fourCardAry={}
self.cloneCardList = {}
for i=1, 3 do
for j=1,9 do
cardData=https://bbs.csdn.net/topics/{}
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
uj5u.com熱心網友回復:
不錯好帖子,值得學習下uj5u.com熱心網友回復:
perfect ,不過房卡游戲演算法一般是在服務端實作,客戶端只需要負責顯示就可以了,而且麻將的演算法如果放在客戶端,就會顯得沒有可讀性uj5u.com熱心網友回復:
你問破譯這個軟體嗎uj5u.com熱心網友回復:
一般客戶端這邊需要自己先捋一遍是否能出這些判斷再發,要不然會有點延遲轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/7882.html
標籤:Cocos2d-x
上一篇:十六進制
下一篇:請問這樣的麻將胡牌如何計算?
