0
点赞
收藏
分享

微信扫一扫

lua——牛牛牌型处理相关算法(中)——牌型判定



牛牛的牌型按从小到大的顺序分为:无牛<有牛<牛牛<银牛<金牛<炸弹<五小牛。


算牛的方式为:先选出三张牌,若能组成十的整数倍 即为有牛,然后再看剩余两张牌除以十的余数。余几就是牛几,若正好也能整除十,即为牛牛。若无法选出三张组成十的整数倍的牌即为无牛。

银牛:1张10 加4张大于10的牌

金牛:5张大于10的牌

炸弹:存在四张相同的牌

五小牛:五张牌总数值相加小于等于10


首先定义牌型,因为客户端不同数的牛显示不一样,所以把有牛的九种情况都分开定义

--五小牛>炸弹>金牛>银牛>牛牛>有牛>没牛
CardType =
{

NOT_NIU=0, --没牛
NIU_1 =1, --牛一
NIU_2 =2, --牛二
NIU_3 =3, --牛三
NIU_4 =4, --牛四
NIU_5 =5, --牛五
NIU_6 =6, --牛六
NIU_7 =7, --牛七
NIU_8 =8, --牛八
NIU_9 =9, --牛九
NIU_NIU =10, --牛牛
SILVER_NIU =11, --银牛
GOLD_NIU=12, --金牛
BOMB = 13, --炸弹
SMALL_NIU = 14, --五小牛
}


为了方便计算,在判定牌型前要对牌进行排序。

function compByCardsValue(a, b)

if a.card_value < b.card_value then
return true
end

if a.card_value > b.card_value then
return false
end

return a.card_color < b.card_color
end


function cardTool.sortByCardsValue(cards)
table.sort(cards, compByCardsValue);
end


下面进行牌值的判定,先计算特殊的牌型

function cardTool.is_small_niu(cards)
local sum = 0
for i = 1,#cards do
sum = sum + cards[i].card_count
end
if sum <= 10 then
return true
else
return false
end
end

function cardTool.is_bomb(cards)

if cards[1].card_value == cards[4].card_value then
return true
elseif cards[2].card_value == cards[5].card_value then
return true
else
return false
end
end

function cardTool.is_gold_niu(cards)
if cards[1].card_value > 10 then
return true
else
return false
end
end

function cardTool.is_silver_niu(cards)
if cards[2].card_value > 10 and cards[1].card_value == 10 then
return true
else
return false
end
end


判定牛数的算法其实很简单,我们先算出五张牌总值除以十的余数,然后再枚举两张牌,若存在两张牌之和除以十的余数等于五张牌除以十的余数,那么其他三张牌必然总和为十的倍数。那么这个余数就是牛数。

function cardTool.getNiubyCards(cards)
local lave = 0 --余数
for i = 1,#cards do
lave = lave + cards[i].card_count
end
lave = lave % 10
for i = 1,#cards - 1 do
for j = i + 1,#cards do
if(cards[i].card_count+cards[j].card_count)%10 == lave then
if lave == 0 then
return 10
else
return lave
end
end
end
end

return 0
end


这样我们所有分支牌型的判定逻辑就都实现了,下面写出接口

function cardTool.getTypebyCards(cards)

cardTool.sortByCardsValue(cards)
local cardtype = CardType.NOT_NIU

if cardTool.is_small_niu(cards) then
cardtype = CardType.SMALL_NIU
return cardtype
end
if cardTool.is_bomb(cards) then
cardtype = CardType.BOMB
return cardtype
end
if cardTool.is_gold_niu(cards) then
cardtype = CardType.GOLD_NIU
return cardtype
end
if cardTool.is_silver_niu(cards) then
cardtype = CardType.SILVER_NIU
return cardtype
end

cardtype=cardTool.getNiubyCards(cards)

return cardtype
end


翻译函数:

function cardTool.getCardTypeNamebyType(CardType)
if CardType==0 then
return "没牛"
end
if CardType==1 then
return "牛一"
end
if CardType==2 then
return "牛二"
end
if CardType==3 then
return "牛三"
end
if CardType==4 then
return "牛四"
end
if CardType==5 then
return "牛五"
end
if CardType==6 then
return "牛六"
end
if CardType==7 then
return "牛七"
end
if CardType==8 then
return "牛八"
end
if CardType==9 then
return "牛九"
end
if CardType==10 then
return "牛牛"
end
if CardType==11 then
return "银牛"
end
if CardType==12 then
return "金牛"
end
if CardType==13 then
return "炸弹"
end
if CardType==14 then
return "五小牛"
end
return "异常牌型"

end



我们依然引用之前的测试函数来查看一下输出:

print_t(cardTool.getCardTypeNamebyType(cardTool.getTypebyCards(cards1)))
print_t(cardTool.getCardNamebyCards(cards1))

print_t(cardTool.getCardTypeNamebyType(cardTool.getTypebyCards(cards2)))
print_t(cardTool.getCardNamebyCards(cards2))


牛牛

梅花3方块8方块9黑桃J红桃K

牛五

黑桃6红桃9方块10梅花J方块K



以上就是牌型判定相关的处理函数,下一章我们实现牌型比较的相关逻辑。








举报

相关推荐

0 条评论