手记

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 thenreturn trueendif a.card_value > b.card_value thenreturn falseendreturn a.card_color < b.card_colorendfunction cardTool.sortByCardsValue(cards)table.sort(cards, compByCardsValue);end


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


function cardTool.is_small_niu(cards)local sum = 0     for i = 1,#cards dosum = sum + cards[i].card_countendif sum <= 10 thenreturn trueelsereturn falseendendfunction cardTool.is_bomb(cards)if cards[1].card_value == cards[4].card_value thenreturn trueelseif cards[2].card_value == cards[5].card_value thenreturn trueelsereturn falseendendfunction cardTool.is_gold_niu(cards)if cards[1].card_value > 10 thenreturn trueelsereturn falseendendfunction cardTool.is_silver_niu(cards)if cards[2].card_value > 10 and cards[1].card_value == 10 thenreturn trueelsereturn falseendend


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


function cardTool.getNiubyCards(cards)local lave = 0     --余数for i = 1,#cards dolave = lave + cards[i].card_countendlave = lave % 10for i = 1,#cards - 1 dofor j = i + 1,#cards doif(cards[i].card_count+cards[j].card_count)%10 == lave thenif lave == 0 thenreturn 10elsereturn laveendendendendreturn 0end


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


function cardTool.getTypebyCards(cards)cardTool.sortByCardsValue(cards)local cardtype = CardType.NOT_NIUif cardTool.is_small_niu(cards) thencardtype = CardType.SMALL_NIUreturn cardtypeendif cardTool.is_bomb(cards) thencardtype = CardType.BOMBreturn cardtypeend if cardTool.is_gold_niu(cards) thencardtype = CardType.GOLD_NIUreturn cardtypeend if cardTool.is_silver_niu(cards) thencardtype = CardType.SILVER_NIUreturn cardtypeend cardtype=cardTool.getNiubyCards(cards)return cardtypeend


翻译函数:


function cardTool.getCardTypeNamebyType(CardType)if CardType==0 thenreturn "没牛"endif CardType==1 thenreturn "牛一"endif CardType==2 thenreturn "牛二"endif CardType==3 thenreturn "牛三"endif CardType==4 thenreturn "牛四"endif CardType==5 thenreturn "牛五"endif CardType==6 thenreturn "牛六"endif CardType==7 thenreturn "牛七"endif CardType==8 thenreturn "牛八"endif CardType==9 thenreturn "牛九"endif CardType==10 thenreturn "牛牛"endif CardType==11 thenreturn "银牛"endif CardType==12 thenreturn "金牛"endif CardType==13 thenreturn "炸弹"endif CardType==14 thenreturn "五小牛"endreturn "异常牌型"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人推荐
随时随地看视频
慕课网APP