上文中我们已经实现了赢三张牌型的判定方法,同时我们也给出了一个枚举结构CardType。不难理解,若两组牌不是同一牌型的话,直接根据枚举的值进行比对就可以了。若是相同牌型还需要进行进一步的判断。所以我们暂且将比牌函数分为两个分支
--@比牌接口函数--@ my_Cards, 本家牌,--@ pre_Cards,下家牌,--@ ret true/falsefunction cardTool.isOvercomePrev(my_Cards, next_Cards)--获取各自牌形local my_Cards_Type = cardTool.getCardType(my_Cards)local next_Cards_Type = cardTool.getCardType(next_Cards)local winorloseif my_Cards_Type == next_Cards_Type then --牌形相同的情况下winorlose = CardTypeSame(my_Cards, next_Cards, my_Cards_Type)endif my_Cards_Type ~= next_Cards_Type then --牌形不同的情况下winorlose = CardTypeDifferent(my_Cards, next_Cards,my_Cards_Type,next_Cards_Type)end return winorloseend
若牌型不同,直接根据枚举值判断,这里有一个235管豹子的说法,所以需要单独处理一下豹子事件。
function CardTypeDifferent( my_Cards, next_Cards, my_Cards_Type, next_Cards_Type ) local win = truelocal lose = falselocal isWinOrloselocal my_Cards_Bao_Zi = falselocal next_Cards_Bao_Zi = falseif my_Cards_Type == CardType.BAO_ZI thenmy_Cards_Bao_Zi = trueendif next_Cards_Type == CardType.BAO_ZI thennext_Cards_Bao_Zi = trueend--如果没有豹子if my_Cards_Bao_Zi == next_Cards_Bao_Zi thenisWinOrlose = my_Cards_Type - next_Cards_Typeif isWinOrlose > 0 thenreturn winendif isWinOrlose < 0 thenreturn loseendendif my_Cards_Bao_Zi or next_Cards_Bao_Zi then my_Cards_235 = is235(my_Cards)next_Cards_235 = is235(next_Cards)if my_Cards_235 thenif cardTool.isTongHua(my_Cards) thenreturn loseelsereturn winend endif next_Cards_235 thenif cardTool.isTongHua(next_Cards) thenreturn winelsereturn lose endend if (my_Cards_235 == false) and (next_Cards_235 == false) then if my_Cards_Type == CardType.BAO_ZI then return win end if next_Cards_Type == CardType.BAO_ZI then return lose end end endend
同牌型的牌比较就要分别处理了:
豹子:比较单张牌牌值
同花顺:比较第三张牌,同时考虑A23特殊顺子情况
同花:从第三张牌开始依次比较
顺子:比较第三张牌,同时考虑A23特殊顺子情况
对牌:首先比较第二张,因为第二张一定是构成对子的那张牌。若相同则再比对(第一张+第三张)
另外:赢三张规定,三张牌值完全相同的情况下,比牌者输
function CardTypeSame( my_Cards, next_Cards, my_Cards_Type )--------------------------------------豹子----------------------------- local win = truelocal lose = falselocal SubValueBaoZiif my_Cards_Type == CardType.Bao_ZI thenSubValueBaoZi = my_Cards[1].card_value - next_Cards[1].card_valueif SubValueBaoZi == 0 thenreturn loseendif SubValueBaoZi > 0 thenreturn winendif SubValueBaoZi < 0 thenreturn loseendend-------------------------------------同花顺-----------------------------local IsOrNotA32_mycardslocal IsOrNotA32_nextcardslocal SubValueSunZiif my_Cards_Type == CardType.TONG_HUA_SHUN thenIsOrNotA32_mycards = isA32(my_Cards)IsOrNotA32_nextcards = isA32(next_Cards)--两个都是A32if IsOrNotA32_mycards and IsOrNotA32_nextcards thenreturn loseend--有一个有A32if IsOrNotA32_mycards or IsOrNotA32_nextcards thenif IsOrNotA32_nextcards thenreturn winelsereturn loseendend--都没有A32if IsOrNotA32_mycards == false and IsOrNotA32_nextcards == false thenSubValueSunZi = my_Cards[3].card_value - next_Cards[3].card_valueif SubValueSunZi == 0 thenreturn loseendif SubValueSunZi > 0 thenreturn winendif SubValueSunZi < 0 thenreturn loseendendend--------------------------------------------同花----------------------------------if my_Cards_Type == CardType.TONG_HUA thenif my_Cards[3].card_value - next_Cards[3].card_value > 0 thenreturn winendif my_Cards[3].card_value - next_Cards[3].card_value < 0 thenreturn loseendif my_Cards[2].card_value - next_Cards[2].card_value > 0 thenreturn winendif my_Cards[2].card_value - next_Cards[2].card_value < 0 thenreturn loseend if my_Cards[1].card_value - next_Cards[1].card_value > 0 thenreturn winendif my_Cards[1].card_value - next_Cards[1].card_value < 0 thenreturn loseendreturn loseend--------------------------------------------顺子----------------------------------local IsOrNotA32_mycardslocal IsOrNotA32_nextcardslocal SubValueSunZiif my_Cards_Type == CardType.SHUN_ZI thenIsOrNotA32_mycards = isA32(my_Cards)IsOrNotA32_nextcards = isA32(next_Cards)--两个都是A32if IsOrNotA32_mycards and IsOrNotA32_nextcards thenreturn loseend--有一个有A32if IsOrNotA32_mycards or IsOrNotA32_nextcards thenif IsOrNotA32_nextcards thenreturn winelsereturn loseendend--都没有A32if IsOrNotA32_mycards == false and IsOrNotA32_nextcards == false thenSubValueSunZi = my_Cards[3].card_value - next_Cards[3].card_valueif SubValueSunZi == 0 thenreturn loseendif SubValueSunZi > 0 thenreturn winendif SubValueSunZi < 0 thenreturn loseendendend--------------------------------------------对子----------------------------------if my_Cards_Type == CardType.DUI_ZI then--第二张牌一定是组成对子的那张牌local SubValueDuiZi = my_Cards[2].card_value - next_Cards[2].card_value--第二张不等if SubValueDuiZi > 0 thenreturn winendif SubValueDuiZi < 0 thenreturn loseend--第二张相等if SubValueDuiZi == 0 thenif (my_Cards[1].card_value + my_Cards[3].card_value ) > (next_Cards[1].card_value + next_Cards[3].card_value) thenreturn winelsereturn loseendendend--------------------------------------------单牌----------------------------------if my_Cards_Type == CardType.UNDEFINE thenif my_Cards[3].card_value - next_Cards[3].card_value > 0 thenreturn winendif my_Cards[3].card_value - next_Cards[3].card_value < 0 thenreturn loseendif my_Cards[2].card_value - next_Cards[2].card_value > 0 thenreturn winendif my_Cards[2].card_value - next_Cards[2].card_value < 0 thenreturn loseend if my_Cards[1].card_value - next_Cards[1].card_value > 0 thenreturn winendif my_Cards[1].card_value - next_Cards[1].card_value < 0 thenreturn loseendreturn lose endend
最后用我们的测试函数验证一下:
local cardBuffer =cardTool.RandCardList()--cardBuffer[1]=2--cardBuffer[2]=3+16--cardBuffer[3]=5--cardBuffer[4]=4+16--cardBuffer[5]=4+32--cardBuffer[6]=4+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))print_t(cardTool.isOvercomePrev(cards1,cards2))
输出:
单牌黑桃4梅花6方块Q同花梅花5梅花9梅花Kfalse
测试235逻辑:
cardBuffer[1]=2 cardBuffer[2]=3+16 cardBuffer[3]=5 cardBuffer[4]=4+16 cardBuffer[5]=4+32 cardBuffer[6]=4+48
输出:
单牌黑桃2红桃3黑桃5豹子红桃4梅花4方块4true
测试A23逻辑:
cardBuffer[1]=2 cardBuffer[2]=3+16 cardBuffer[3]=14 cardBuffer[4]=4+16 cardBuffer[5]=5+32 cardBuffer[6]=6+48
输出:
顺子黑桃2红桃3黑桃A顺子红桃4梅花5方块6false
好啦~赢三张系列已经完结~~~~~~~撒花~~~~~~~同时还要吐槽一个CSDN,之前因为叫zhajinhua侵犯了敏感词结果被封了,宝宝很心塞,宝宝很委屈╮(╯╰)╭
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