lua——赢三张牌型处理相关算法(下)——牌型比较

上文中我们已经实现了赢三张牌型的判定方法,同时我们也给出了一个枚举结构CardType。不难理解,若两组牌不是同一牌型的话,直接根据枚举的值进行比对就可以了。若是相同牌型还需要进行进一步的判断。所以我们暂且将比牌函数分为两个分支

 

[plain] view plain copy
 
  1. --@比牌接口函数  
  2. --@ my_Cards, 本家牌,  
  3. --@ pre_Cards,下家牌,  
  4. --@ ret true/false  
  5. function cardTool.isOvercomePrev(my_Cards, next_Cards)  
  6.     --获取各自牌形  
  7.     local my_Cards_Type = cardTool.getCardType(my_Cards)  
  8.     local next_Cards_Type = cardTool.getCardType(next_Cards)  
  9.     local winorlose  
  10.     if  my_Cards_Type == next_Cards_Type then --牌形相同的情况下  
  11.         winorlose =  CardTypeSame(my_Cards, next_Cards, my_Cards_Type)  
  12.     end  
  13.     if my_Cards_Type ~= next_Cards_Type  then --牌形不同的情况下  
  14.     
  15.         winorlose =  CardTypeDifferent(my_Cards, next_Cards,my_Cards_Type,next_Cards_Type)  
  16.     end  
  17.     return winorlose  
  18. end  


若牌型不同,直接根据枚举值判断,这里有一个235管豹子的说法,所以需要单独处理一下豹子事件。

 

 

[plain] view plain copy
 
  1. function  CardTypeDifferent( my_Cards, next_Cards, my_Cards_Type, next_Cards_Type )  
  2.     local  win  = true  
  3.     local  lose = false  
  4.   
  5.     local isWinOrlose  
  6.   
  7.     local my_Cards_Bao_Zi = false  
  8.     local next_Cards_Bao_Zi = false  
  9.   
  10.     if my_Cards_Type == CardType.BAO_ZI then  
  11.         my_Cards_Bao_Zi = true  
  12.     end  
  13.   
  14.     if next_Cards_Type == CardType.BAO_ZI then  
  15.         next_Cards_Bao_Zi = true  
  16.     end  
  17.   
  18.     --如果没有豹子  
  19.     if my_Cards_Bao_Zi == next_Cards_Bao_Zi then  
  20.         isWinOrlose = my_Cards_Type - next_Cards_Type  
  21.         if isWinOrlose > 0 then  
  22.             return win  
  23.         end  
  24.   
  25.         if isWinOrlose < 0 then  
  26.             return lose  
  27.         end  
  28.     end  
  29.   
  30.     if my_Cards_Bao_Zi or next_Cards_Bao_Zi then  
  31.         my_Cards_235 = is235(my_Cards)  
  32.         next_Cards_235 = is235(next_Cards)  
  33.         if my_Cards_235  then  
  34.             if cardTool.isTongHua(my_Cards) then  
  35.                 return lose  
  36.             else  
  37.                 return win  
  38.             end  
  39.         end  
  40.         if next_Cards_235 then  
  41.             if cardTool.isTongHua(next_Cards) then  
  42.                 return win  
  43.             else  
  44.                 return lose  
  45.             end  
  46.         end  
  47.         if (my_Cards_235 == false) and (next_Cards_235 == false) then  
  48.             if  my_Cards_Type == CardType.BAO_ZI then  
  49.                 return win  
  50.             end  
  51.   
  52.             if next_Cards_Type == CardType.BAO_ZI then  
  53.                 return lose  
  54.             end  
  55.         end  
  56.     end  
  57. end  


同牌型的牌比较就要分别处理了:

 

豹子:比较单张牌牌值

同花顺:比较第三张牌,同时考虑A23特殊顺子情况

同花:从第三张牌开始依次比较

顺子:比较第三张牌,同时考虑A23特殊顺子情况

对牌:首先比较第二张,因为第二张一定是构成对子的那张牌。若相同则再比对(第一张+第三张)

另外:赢三张规定,三张牌值完全相同的情况下,比牌者输

 

