zoukankan      html  css  js  c++  java
  • 斗地主出牌规则

    1. 斗地主游戏参数

      都定义为枚举类型,代码为:

     1 using System;
     2 using System.Collections.Generic;
     3 using UnityEngine;
     4 
     5 public class Consts
     6 {
     7 /// <summary>
     8 /// 角色类型
     9 /// </summary>
    10 public enum CharacterType
    11 {
    12     Library = 0,//牌库
    13     Player = 1,//玩家
    14     ComputerRight = 2,//电脑
    15     ComputerLeft = 3,
    16     Desk//桌子
    17 }
    18 
    19 
    20 /// <summary>
    21 /// 卡牌花色
    22 /// </summary>
    23 public enum Colors
    24 {
    25     None,
    26     Club,//梅花
    27     Heart,//红桃
    28     Spade,//黑桃
    29     Square//方片
    30 }
    31 
    32 
    33 /// <summary>
    34 /// 卡牌的权值
    35 /// </summary>
    36 public enum Weight
    37 {
    38     Three,      // 3-9
    39     Four,
    40     Five,
    41     Six,
    42     Seven,
    43     Eight,
    44     Nine,
    45     Ten,
    46     Jack,       // J
    47     Queen,      // Q
    48     King,       // K
    49     One,        // A
    50     Two,        // 2
    51     SJoker,     // 小王
    52     LJoker      // 大王
    53 }
    54 
    55 
    56 /// <summary>
    57 /// 出牌类型
    58 /// </summary>
    59 public enum CardType
    60 {
    61     None,
    62     Single,//
    63     Double,//对儿
    64     Straight,//顺子
    65     DoubleStraight,//双顺
    66     TripleStraight,//飞机
    67     Three,//三不带
    68     ThreeAndOne,//三带一
    69     ThreeAndTwo,//三代二
    70     Boom,//炸弹
    71     JokerBoom//王炸
    72 }
    73 
    74 
    75 /// <summary>
    76 /// 身份
    77 /// </summary>
    78 public enum Identity
    79 {
    80     Farmer,//农民
    81     Landlord//地主
    82 }

    2. 出牌规则代码实现

      1 using System;
      2 using System.Collections.Generic;
      3 
      4 /// <summary>
      5 /// 游戏规则类
      6 /// 假设传入的参数选择的手牌已排序
      7 /// </summary>
      8 public static class Rulers
      9 {
     10     /// <summary>
     11     /// 是否是单牌
     12     /// 2
     13     /// </summary>
     14     /// <param name="cards">选择的手牌</param>
     15     /// <returns></returns>
     16     public static bool IsSingle(List<Card> cards)
     17     {
     18         return cards.Count == 1;
     19     }
     20 
     21     /// <summary>
     22     /// 判断是否是对儿
     23     /// 22
     24     /// </summary>
     25     /// <param name="cards">选择的手牌</param>
     26     /// <returns></returns>
     27     public static bool IsDouble(List<Card> cards)
     28     {
     29         if (cards.Count == 2)
     30         {
     31             if (cards[0].CardWeight == cards[1].CardWeight)
     32             {
     33                 return true;
     34             }
     35         }
     36 
     37         return false;
     38 
     39     }
     40 
     41     /// <summary>
     42     /// 是否是顺子
     43     /// 如:23456
     44     /// </summary>
     45     /// <param name="cards">选择的手牌</param>
     46     /// <returns></returns>
     47     public static bool IsStraight(List<Card> cards)
     48     {
     49         if (cards.Count < 5 || cards.Count > 12)
     50             return false;
     51 
     52         for (int i = 0; i < cards.Count - 1; i++)
     53         {
     54             Weight tempWeight = cards[i].CardWeight;
     55             if (cards[i + 1].CardWeight - tempWeight != 1)
     56                 return false;
     57             //不能超过A
     58             if (tempWeight > Weight.One || cards[i + 1].CardWeight > Weight.One)
     59                 return false;
     60         }
     61 
     62         return true;
     63     }
     64 
     65     /// <summary>
     66     /// 是否是双顺
     67     /// 如:223344
     68     /// </summary>
     69     /// <param name="cards">选择的手牌</param>
     70     /// <returns></returns>
     71     public static bool IsDoubleStraight(List<Card> cards)
     72     {
     73         // 牌数大于6且成对出现
     74         if (cards.Count < 6 || cards.Count % 2 != 0)
     75             return false;
     76 
     77         for (int i = 0; i < cards.Count - 2; i += 2)
     78         {
     79             if (cards[i].CardWeight != cards[i + 1].CardWeight)
     80                 return false;
     81             if (cards[i + 2].CardWeight - cards[i].CardWeight != 1)
     82                 return false;
     83             //不能超过A
     84             if (cards[i].CardWeight > Weight.One || cards[i + 2].CardWeight > Weight.One)
     85                 return false;
     86         }
     87 
     88         return true;
     89     }
     90 
     91     /// <summary>
     92     /// 是否是飞机
     93     /// 如:222333
     94     /// </summary>
     95     /// <param name="cards">选择的手牌</param>
     96     /// <returns></returns>
     97     public static bool IsTripleStraight(List<Card> cards)
     98     {
     99         if (cards.Count < 6 || cards.Count % 3 != 0)
    100             return false;
    101 
    102         for (int i = 0; i < cards.Count - 3; i += 3)
    103         {
    104             if (cards[i].CardWeight != cards[i + 1].CardWeight)
    105                 return false;
    106             if (cards[i + 2].CardWeight != cards[i + 1].CardWeight)
    107                 return false;
    108             if (cards[i].CardWeight != cards[i + 2].CardWeight)
    109                 return false;
    110 
    111             if (cards[i + 3].CardWeight - cards[i].CardWeight != 1)
    112                 return false;
    113             //不能超过A
    114             if (cards[i].CardWeight > Weight.One || cards[i + 3].CardWeight > Weight.One)
    115                 return false;
    116         }
    117 
    118         return true;
    119     }
    120 
    121     /// <summary>
    122     /// 是否是三不带
    123     /// 如:222
    124     /// </summary>
    125     /// <param name="cards">选择的手牌</param>
    126     /// <returns></returns>
    127     public static bool IsThree(List<Card> cards)
    128     {
    129         if (cards.Count != 3)
    130             return false;
    131         if (cards[0].CardWeight != cards[1].CardWeight)
    132             return false;
    133         if (cards[2].CardWeight != cards[1].CardWeight)
    134             return false;
    135         if (cards[0].CardWeight != cards[2].CardWeight)
    136             return false;
    137 
    138         return true;
    139     }
    140 
    141     /// <summary>
    142     /// 是否是三带一
    143     /// 如:2224
    144     /// </summary>
    145     /// <param name="cards">选择的手牌</param>
    146     /// <returns></returns>
    147     public static bool IsThreeAndOne(List<Card> cards)
    148     {
    149         if (cards.Count != 4)
    150             return false;
    151 
    152         if (cards[0].CardWeight == cards[1].CardWeight && cards[1].CardWeight == cards[2].CardWeight)
    153             return true;
    154         else if (cards[1].CardWeight == cards[2].CardWeight && cards[2].CardWeight == cards[3].CardWeight)
    155             return true;
    156 
    157         return false;
    158     }
    159 
    160     /// <summary>
    161     /// 是否是三带二
    162     /// 如:22233
    163     /// </summary>
    164     /// <param name="cards">选择的手牌</param>
    165     /// <returns></returns>
    166     public static bool IsThreeAndTwo(List<Card> cards)
    167     {
    168         if (cards.Count != 5)
    169             return false;
    170 
    171         if (cards[0].CardWeight == cards[1].CardWeight && cards[1].CardWeight == cards[2].CardWeight)
    172         {
    173             if (cards[3].CardWeight == cards[4].CardWeight)
    174                 return true;
    175         }
    176         else if (cards[2].CardWeight == cards[3].CardWeight && cards[3].CardWeight == cards[4].CardWeight)
    177         {
    178             if (cards[0].CardWeight == cards[1].CardWeight)
    179                 return true;
    180         }
    181 
    182         return false;
    183     }
    184 
    185     /// <summary>
    186     /// 判断是否是炸弹
    187     /// 如:4444
    188     /// </summary>
    189     /// <param name="cards">选择的手牌</param>
    190     /// <returns></returns>
    191     public static bool IsBoom(List<Card> cards)
    192     {
    193         if (cards.Count != 4)
    194             return false;
    195         if (cards[0].CardWeight != cards[1].CardWeight)
    196             return false;
    197         if (cards[1].CardWeight != cards[2].CardWeight)
    198             return false;
    199         if (cards[2].CardWeight != cards[3].CardWeight)
    200             return false;
    201 
    202         return true;
    203     }
    204 
    205     /// <summary>
    206     /// 判断是不是王炸
    207     /// 如:大王小王
    208     /// </summary>
    209     /// <param name="cards">选择的手牌</param>
    210     /// <returns></returns>
    211     public static bool IsJokerBoom(List<Card> cards)
    212     {
    213         if (cards.Count != 2)
    214             return false;
    215 
    216         if (cards[0].CardWeight == Weight.SJoker && cards[1].CardWeight == Weight.LJoker)
    217             return true;
    218         else if (cards[0].CardWeight == Weight.LJoker && cards[1].CardWeight == Weight.SJoker)
    219             return true;
    220 
    221         return false;
    222     }
    223 
    224     /// <summary>
    225     /// 判断能否出牌
    226     /// </summary>
    227     /// <param name="cards">要出的牌</param>
    228     /// <param name="type">出牌类型</param>
    229     /// <returns>能否出牌</returns>
    230     public static bool CanPop(List<Card> cards, out CardType type)
    231     {
    232         type = CardType.None;
    233         bool can = false;
    234 
    235         switch (cards.Count)                        // 牌数
    236         {
    237             case 1:
    238                 if (IsSingle(cards))
    239                 {
    240                     type = CardType.Single;
    241                     can = true;
    242                 }
    243                 break;
    244             case 2:
    245                 if (IsDouble(cards))          
    246                 {
    247                     type = CardType.Double;
    248                     can = true;
    249                 }
    250                 else if (IsJokerBoom(cards))   
    251                 {
    252                     type = CardType.JokerBoom;
    253                     can = true;
    254                 }
    255                 break;
    256             case 3:                             
    257                 if (IsThree(cards))
    258                 {
    259                     type = CardType.Three;
    260                     can = true;
    261                 }
    262                 break;
    263             case 4:                                 // 优先考虑炸弹
    264                 if (IsBoom(cards))
    265                 {
    266                     type = CardType.Boom;
    267                     can = true;
    268                 }
    269                 else if (IsThreeAndOne(cards))
    270                 {
    271                     type = CardType.ThreeAndOne;
    272                     can = true;
    273                 }
    274                 break;
    275             case 5:
    276                 if (IsStraight(cards))
    277                 {
    278                     type = CardType.Straight;
    279                     can = true;
    280                 }
    281                 else if (IsThreeAndTwo(cards))
    282                 {
    283                     type = CardType.ThreeAndTwo;
    284                     can = true;
    285                 }
    286                 break;
    287             case 6:
    288                 if (IsStraight(cards))
    289                 {
    290                     type = CardType.Straight;
    291                     can = true;
    292                 }
    293                 else if (IsDoubleStraight(cards))
    294                 {
    295                     type = CardType.DoubleStraight;
    296                     can = true;
    297                 }
    298                 else if (IsTripleStraight(cards))
    299                 {
    300                     type = CardType.TripleStraight;
    301                     can = true;
    302                 }
    303                 break;
    304             case 7:
    305                 if (IsStraight(cards))
    306                 {
    307                     type = CardType.Straight;
    308                     can = true;
    309                 }
    310                 break;
    311             case 8:
    312                 if (IsStraight(cards))
    313                 {
    314                     type = CardType.Straight;
    315                     can = true;
    316                 }
    317                 else if (IsDoubleStraight(cards))
    318                 {
    319                     type = CardType.DoubleStraight;
    320                     can = true;
    321                 }
    322                 break;
    323             case 9:
    324                 if (IsStraight(cards))
    325                 {
    326                     type = CardType.Straight;
    327                     can = true;
    328                 }//777 888 999 
    329                 else if (IsTripleStraight(cards))
    330                 {
    331                     type = CardType.TripleStraight;
    332                     can = true;
    333                 }
    334                 break;
    335             case 10:
    336                 if (IsStraight(cards))
    337                 {
    338                     type = CardType.Straight;
    339                     can = true;
    340                 }
    341                 else if (IsDoubleStraight(cards))
    342                 {
    343                     type = CardType.DoubleStraight;
    344                     can = true;
    345                 }
    346                 break;
    347             case 11:
    348                 if (IsStraight(cards))
    349                 {
    350                     type = CardType.Straight;
    351                     can = true;
    352                 }
    353                 break;
    354             case 12:
    355                 if (IsStraight(cards))
    356                 {
    357                     type = CardType.Straight;
    358                     can = true;
    359                 }
    360                 else if (IsDoubleStraight(cards))
    361                 {
    362                     type = CardType.DoubleStraight;
    363                     can = true;
    364                 }// 444 555 666 777
    365                 else if (IsTripleStraight(cards))
    366                 {
    367                     type = CardType.TripleStraight;
    368                     can = true;
    369                 }
    370                 break;
    371             case 13:
    372                 break;
    373             case 14:
    374                 if (IsDoubleStraight(cards))
    375                 {
    376                     type = CardType.DoubleStraight;
    377                     can = true;
    378                 }
    379                 break;
    380             case 15:
    381                 if (IsTripleStraight(cards))
    382                 {
    383                     type = CardType.TripleStraight;
    384                     can = true;
    385                 }
    386                 break;
    387             case 16:
    388                 if (IsDoubleStraight(cards))
    389                 {
    390                     type = CardType.DoubleStraight;
    391                     can = true;
    392                 }
    393                 break;
    394             case 17:
    395                 break;
    396             case 18:
    397                 if (IsDoubleStraight(cards))
    398                 {
    399                     type = CardType.DoubleStraight;
    400                     can = true;
    401                 }// 444 555 666 777 888 999 
    402                 else if (IsTripleStraight(cards))
    403                 {
    404                     type = CardType.TripleStraight;
    405                     can = true;
    406                 }
    407                 break;
    408             case 19:
    409                 break;
    410             case 20:
    411                 if (IsDoubleStraight(cards))
    412                 {
    413                     type = CardType.DoubleStraight;
    414                     can = true;
    415                 }
    416                 break;
    417             default:
    418                 break;
    419         }
    420         return can;
    421     }
    422 }
  • 相关阅读:
    5款强大的Java Web开发工具
    [Visual Studio] 重置默认设置 还原默认设置
    [Visual Studio] VS2012调试时很慢的解决方案
    SQL 语句与性能之执行顺序
    SQL 语句与性能之联合查询和联合分类查询
    使用SHFB(Sandcastle Help File Builder)建立MSDN风格的代码文档
    循序渐进地代码重构
    博客收藏
    [已解决]:调用 LoadLibraryEx 失败,在 ISAPI 筛选器 "c:WindowsMicrosoft.NETFrameworkv4.0.30319\aspnet_filter.
    [Visual Studio] .vsix项目模板制作
  • 原文地址:https://www.cnblogs.com/coderJiebao/p/CSharp07.html
Copyright © 2011-2022 走看看