lua——赢三张牌型处理相关算法(下)——牌型比较
来源:互联网 发布:大数据统计学基础 编辑:程序博客网 时间:2024/06/12 18:07
上文中我们已经实现了赢三张牌型的判定方法,同时我们也给出了一个枚举结构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 winorlose if my_Cards_Type == next_Cards_Type then --牌形相同的情况下 winorlose = CardTypeSame(my_Cards, next_Cards, my_Cards_Type) end if my_Cards_Type ~= next_Cards_Type then --牌形不同的情况下 winorlose = CardTypeDifferent(my_Cards, next_Cards,my_Cards_Type,next_Cards_Type) endreturn winorloseend
若牌型不同,直接根据枚举值判断,这里有一个235管豹子的说法,所以需要单独处理一下豹子事件。
function CardTypeDifferent( my_Cards, next_Cards, my_Cards_Type, next_Cards_Type )local win = true local lose = false local isWinOrlose local my_Cards_Bao_Zi = false local next_Cards_Bao_Zi = false if my_Cards_Type == CardType.BAO_ZI then my_Cards_Bao_Zi = true end if next_Cards_Type == CardType.BAO_ZI then next_Cards_Bao_Zi = true end --如果没有豹子 if my_Cards_Bao_Zi == next_Cards_Bao_Zi then isWinOrlose = my_Cards_Type - next_Cards_Type if isWinOrlose > 0 then return win end if isWinOrlose < 0 then return lose end end if my_Cards_Bao_Zi or next_Cards_Bao_Zi thenmy_Cards_235 = is235(my_Cards) next_Cards_235 = is235(next_Cards) if my_Cards_235 then if cardTool.isTongHua(my_Cards) then return lose else return win endend if next_Cards_235 then if cardTool.isTongHua(next_Cards) then return win else return loseend endif (my_Cards_235 == false) and (next_Cards_235 == false) thenif my_Cards_Type == CardType.BAO_ZI thenreturn winendif next_Cards_Type == CardType.BAO_ZI thenreturn loseend endendend
同牌型的牌比较就要分别处理了:
豹子:比较单张牌牌值
同花顺:比较第三张牌,同时考虑A23特殊顺子情况
同花:从第三张牌开始依次比较
顺子:比较第三张牌,同时考虑A23特殊顺子情况
对牌:首先比较第二张,因为第二张一定是构成对子的那张牌。若相同则再比对(第一张+第三张)
另外:赢三张规定,三张牌值完全相同的情况下,比牌者输
function CardTypeSame( my_Cards, next_Cards, my_Cards_Type ) --------------------------------------豹子-----------------------------local win = true local lose = false local SubValueBaoZi if my_Cards_Type == CardType.Bao_ZI then SubValueBaoZi = my_Cards[1].card_value - next_Cards[1].card_value if SubValueBaoZi == 0 then return lose end if SubValueBaoZi > 0 then return win end if SubValueBaoZi < 0 then return lose end end -------------------------------------同花顺----------------------------- local IsOrNotA32_mycards local IsOrNotA32_nextcards local SubValueSunZi if my_Cards_Type == CardType.TONG_HUA_SHUN then IsOrNotA32_mycards = isA32(my_Cards) IsOrNotA32_nextcards = isA32(next_Cards) --两个都是A32 if IsOrNotA32_mycards and IsOrNotA32_nextcards then return lose end --有一个有A32 if IsOrNotA32_mycards or IsOrNotA32_nextcards then if IsOrNotA32_nextcards then return win else return lose end end --都没有A32 if IsOrNotA32_mycards == false and IsOrNotA32_nextcards == false then SubValueSunZi = my_Cards[3].card_value - next_Cards[3].card_value if SubValueSunZi == 0 then return lose end if SubValueSunZi > 0 then return win end if SubValueSunZi < 0 then return lose end end end --------------------------------------------同花---------------------------------- if my_Cards_Type == CardType.TONG_HUA then if my_Cards[3].card_value - next_Cards[3].card_value > 0 then return win end if my_Cards[3].card_value - next_Cards[3].card_value < 0 then return lose end if my_Cards[2].card_value - next_Cards[2].card_value > 0 then return win end if my_Cards[2].card_value - next_Cards[2].card_value < 0 then return lose end if my_Cards[1].card_value - next_Cards[1].card_value > 0 then return win end if my_Cards[1].card_value - next_Cards[1].card_value < 0 then return lose end return lose end --------------------------------------------顺子---------------------------------- local IsOrNotA32_mycards local IsOrNotA32_nextcards local SubValueSunZi if my_Cards_Type == CardType.SHUN_ZI then IsOrNotA32_mycards = isA32(my_Cards) IsOrNotA32_nextcards = isA32(next_Cards) --两个都是A32 if IsOrNotA32_mycards and IsOrNotA32_nextcards then return lose end --有一个有A32 if IsOrNotA32_mycards or IsOrNotA32_nextcards then if IsOrNotA32_nextcards then return win else return lose end end --都没有A32 if IsOrNotA32_mycards == false and IsOrNotA32_nextcards == false then SubValueSunZi = my_Cards[3].card_value - next_Cards[3].card_value if SubValueSunZi == 0 then return lose end if SubValueSunZi > 0 then return win end if SubValueSunZi < 0 then return lose end end end --------------------------------------------对子---------------------------------- if my_Cards_Type == CardType.DUI_ZI then --第二张牌一定是组成对子的那张牌 local SubValueDuiZi = my_Cards[2].card_value - next_Cards[2].card_value --第二张不等 if SubValueDuiZi > 0 then return win end if SubValueDuiZi < 0 then return lose end --第二张相等 if SubValueDuiZi == 0 then if (my_Cards[1].card_value + my_Cards[3].card_value ) > (next_Cards[1].card_value + next_Cards[3].card_value) then return win else return lose end end end --------------------------------------------单牌---------------------------------- if my_Cards_Type == CardType.UNDEFINE then if my_Cards[3].card_value - next_Cards[3].card_value > 0 then return win end if my_Cards[3].card_value - next_Cards[3].card_value < 0 then return lose end if my_Cards[2].card_value - next_Cards[2].card_value > 0 then return win end if my_Cards[2].card_value - next_Cards[2].card_value < 0 then return lose end if my_Cards[1].card_value - next_Cards[1].card_value > 0 then return win end if my_Cards[1].card_value - next_Cards[1].card_value < 0 then return lose end return loseendend
最后用我们的测试函数验证一下:
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 do local cardColor = luabit.band(cardBuffer[i] , 0xF0) local cardValue = luabit.band(cardBuffer[i] , 0x0F) local cardinfo = { card_value = cardValue; card_Color = cardColor; } if i >3 then cards2[i-3] = cardinfo else cards1[i] = cardinfo endendprint_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梅花K false
cardBuffer[1]=2cardBuffer[2]=3+16cardBuffer[3]=5cardBuffer[4]=4+16cardBuffer[5]=4+32cardBuffer[6]=4+48
输出:
单牌 黑桃2红桃3黑桃5 豹子 红桃4梅花4方块4 true
测试A23逻辑:
cardBuffer[1]=2cardBuffer[2]=3+16cardBuffer[3]=14cardBuffer[4]=4+16cardBuffer[5]=5+32cardBuffer[6]=6+48
输出:
顺子 黑桃2红桃3黑桃A 顺子 红桃4梅花5方块6 false
好啦~赢三张系列已经完结~~~~~~~撒花~~~~~~~同时还要吐槽一个CSDN,之前因为叫zhajinhua侵犯了敏感词结果被封了,宝宝很心塞,宝宝很委屈╮(╯▽╰)╭
阅读全文
2 0
- lua——赢三张牌型处理相关算法(下)——牌型比较
- lua——牛牛牌型处理相关算法(下)——牌型比较
- lua——赢三张牌型处理相关算法(中)——牌型判定
- lua——赢三张牌型处理相关算法(上)——牌值数据
- lua——牛牛牌型处理相关算法(中)——牌型判定
- lua——牛牛牌型处理相关算法(上)——牌值数据
- golang—nginx-lua学习相关
- Lua学习——string字符处理
- lua入门——什么都不懂的情况下写一道算法题
- 文本比较算法——LD算法
- Lua(—)
- 目标检测算法(一)——常见算法比较
- 全排列及相关扩展算法(二)——求字典序下一组排列及全排列算法
- 验证算法——二叉排序树相关算法
- 【机器学习】决策树(下)——CART算法及剪枝处理
- ffmpeg——视频处理相关术语
- (PL之LUA)工欲善其事必先利其器——配置LUA
- Lua与C通信——Lua API(一)
- PCM转WAV格式 Wav数据格式的44个字节头部信息汇总
- Oracle select case when
- [leetcode]: 543. Diameter of Binary Tree
- c++ 操作系统实验 进程调度
- nginx在windows下配置反向代理
- lua——赢三张牌型处理相关算法(下)——牌型比较
- POI导出工具类
- [蓝桥杯] 历届试题 剪格子(dfs+回溯)
- Hello cocos2d-x!
- java实现冒泡排序
- java导出Excel表格
- Gradle 15分钟入门教程
- iOS开发技巧
- codevs 1427 特种部队 (双路DP)