[plain] view plain copy
 
  1. function CardTypeSame( my_Cards, next_Cards, my_Cards_Type )  
  2.     --------------------------------------豹子-----------------------------  
  3.     local  win  = true  
  4.     local  lose = false  
  5.     local  SubValueBaoZi  
  6.     if  my_Cards_Type == CardType.Bao_ZI then  
  7.         SubValueBaoZi = my_Cards[1].card_value - next_Cards[1].card_value  
  8.         if  SubValueBaoZi == 0 then  
  9.             return lose  
  10.         end  
  11.         if SubValueBaoZi > 0 then  
  12.             return win  
  13.         end  
  14.         if SubValueBaoZi < 0 then  
  15.             return lose  
  16.         end  
  17.     end  
  18.     -------------------------------------同花顺-----------------------------  
  19.   
  20.     local  IsOrNotA32_mycards  
  21.     local  IsOrNotA32_nextcards  
  22.     local  SubValueSunZi  
  23.     if  my_Cards_Type == CardType.TONG_HUA_SHUN then  
  24.   
  25.         IsOrNotA32_mycards = isA32(my_Cards)  
  26.         IsOrNotA32_nextcards = isA32(next_Cards)  
  27.   
  28.         --两个都是A32  
  29.         if IsOrNotA32_mycards  and IsOrNotA32_nextcards then  
  30.             return lose  
  31.         end  
  32.   
  33.         --有一个有A32  
  34.         if IsOrNotA32_mycards  or IsOrNotA32_nextcards then  
  35.             if IsOrNotA32_nextcards then  
  36.                 return win  
  37.             else  
  38.                 return lose  
  39.             end  
  40.         end  
  41.   
  42.         --都没有A32  
  43.         if IsOrNotA32_mycards  == false and IsOrNotA32_nextcards == false then  
  44.             SubValueSunZi = my_Cards[3].card_value - next_Cards[3].card_value  
  45.             if SubValueSunZi == 0 then  
  46.                 return lose  
  47.             end  
  48.             if SubValueSunZi > 0 then  
  49.                 return win  
  50.             end  
  51.             if SubValueSunZi < 0 then  
  52.                 return lose  
  53.             end  
  54.         end  
  55.     end  
  56.   
  57.     --------------------------------------------同花----------------------------------  
  58.     if  my_Cards_Type == CardType.TONG_HUA then  
  59.         if my_Cards[3].card_value - next_Cards[3].card_value > 0 then  
  60.             return win   
  61.         end  
  62.   
  63.         if my_Cards[3].card_value - next_Cards[3].card_value < 0 then  
  64.             return lose  
  65.         end  
  66.         if my_Cards[2].card_value - next_Cards[2].card_value > 0 then  
  67.             return win   
  68.         end  
  69.   
  70.         if my_Cards[2].card_value - next_Cards[2].card_value < 0 then  
  71.             return lose  
  72.         end          
  73.         if my_Cards[1].card_value - next_Cards[1].card_value > 0 then  
  74.             return win   
  75.         end  
  76.   
  77.         if my_Cards[1].card_value - next_Cards[1].card_value < 0 then  
  78.             return lose  
  79.         end  
  80.         return lose  
  81.     end  
  82.       
  83.     --------------------------------------------顺子----------------------------------  
  84.     local  IsOrNotA32_mycards  
  85.     local  IsOrNotA32_nextcards  
  86.     local  SubValueSunZi  
  87.     if  my_Cards_Type == CardType.SHUN_ZI then  
  88.   
  89.         IsOrNotA32_mycards = isA32(my_Cards)  
  90.         IsOrNotA32_nextcards = isA32(next_Cards)  
  91.   
  92.         --两个都是A32  
  93.         if IsOrNotA32_mycards  and IsOrNotA32_nextcards then  
  94.             return lose  
  95.         end  
  96.   
  97.         --有一个有A32  
  98.         if IsOrNotA32_mycards  or IsOrNotA32_nextcards then  
  99.             if IsOrNotA32_nextcards then  
  100.                 return win  
  101.             else  
  102.                 return lose  
  103.             end  
  104.         end  
  105.   
  106.         --都没有A32  
  107.         if IsOrNotA32_mycards  == false and IsOrNotA32_nextcards == false then  
  108.             SubValueSunZi = my_Cards[3].card_value - next_Cards[3].card_value  
  109.             if SubValueSunZi == 0 then  
  110.                 return lose  
  111.             end  
  112.             if SubValueSunZi > 0 then  
  113.                 return win  
  114.             end  
  115.             if SubValueSunZi < 0 then  
  116.                 return lose  
  117.             end  
  118.         end  
  119.     end  
  120.   
  121.     --------------------------------------------对子----------------------------------  
  122.      
  123.     if  my_Cards_Type == CardType.DUI_ZI then  
  124.         --第二张牌一定是组成对子的那张牌  
  125.         local SubValueDuiZi = my_Cards[2].card_value - next_Cards[2].card_value  
  126.         --第二张不等  
  127.         if SubValueDuiZi > 0 then  
  128.             return win   
  129.         end  
  130.         if SubValueDuiZi < 0 then  
  131.             return lose  
  132.         end  
  133.         --第二张相等  
  134.         if SubValueDuiZi == 0 then  
  135.             if (my_Cards[1].card_value + my_Cards[3].card_value ) > (next_Cards[1].card_value + next_Cards[3].card_value) then  
  136.                 return win  
  137.             else  
  138.                 return lose  
  139.             end  
  140.         end  
  141.           
  142.     end  
  143.   
  144.     --------------------------------------------单牌----------------------------------  
  145.     if  my_Cards_Type == CardType.UNDEFINE then  
  146.         if my_Cards[3].card_value - next_Cards[3].card_value > 0 then  
  147.             return win   
  148.         end  
  149.   
  150.         if my_Cards[3].card_value - next_Cards[3].card_value < 0 then  
  151.             return lose  
  152.         end  
  153.         if my_Cards[2].card_value - next_Cards[2].card_value > 0 then  
  154.             return win   
  155.         end  
  156.   
  157.         if my_Cards[2].card_value - next_Cards[2].card_value < 0 then  
  158.             return lose  
  159.         end          
  160.         if my_Cards[1].card_value - next_Cards[1].card_value > 0 then  
  161.             return win   
  162.         end  
  163.   
  164.         if my_Cards[1].card_value - next_Cards[1].card_value < 0 then  
  165.             return lose  
  166.         end  
  167.         return lose  
  168.     end  
  169. end  

 

