十三水中各种牌型判断LUA版

近期回归程序行业,由于业务需求需要做十三水游戏,什么是十三水就不在多讲,下面是判断十三水牌型的方法(带大小王)

GetSSSPaiType = {};
local this = GetSSSPaiType;
local huaseTable = {};
local numTable = {};
function GetSSSPaiType.Gtetype(cardList)
	--首先清空数组
	huaseTable = {};
	numTable = {};
	--格式化牌
	for i,card in ipairs(cardList) do
		local num,huase = GetPokerHasJoker(card)
		table.insert(huaseTable,huase);
		table.insert(numTable,num);
	end
	--获取是第几墩牌需要判断
	if(#cardList==3)then --是头道 只判断是三条顺子或者散牌 头道只需要判断是三条还是对子
		return SSSPaiData[this.GtetSanZhangype(numTable)].imageName;
	elseif (#cardList==5)then
		return SSSPaiData[this.GtetWuZhangType(huaseTable,numTable)].imageName;
	else
		return SSSPaiData[SSSPaiType.Error].imageName; --数组越界 return
	end
end
function GetSSSPaiType.GtetSanZhangype(cards) --判断头道是否三条
	if cards[1] == cards[2] and cards[1]==cards[3]then --是三条
		return SSSPaiType.SanTiao;
	else
		if cards[1] == cards[2] or cards[1] == cards[3] or cards[3] == cards[2] then --是对子
			return SSSPaiType.DuiZi;
		else 			--是散牌
			return SSSPaiType.SanPai;
		end
	end
end
--牌大小优先级 五同>同花顺>炸弹>葫芦>同花>顺子>三条>两对>对子>散牌
function GetSSSPaiType.GtetWuZhangType(huaNums,dianNums) --五张牌需要两个参数 花色和点数
	if(table.ContainsValue(dianNums,0))then --带王 特殊判断
		table.RemoveValue(dianNums, 0) --首先吧王移除 
		table.RemoveValue(huaNums, 5) --花色也需要移除
		if (table.ContainsValue(dianNums,0))then --再次判断是否有王 防止有两个王 --有两个王
			table.RemoveValue(dianNums, 0) 	--有两个王再次移除掉王
			table.RemoveValue(huaNums, 5) --花色也需要移除
			if(this.isWuTongHasTwoJoker(dianNums))then --两个王 是五同
				return SSSPaiType.WuTong;
			elseif(this.isTongHuaHasTwoJoker(huaNums) and this.isShunZi(dianNums) and table.getrepeat(dianNums)==3)then --同花顺
				return SSSPaiType.TongHuaShun;
			elseif(this.isTeShuShunZiHasTwoJoker(dianNums) and this.isTongHuaHasTwoJoker(dianNums)) then --特殊同花顺
				return SSSPaiType.ShunZi;
			elseif(this.isZhaDanHasTwoJoker(dianNums))then --是炸弹
				return SSSPaiType.ZhaDan;
			elseif(this.isTongHuaHasTwoJoker(dianNums))then --同花
				return SSSPaiType.TongHua;
			elseif(this.isShunZi(dianNums))then --顺子
				return SSSPaiType.ShunZi;
			elseif(this.isTeShuShunZiHasTwoJoker(dianNums))then --特殊顺子
				return SSSPaiType.ShunZi;
			else
				return SSSPaiType.SanTiao;  --有两个王 最次也是个三条 不能是葫芦
			end
		else 	--只有一个王
			if(this.isWuTongHasOneJoker(dianNums))then --一个王 是五同
				return SSSPaiType.WuTong;
			elseif (this.isTongHuaHasOneJoker(huaNums) and this.isShunZi(dianNums) and table.getrepeat(dianNums)==4)then --是同花顺
				return SSSPaiType.TongHuaShun;
			elseif(this.isTeShuShunZiHasOneJoker(dianNums)and this.isTongHuaHasOneJoker(huaNums))then --特殊同花顺
				return SSSPaiType.TongHuaShun;
			elseif(this.isZhaDanHasOneJoker(dianNums))then --是炸弹
				return SSSPaiType.ZhaDan; 
			elseif(this.isHuLuHasOneJoker(dianNums))then --是葫芦
				return SSSPaiType.HuLu; 
			elseif(this.isTongHuaHasOneJoker(huaNums) and not this.isShunZi(dianNums))then --是同花 但不是顺子
				return SSSPaiType.TongHua;
			elseif(this.isShunZi(dianNums) and not this.isTongHuaHasOneJoker(huaNums) and table.getrepeat(dianNums)==4)then --是顺子 但不同花
				return SSSPaiType.ShunZi;
			elseif(this.isTeShuShunZiHasOneJoker(dianNums)and not this.isTongHuaHasOneJoker(huaNums))then --特殊顺子 但不是同花
				return SSSPaiType.ShunZi;
			elseif(this.isSnaTiaoHasOneJoker(dianNums))then --是三条
				return SSSPaiType.SanTiao; 
			elseif(this.isLiangDuiHasOneJoker(dianNums))then --是两对
				return SSSPaiType.LiangDui; 
			else
				return SSSPaiType.DuiZi; --有一个王 最低也是个对子
			end
		end
	else 									--不带王 直接判断
		if(this.isWuTong(dianNums))then 		--是五同
			return SSSPaiType.WuTong;
		elseif(this.isShunZi(dianNums) and this.isTongHua(huaNums))then --是同花顺
			return SSSPaiType.TongHuaShun;
		elseif(this.isTeShuShunZi(dianNums) and this.isTongHua(huaNums))then --特殊同花顺
			return SSSPaiType.ShunZi;
		elseif(this.isZhaDan(dianNums))then --炸弹
			return SSSPaiType.ZhaDan;
		elseif(this.isHuLu(dianNums)) then --是葫芦
			return SSSPaiType.HuLu;
		elseif(this.isTongHua(huaNums))then 	--是同花
			return SSSPaiType.TongHua;
		elseif(this.isShunZi(dianNums) and table.getrepeat(dianNums)==5)then 	--只有连续的数才是顺子 不能有重复的 
			return SSSPaiType.ShunZi;
		elseif(this.isTeShuShunZi(dianNums))then --12345也是顺子
			return SSSPaiType.ShunZi;
		elseif(this.isSanTiao(dianNums))then --是三条
			return SSSPaiType.SanTiao;
		elseif(this.isLiangDui(dianNums))then 	--是两对
			return SSSPaiType.LiangDui;
		elseif(this.isDuiZi(dianNums))then --不对子
			return SSSPaiType.DuiZi;
		else
			return SSSPaiType.SanPai;
		end
	end
	
end
--判断是否顺子 带王
function GetSSSPaiType.isShunZi(paiNums)
	local n = #paiNums
    --计算0的数量
    local joker = 0
    for i=1,n do
        if paiNums[i] == 0 then
            joker = joker + 1
        end
    end
    --排序
    local sortt = function(a, b)
        return a < b
    end
    table.sort(paiNums,sortt)
    local pre = paiNums[1]
    --计算总的补充数量
    local x = 0
    for i=2,n do
        local tx = paiNums[i] - pre
        x = x + tx - 1
        pre = paiNums[i]
    end
    --如果x比0的数量少,那么可以将数列补充成连续的
    if x <= joker then --是顺子
    	return true;
    else
    	--不是顺子
        return false;
    end
end
--12345也是顺子 特殊判断
function GetSSSPaiType.isTeShuShunZi(paiNums)
	local teshu = {2,3,4,5,14};
	table.sort(paiNums)
	if(table.isEquation(teshu,paiNums))then
		return true;
	else
		return false;
	end
end
--是否同花
function GetSSSPaiType.isTongHua(huaNums)
	if(huaNums[1]==huaNums[2] and huaNums[1]==huaNums[3] and huaNums[1]==huaNums[3] and huaNums[1]==huaNums[4] and huaNums[1]==huaNums[5]) then
		return true;
	else
		return false;
	end
end
--是否五同
function GetSSSPaiType.isWuTong(paiNums)
	if(paiNums[1]==paiNums[2] and paiNums[1]==paiNums[3] and paiNums[1]==paiNums[3] and paiNums[1]==paiNums[4] and paiNums[1]==paiNums[5]) then
		return true;
	else
		return false;
	end
	return false;
end
--判断是否葫芦
function GetSSSPaiType.isHuLu(paiNums)
    if paiNums[1] == paiNums[2] and paiNums[1] == paiNums[3] then
        if paiNums[4] == paiNums[5] then
            return true
        end
    end
    if paiNums[3] == paiNums[4] and paiNums[3] == paiNums[5] then
        if paiNums[1] == paiNums[2] then
            return true
        end
    end
    return false
end
--判断是否三条
function GetSSSPaiType.isSanTiao(paiNums)
	table.sort(paiNums)
	if(paiNums[1]==paiNums[2]and paiNums[2]==paiNums[3])then 
		return true;
	elseif(paiNums[2]==paiNums[3]and paiNums[3]==paiNums[4])then 
		return true;
	elseif(paiNums[3]==paiNums[4]and paiNums[4]==paiNums[5])then 
		return true;
	else
    	return false
	end
end
--判断是否炸弹
function GetSSSPaiType.isZhaDan(paiNums)
	table.sort(paiNums) --避免浪费性能 排序后单牌只会在1或者5
    if paiNums[1] == paiNums[2] and paiNums[1] == paiNums[3] and paiNums[1] == paiNums[4] then
        return true
    elseif paiNums[2] == paiNums[3] and paiNums[2] == paiNums[4] and paiNums[2] == paiNums[5] then
    	return true
    else
    	return false
    end
end
--判断是否两对
function GetSSSPaiType.isLiangDui(paiNums)
	table.sort(paiNums) --避免浪费性能 排序后单牌只会在1 3 5
	if paiNums[1] == paiNums[2] and paiNums[4] == paiNums[5] then
		return true
	elseif paiNums[2] == paiNums[3] and paiNums[4] == paiNums[5] then
		return true
	elseif paiNums[1] == paiNums[2] and paiNums[2] == paiNums[3] then
		return true
	else
		return false
	end
end
--判断是否是对子 没有王
function GetSSSPaiType.isDuiZi(paiNums)
	table.sort(paiNums)
	if(paiNums[1]==paiNums[2] or paiNums[2]==paiNums[3] or paiNums[3]==paiNums[4] or paiNums[4]==paiNums[5])then 
		return true
	else
		return false
	end
end
-------------------------有一个王判断-------------------------
--判断是否五同 有一个王
function GetSSSPaiType.isWuTongHasOneJoker(paiNums)
	if (paiNums[1]==paiNums[2] and paiNums[1]==paiNums[3] and paiNums[1]==paiNums[4])then
		return true
	else
		return false
	end
end
--判断是否同花 有一个王
function GetSSSPaiType.isTongHuaHasOneJoker(huaNums)
	if(huaNums[1]==huaNums[2] and huaNums[1]==huaNums[3] and huaNums[1]==huaNums[4])then
		return true
	else
		return false
	end
end
--判断是否特殊顺子 有一个王 就是和王能组成A2345的顺子
function GetSSSPaiType.isTeShuShunZiHasOneJoker(paiNums)
	table.sort(paiNums) --首先吧自己的手牌格式化
	local teshu1 = {2,3,4,5};
	local teshu2 = {3,4,5,14};
	local teshu3 = {2,4,5,14};
	local teshu4 = {2,3,5,14};
	local teshu5 = {2,3,4,14};
	if(table.isEquation(teshu1,paiNums) or table.isEquation(teshu2,paiNums) or table.isEquation(teshu3,paiNums) or table.isEquation(teshu4,paiNums) or table.isEquation(teshu5,paiNums))then
		return true;
	else
		return false;
	end
end
--判断是否是炸弹 有一个王
function GetSSSPaiType.isZhaDanHasOneJoker(paiNums)
	table.sort(paiNums) --格式化牌 让散牌在1或者4
	if(paiNums[1]==paiNums[2] and paiNums[1]==paiNums[3])then 
		return true;
	elseif(paiNums[2]==paiNums[3] and paiNums[2]==paiNums[4])then 
		return true;
	else
		return false;
	end
end
--判断是否是葫芦 有一个王
function GetSSSPaiType.isHuLuHasOneJoker(paiNums)
	table.sort(paiNums)
	if(paiNums[1]==paiNums[2] and paiNums[3] == paiNums[4])then 
		return true;
	else
		return false;
	end
end
--判断是否三条 有一个王
function GetSSSPaiType.isSnaTiaoHasOneJoker(paiNums)
	table.sort(paiNums)
	if paiNums[1] == paiNums[2] or paiNums[2] == paiNums[3] or paiNums[3]==paiNums[4] then
        return true
    else
    	return false
	end
end
--判断是否两对 有一个王
function GetSSSPaiType.isLiangDuiHasOneJoker(paiNums)
	table.sort(paiNums)
	if(paiNums[1]==paiNums[2] or paiNums[2]==paiNums[3] or paiNums[3]==paiNums[4])then 
		return true
	else
		return false
	end
end
-------------------------有两个王判断-------------------------
--判断是否五同
function GetSSSPaiType.isWuTongHasTwoJoker(paiNums)
	if(paiNums[1] == paiNums[2] and paiNums[1] == paiNums[3])then 
		return true;
	else
		return false;
	end
end
--判断是否同花
function GetSSSPaiType.isTongHuaHasTwoJoker(huaNums)
	if(huaNums[1] == huaNums[2] and huaNums[1] == huaNums[3])then 
		return true;
	else
		return false;
	end
end
--判断是否炸弹
function GetSSSPaiType.isZhaDanHasTwoJoker(paiNums)
	table.sort(paiNums)
	if(paiNums[1] == paiNums[2] or paiNums[2] == paiNums[3])then 
		return true;
	else
		return false;
	end
end
--特殊顺子 就是和王能组成A2345的顺子
function GetSSSPaiType.isTeShuShunZiHasTwoJoker(paiNums)
	table.sort(paiNums)
	local teshu1 = {2,3,14};
	local teshu2 = {2,4,14};
	local teshu3 = {2,5,14};
	local teshu4 = {3,4,14};
	local teshu5 = {3,5,14};
	local teshu6 = {2,3,4};
	local teshu7 = {2,3,5};
	local teshu8 = {2,4,5};
	local teshu9 = {3,4,5};
	if(table.isEquation(teshu1,paiNums) or table.isEquation(teshu2,paiNums) or table.isEquation(teshu3,paiNums) or table.isEquation(teshu4,paiNums) or table.isEquation(teshu5,paiNums) or table.isEquation(teshu6,paiNums) or table.isEquation(teshu7,paiNums) or table.isEquation(teshu8,paiNums) or table.isEquation(teshu9,paiNums))then
		return true;
	else
		return false;
	end
end

使用时仅需调用GetSSSPaiType.Gtetype()即可,牌类型如下:十三水中各种牌型判断LUA版_第1张图片

用到的牌型枚举:

SSSPaiData = {};
SSSPaiData[SSSPaiType.SanPai] = {typeName = "散牌",imageName = "sanpai"};
SSSPaiData[SSSPaiType.DuiZi] = {typeName = "对子",imageName = "duizi"};
SSSPaiData[SSSPaiType.LiangDui] = {typeName = "两对",imageName = "liangdui"};
SSSPaiData[SSSPaiType.SanTiao] = {typeName = "三条",imageName = "santiao"};
SSSPaiData[SSSPaiType.ShunZi] = {typeName = "顺子",imageName = "shunzi"};
SSSPaiData[SSSPaiType.TongHua] = {typeName = "同花",imageName = "tonghua"};
SSSPaiData[SSSPaiType.HuLu] = {typeName = "葫芦",imageName = "hulu"};
SSSPaiData[SSSPaiType.ZhaDan] = {typeName = "炸弹",imageName = "zhadan"};
SSSPaiData[SSSPaiType.TongHuaShun] = {typeName = "同花顺",imageName = "tonghuashun"};
SSSPaiData[SSSPaiType.WuTong] = {typeName = "五同",imageName = "tiezhi"};
SSSPaiData[SSSPaiType.Error] = {typeName = "错误",imageName = "error"};

用到的公共方法:可以放本类,也可以放全局,个人认为放全局会方便一点,这样其他地方也可以使用全局进行调用

--切割牌 带王
function GetPokerHasJoker(pkId)
	local sets = string.split(pkId, "_");
	
	local color = tonumber(sets[1]);
	local num = tonumber(sets[2]);
	
	if(num == 1) then
		num = 14;
	end
	
	if(color == 5)then --如果是王 就吧点数当成0
		num = 0;
	end
	return num, color;
end

--判断value是否存在
--字典/数组
function table.ContainsValue(tb, val)
	if(type(tb) == "table") then
		for k, v in pairs(tb) do
			if equals(v, val) then
				return true;
			end
		end
	else
		return equals(tb, val);
	end
	return false;
end

--删除字典中值
function table.RemoveValue(tb, val)
	for k, v in pairs(tb) do
		if(v == val) then
			table.remove(tb, k);
			return;
		end
	end
end

--判断两个数组是否相等
function table.isEquation(tb1,tb2)
	if(#tb1~=#tb2)then
		return false;
	end
	for i,v in pairs(tb1) do
		if tb1[i] ~= tb2[i]then
			return false;
		end
	end
	return true;
end

--检查表内相同元素个数
function table.getrepeat(t)
	local check = {} 
    local n = {}
    for key, value in pairs(t) do
        if not check[value] then
            n[key] = value
            check[value] = value
        end
    end
    return #n;
end

写在后面:方法是临时拼凑,可能会有bug,我会持续修复,直至修复完美!!

你可能感兴趣的:(lua,开发语言,unity,游戏引擎)