赢三张的牌型大题分为6种:单牌<对子<顺子<同花<同花顺<豹子。
CardType = { UNDEFINE=0, --单牌 DUI_ZI =1, --对子 SHUN_ZI =2, --顺子 TONG_HUA=3, --同花 TONG_HUA_SHUN = 4, --同花顺 BAO_ZI = 5, --豹子 }
首先为了方便我们的牌型判定逻辑,我们先将3张牌数据cards按牌值大小进行排序
function compByCardsValue(a, b)return a.card_value < b.card_valueendfunction cardTool.sortByLaiziAsc(cards)table.sort(cards, compByCardsValue);end
接下来我们从大到小的写出各牌型的判断逻辑
豹子:三张牌值相同的牌型,由于是有序的,故只需判断第一张与第三张牌值是否相等即可
function cardTool.isBaozi( cards)if cards[1].card_value==cards[3].card_value thenreturn trueelsereturn falseendend
同花顺:即满足同花又满足顺子的牌型
function cardTool.isTongHuaShun(cards)local TagTongHualocal TagShunZiTagTongHua = cardTool.isTongHua(cards)TagShunZi = cardTool.isShunZi(cards)if TagTongHua and TagShunZi then --既满足同花也满足顺子的时候,就是同花顺,包含23Areturn trueelsereturn falseendend
同花:三张牌花色相同
function cardTool.isTongHua (cards)if cards[1].card_Color==cards[2].card_Color and cards[1].card_Color==cards[3].card_Color thenreturn trueelsereturn falseendend
顺子:三张牌牌值依次递增1,同时还包括A23特殊牌型
function cardTool.isShunZi(cards)if isA32(cards) thenreturn trueendif cards[3].card_value - cards[2].card_value == 1 and cards[2].card_value - cards[1].card_value == 1 thenreturn trueelsereturn falseendend
对子:两张牌牌值相等,但第一张与第三张不能相等,否则就是豹子了
function cardTool.isDuiZi(cards)if cards[1].card_value ~= cards[3].card_value thenif cards[1].card_value == cards[2].card_value thenreturn trueendif cards[2].card_value == cards[3].card_value thenreturn trueendreturn falseelsereturn falseendend
若以上牌型都不满足,即为单牌牌型。
封装获取牌型函数:
function cardTool.getCardType(cards)cardTool.sortByCardsValue(cards)local card_type = CardType.UNDEFINElocal retif (cards) then--《豹子ret = cardTool.isBaozi(cards)if ret == true thencard_type = CardType.BAO_ZIreturn card_typeend--《同花顺ret = cardTool.isTongHuaShun(cards)if (ret == true) thencard_type = CardType.TONG_HUA_SHUN;return card_typeend--《同花ret = cardTool.isTongHua(cards)if (ret == true) thencard_type = CardType.TONG_HUA;return card_typeend--《顺子ret = cardTool.isShunZi(cards)if (ret == true) thencard_type = CardType.SHUN_ZI;return card_typeend-- 《对子ret = cardTool.isDuiZi(cards)if (ret == true) thencard_type = CardType.DUI_ZI;return card_typeendendreturn card_type --UNDEFINE=0,--初始单牌end
另外存在特殊牌型,A23及235(A23算最小的顺子,非同花的235可以管豹子)
--》A23function isA32(cards)if cards[1].card_value == 2 and cards[2].card_value == 3 and cards[3].card_value == 14 thenreturn trueelsereturn falseendend--》235function is235(cards)if cards[1].card_value == 2 and cards[2].card_value == 3 and cards[3].card_value == 5 thenreturn trueelsereturn falseendend
翻译函数:
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 "豹子"endreturn "异常牌型"end
按照上一章的测试函数,我们输出一下牌的类型
local cardBuffer =cardTool.RandCardList()--cardBuffer[1]=6--cardBuffer[2]=7+16--cardBuffer[3]=7--cardBuffer[4]=5+16--cardBuffer[5]=7+32--cardBuffer[6]=7+48local cards1={}local cards2={}for i=1,6,1 dolocal cardColor = luabit.band(cardBuffer[i] , 0xF0)local cardValue = luabit.band(cardBuffer[i] , 0x0F)local cardinfo ={ card_value = cardValue; card_Color = cardColor; }if i >3 thencards2[i-3] = cardinfoelsecards1[i] = cardinfoendendprint_t(cardTool.getCardTypeNamebyType(cardTool.getCardType(cards1)))print_t(cardTool.getCardNamebyCards(cards1))print_t(cardTool.getCardTypeNamebyType(cardTool.getCardType(cards2)))print_t(cardTool.getCardNamebyCards(cards2))
PS:print_t是我自己写的实现输出table类型的输出函数
输出:
对子 方块9红桃K梅花K 单牌 方块3黑桃J红桃Q
以上就是牌型判定相关的处理函数,下一章我们实现牌型比较的相关逻辑。