最后用我们的测试函数验证一下:

 

[plain] view plain copy
 
  1. local cardBuffer =cardTool.RandCardList()  
  2.   
  3.   
  4. --cardBuffer[1]=2  
  5. --cardBuffer[2]=3+16  
  6. --cardBuffer[3]=5  
  7.   
  8. --cardBuffer[4]=4+16  
  9. --cardBuffer[5]=4+32  
  10. --cardBuffer[6]=4+48  
  11.   
  12.   
  13. local cards1={}  
  14. local cards2={}  
  15.   
  16. for i=1,6,1 do  
  17.     local cardColor = luabit.band(cardBuffer[i] , 0xF0)  
  18.     local cardValue = luabit.band(cardBuffer[i] , 0x0F)  
  19.   
  20.   
  21.     local cardinfo =  
  22.     {  
  23.         card_value = cardValue;  
  24.         card_Color = cardColor;  
  25.     }  
  26.     if i >3 then  
  27.         cards2[i-3] = cardinfo  
  28.     else  
  29.         cards1[i] = cardinfo  
  30.     end  
  31.   
  32.   
  33. end  
  34.   
  35.   
  36. print_t(cardTool.getCardTypeNamebyType(cardTool.getCardType(cards1)))  
  37. print_t(cardTool.getCardNamebyCards(cards1))  
  38.   
  39. print_t(cardTool.getCardTypeNamebyType(cardTool.getCardType(cards2)))  
  40. print_t(cardTool.getCardNamebyCards(cards2))  
  41.   
  42.   
  43.   
  44. print_t(cardTool.isOvercomePrev(cards1,cards2))  


输出:

 

 

[plain] view plain copy
 
  1. 单牌  
  2.   
  3. 黑桃4梅花6方块Q  
  4.   
  5. 同花  
  6.   
  7. 梅花5梅花9梅花K  
  8.   
  9. false  

 

 

测试235逻辑:

 

[plain] view plain copy
 
  1. cardBuffer[1]=2  
  2. cardBuffer[2]=3+16  
  3. cardBuffer[3]=5  
  4.   
  5. cardBuffer[4]=4+16  
  6. cardBuffer[5]=4+32  
  7. cardBuffer[6]=4+48  


输出:

 

 

[plain] view plain copy
 
  1. 单牌  
  2.   
  3. 黑桃2红桃3黑桃5  
  4.   
  5. 豹子  
  6.   
  7. 红桃4梅花4方块4  
  8.   
  9. true  

 

测试A23逻辑:

 

 

[plain] view plain copy
 
  1. cardBuffer[1]=2  
  2. cardBuffer[2]=3+16  
  3. cardBuffer[3]=14  
  4.   
  5. cardBuffer[4]=4+16  
  6. cardBuffer[5]=5+32  
  7. cardBuffer[6]=6+48  

 

 

输出:

[plain] view plain copy
 
  1. 顺子  
  2.   
  3. 黑桃2红桃3黑桃A  
  4.   
  5. 顺子  
  6.   
  7. 红桃4梅花5方块6  
  8.   
  9. false  


好啦~赢三张系列已经完结

posted on 2018-06-12 22:31  &大飞  阅读(330)  评论(0编辑  收藏  举报

导航