lua——赢三张牌型处理相关算法(下)——牌型比较
上文中我们已经实现了赢三张牌型的判定方法,同时我们也给出了一个枚举结构CardType。不难理解,若两组牌不是同一牌型的话,直接根据枚举的值进行比对就可以了。若是相同牌型还需要进行进一步的判断。所以我们暂且将比牌函数分为两个分支
- --@比牌接口函数
- --@ my_Cards, 本家牌,
- --@ pre_Cards,下家牌,
- --@ ret true/false
- function 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)
- end
- return winorlose
- end
若牌型不同,直接根据枚举值判断,这里有一个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 then
- my_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
- end
- end
- if next_Cards_235 then
- if cardTool.isTongHua(next_Cards) then
- return win
- else
- return lose
- end
- end
- 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
- end
- end
同牌型的牌比较就要分别处理了:
豹子:比较单张牌牌值
同花顺:比较第三张牌,同时考虑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 lose
- end
- end
最后用我们的测试函数验证一下:
- 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+48
- local 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
- end
- end
- print_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
测试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方块4
- true
测试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方块6
- false
好啦~赢三张系列已经完结