admin管理员组文章数量:1122854
Lua
Lua–棋牌游戏开发(概念性设计二)
1:获取"棋牌工具.lua"文件,文件后return CardUtils
参考上一篇博文链接:Lua–棋牌游戏开发(概念性设计一)
local CardUtils = require "棋牌工具" -- 加载文件
2:牌的类型定义
local CardsType =
{-- 键值对 = 值Single = 1, -- 单张DuiZi = 2, -- 对子ShunZi = 3, -- 顺子LianDui = 4, -- 连对Three = 5, -- 三张ThreeTakeOne = 6, -- 三带一ThreeTakeTwo = 7, -- 三带二FourTakeTwo = 8, -- 四带二FeiJi = 9, -- 三张(333444)/三张带一(33344455/33344445)/三带二(3334445566)Boom = 10, -- 炸弹BoomBoom = 11, -- 王炸None = 12 -- 不符合牌型
}
3:记牌器:将选中的牌转换成(牌值对应数量的键值对)
local DDZLogic = {}function DDZLogic:toCardMap(selectedCards)local map = {}-- 键值size对应选中牌的数量map.size = #selectedCardsfor i = 1,20 domap[i] = 0endfor i = 1,#selectedCards dolocal value = CardUtils:getCardValue(selectedCards[i])map[value] = map[value] + 1endreturn map
end
4:判断是否是单张
function DDZLogic:isSingle(cards) -- cards:记牌后的结果return 1 == cards.size
end
5:判断是否是对子
function DDZLogic:isDuiZi(cards)-- 判断张数if cards.size ~= 2 thenreturn falseend-- 遍历所有的牌值对应的数量,如果不等于0,判断是否等于2for i = 1,20 dolocal cardNum = cards[i] -- 得到当前牌值对应的数量if cardNum ~= 0 thenreturn 2 == cardNumendend
end
6:判断是否是顺子
function DDZLogic:isShunZi(cards)if cards.size < 5 thenreturn falseendfor i = 1,20 dolocal cardNum = cards[i]if 1 == cardNum then-- 遍历从i开始到i+cards.size - 1对应的数量都为1for j = 1,cards.size - 1 dolocal cardNum = cards[i + j]if cardNum ~= 1 thenreturn falseendendreturn true -- 整个循环完成,返回trueendendreturn false -- 没有单张
end
7:判断是否是连对
function DDZLogic:isLianDui(cards)if cards.size%2 ~=0 or cards.size<6 thenreturn falseendfor i = 1,20 dolocal cardNum = cards[i]if 2 == cardNum then-- 遍历for j = 1,cards.size/2-1 dolocal cardNum = cards[i + j]if cardNum ~= 2 thenreturn falseendendreturn trueendendreturn false
end
8:判断是否是三张
--判断是否是三张
function DDZLogic:isThree(cards)if cards.size~=3 thenreturn falseendfor i=1,20 dolocal cardNum=cards[i]if cardNum ==3 thenreturn trueendendreturn false
end
9:判断是否是三带一
function DDZLogic:ThreeTakeOne(cards)if cards.size~=4 thenreturn falseend
local issingle=0
local isThree=falsefor i=1,20 dolocal cardNum=cards[i]if cardNum ==3 thenisThree=trueendif cardNum == 1 thenissingle=issingle+1endendif issingle==1 and isThree==true thenreturn trueelsereturn falseend
end
10:判断是否是三带二
function DDZLogic:ThreeTakeTwo(cards)if cards.size~=5 thenreturn falseend
local istwo=false
local isThree=falsefor i=1,20 dolocal cardNum=cards[i]if cardNum ==3 thenisThree=trueendif cardNum == 1 thenistwo=trueendendif istwo==true and isThree==true thenreturn trueelsereturn falseend
end
11:判断是否是四带二
function DDZLogic:FourTakeTwo(cards)if cards.size~=6 thenreturn falseend
local istwo=false
local isfour=falsefor i=1,20 dolocal cardNum=cards[i]if cardNum ==4 thenisfour=trueendif cardNum == 2 thenistwo=trueendendif istwo==true and isfour==true thenreturn trueelsereturn falseend
end
12:判断是否是飞机
function DDZLogic:FeiJi(cards)if cards.size < 6 thenreturn falseend-- 3 * n(333444) --666888if cards.size % 3 == 0 thenlocal feijiLength = cards.size / 3 -- 飞机的总长度local length = 0 -- 记录的长度for i = 1,20 dolocal cardNum = cards[i]if 3 == cardNum thenlength = length + 1 -- 飞机数量+1if feijiLength == length thenreturn trueendelselength = 0endendend-- (3 + 1)* n(33344456 / 33334445 / 33334444/555666777333)if cards.size % 4 == 0 thenlocal feijiLength = cards.size / 4 -- 飞机的总长度local length = 0 -- 记录的长度for i = 1,20 dolocal cardNum = cards[i]if cardNum >= 3 thenlength = length + 1 -- 飞机数量+1if feijiLength == length thenreturn trueendelselength = 0endendend-- (3 + 2)* n(3334445566)if cards.size % 5 == 0 thenlocal feijiLength = cards.size / 5local length = 0for i = 1,20 dolocal cardNum = cards[i]if 3 == cardNum thenlength = length + 1if feijiLength == length then-- 从i往后到20,所有的牌值对应的数量不能1和3for j = i + 1,20 dolocal cardNum = cards[j]if 1 == cardNum or 3 == cardNum thenreturn falseendendreturn true -- 后面的牌对应的数量没有出现1和3endelse -- 0 1 2 4if 1 == cardNum then -- 当出现数量为1return falseendif length > 0 then -- 之前已经出现了3张return falseendendendendreturn false
end
13:判断是否是炸弹
--判断是否是炸弹
function DDZLogic:Boom(cards)if cards.size~=4 thenreturn falseendfor i=1,20 dolocal cardNum=cards[i]if cardNum ==4 thenreturn trueend endreturn falseend
14:判断是否是王炸
--判断是否是王炸
function DDZLogic:BoomBoom(cards)if cards.size~=2 thenreturn falseendlocal cardNum1=cards[18]local cardnum2=cards[19]if cardNum1==1 and cardnum2 ==1 thenreturn trueelsereturn falseendend
15:获取牌的类型
function DDZLogic:getCardsType(selectedCards) -- 获取选中牌的类型local cards = self:toCardMap(selectedCards) -- 转为记牌器--单张if self:isSingle(cards) thenreturn CardsType.Singleend--对子if self:isDuiZi(cards) thenreturn CardsType.DuiZiend--顺子if self:isShunZi(cards) thenreturn CardsType.ShunZiend--连队if self:isLianDui(cards) thenreturn CardsType.LianDuiend--三张if self:isThree(cards) thenreturn CardsType.Threeend--三带一if self:ThreeTakeOne(cards) thenreturn CardsType.ThreeTakeOneend--三带二if self:ThreeTakeTwo(cards) thenreturn CardsType.ThreeTakeTwoend--四带二if self:FourTakeTwo(cards) thenreturn CardsType.FourTakeTwoend--飞机if self:FeiJi(cards) thenreturn CardsType.FeiJiend--炸弹if self:Boom(cards) thenreturn CardsType.Boomend--王炸if self:BoomBoom(cards) thenreturn CardsType.BoomBoomend--不符合return CardsType.None
end
测试用例
local selectedCards = {0x03,0x14,0x25,0x26,0x37}
local type = DDZLogic:getCardsType(selectedCards)
print(type)
本文标签: Lua
版权声明:本文标题:Lua 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1686532668a6814.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论