加载斗地主工具类、创建斗地主类以及牌型
local CardUtils = require "qilaiUtils"
local CardsType =
{
Single = 1,
DuiZi = 2,
ShunZi = 3,
LianDui = 4,
Three = 5,
ThreeTakeOne = 6,
ThreeTakeTwo = 7,
FourTakeTwo = 8,
FeiJi = 9,
Boom = 10,
BoomBoom = 11,
None = 12
}
local DDZLogic = {}
记牌器:将选中的牌转换成(牌值对应数量的键值对)
function DDZLogic:toCardMap(selectedCards)
local map = {}
map.size = #selectedCards
for i = 1,20 do
map[i] = 0
end
for i = 1,#selectedCards do
local value = CardUtils:getCardValue(selectedCards[i])
map[value] = map[value] + 1
end
return map
end
判断是否是单张
function DDZLogic:isSingle(cards)
return cards.size == 1
end
判断是否是对子
function DDZLogic:isDuiZi(cards)
if cards.size ~= 2 then
return false
end
for i = 1, #cards do
if cards[i] ~= 0 then
return 2 == cards[i]
end
end
end
获取牌中对子的数量
function DDZLogic:getDuiZiCount(cards)
local count = 0
for i = 1,#cards do
if cards[i] == 2 then
count = count + 1
end
end
return count
end
判断是否是顺子
function DDZLogic:isShunZi(cards)
if cards.size < 5 then
return false
end
for i = 1, #cards do
if cards[i] == 1 then
for j = 1,cards.size - 1 do
local cardNum = cards[i + j]
if cardNum ~= 1 then
return false
end
end
return true
end
end
return false
end
判断是否是连对
function DDZLogic:isLianDui(cards)
if cards.size < 6 or ( cards.size >= 6 and cards.size % 2 == 1 )then
return false
end
for i = 1, #cards do
if cards[i] == 2 then
for j = 1,cards.size / 2 - 1 do
local cardNum = cards[i + j]
if cardNum ~= 2 then
return false
end
end
return true
end
end
return false
end
判断是否是三张
function DDZLogic:isThree(cards)
if cards.size ~= 3 then
return false
end
for i = 1, #cards do
if cards[i] ~= 0 then
return 3 == cards[i]
end
end
end
判断是否是三带一
function DDZLogic:isThreeTakeOne(cards)
if cards.size ~= 4 then
return false
end
for i = 1,#cards do
if cards[i] == 3 then
return true
end
end
return false
end
判断是否是三带一对
function DDZLogic:isThreeTakeTwo(cards)
if cards.size ~= 5 then
return false
end
local hasTwo = false
local hasThree = false
for i = 1,#cards do
if 2 == cards[i] then
hasTwo = true
elseif 3 == cardNum then
hasThree = true
end
end
return hasTwo and hasThree
end
判断是否是四带二
function DDZLogic:isFourTakeTwo(cards)
if cards.size ~= 6 then
return false
end
for i = 1,#cards do
if cards[i] == 4 then
return true
end
end
return false
end
判断是否是飞机
function DDZLogic:isFeiJi(cards)
if cards.size < 6 then
return false
end
if cards.size % 3 == 0 then
local feijiLength = cards.size / 3
local length = 0
for i = 1,#cards do
local cardNum = cards[i]
if cardNum == 3 then
length = length + 1
if feijiLength == length then
return true
end
else
length = 0
end
end
end
if cards.size % 4 == 0 then
local feijiLength = cards.size / 4
local length = 0
for i = 1,#cards do
local cardNum = cards[i]
if cardNum >= 3 then
length = length + 1
if feijiLength == length then
return true
end
else
length = 0
end
end
end
if cards.size % 5 == 0 then
local feijiLength = cards.size / 5
local length = 0
for i = 1,#cards do
local cardNum = cards[i]
if cardNum == 3 then
length = length + 1
if feijiLength == length then
for j = i + 1,#cards do
local cardNum = cards[j]
if 1 == cardNum or 3 == cardNum then
return false
end
end
return true
end
else
if cardNum == 1 then
return false
end
if length > 0 then
return false
end
end
end
end
return false
end
判断是否是炸弹
function DDZLogic:isBoom(cards)
if cards.size ~= 4 then
return false
end
for i = 1,#cards do
if cards[i] == 4 then
return true
end
end
return false
end
function DDZLogic:isBoomBoom(cards)
if cards.size ~= 2 then
return false
end
return cards[18] == 1 and cards[19] == 1
end
获取选中牌的类型
function DDZLogic:getCardsType(selectedCards)
local cards = self:toCardMap(selectedCards)
if self:isSingle(cards) then
return CardsType.Single
end
if self:isDuiZi(cards) then
return CardsType.DuiZi
end
if self:isShunZi(cards) then
return CardsType.ShunZi
end
if self:isLianDui(cards) then
return CardsType.LianDui
end
if self:isBoom(cards) then
return CardsType.Boom
end
if self:isThree(cards) then
return CardsType.Three
end
if self:isThreeTakeOne(cards) then
return CardsType.ThreeTakeOne
end
if self:isThreeTakeTwo(cards) then
return CardsType.ThreeTakeTwo
end
if self:isFourTakeTwo(cards) then
return CardsType.FourTakeTwo
end
if self:isBoomBoom(cards) then
return CardsType.BoomBoom
end
if self:isFeiJi(cards) then
return CardsType.FeiJi
end
return CardsType.None
end
测试代码
local selectedCards = {0x14, 0x24, 0x35, 0x15, 0x25, 0x36, 0x16, 0x26, 0x38, 0x18}
local type = DDZLogic:getCardsType(selectedCards)
print(type)