十三水游戏 自动理牌算法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了十三水游戏 自动理牌算法相关的知识,希望对你有一定的参考价值。

  今天上班一大早,人事就发信息叫我填转正单,才发现自己入职已经快满三个月了,期间已经做了两个十三水的棋牌游戏,都已经准备新开第三个游戏了,发现自己还 

未写下一篇博客,实在是对不起自己之前的承诺,不过之前也是太忙了,趁着现在有点时间,把之前十三水游戏中比较核心的自动理牌算法和大家分享下,其中包括扑
克牌的生成,排序,牌型获取等。

  十三水游戏中,有一个自动推荐牌型的功能,如下:

  技术分享

  给玩家发完牌后,会立即出现该界面,给玩家推荐牌型,具体代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace text
 8 {
 9 
10     /// <summary>
11     /// 自动理牌
12     /// </summary>
13     public class CardModel
14     {
15         public List<TypeCard> typeCardList;
16 
17         public CardModel()
18         {
19             typeCardList = new List<TypeCard>();
20         }
21 
22     }
23 
24     /// <summary>
25     /// 牌型
26     /// </summary>
27     public enum DeckTypeEnum : byte
28     {
29         /// <summary>
30         /// 未识别
31         /// </summary>
32         Error = 255,
33         /// <summary>
34         /// 过牌为空
35         /// </summary>
36         None = 0,
37         /// <summary>
38         /// 高牌-散牌-乌龙1
39         /// </summary>
40         Single,
41         /// <summary>
42         /// 对子2
43         /// </summary>
44         Double,
45         /// <summary>
46         /// 两对3
47         /// </summary>
48         TwoDouble,
49         /// <summary>
50         /// 三张4
51         /// </summary>
52         Three,
53         /// <summary>
54         /// 顺子5
55         /// </summary>
56         ShunZi,
57         /// <summary>
58         /// 同花6
59         /// </summary>
60         TongHua,
61         /// <summary>
62         /// 葫芦7
63         /// </summary>
64         Gourd,
65         /// <summary>
66         /// 炸弹8
67         /// </summary>
68         Bomb,
69         /// <summary>
70         /// 同花顺9
71         /// </summary>
72         TongHuaShun,
73         /// <summary>
74         /// 五炸10
75         /// </summary>
76         FiveBomb
77     }
78 
79     public class TypeCard
80     {
81         public DeckTypeEnum cardType;//牌型
82         public List<int> cardList;//手牌
83     }
84 
85 
86 }
   1 using System;
   2 using System.Collections;
   3 using System.Collections.Generic;
   4 using System.Linq;
   5 using System.Text;
   6 using System.Threading.Tasks;
   7 
   8 
   9 namespace text
  10 {
  11     class Program
  12     {
  13 
  14         /// <summary>
  15         /// 牌面从大到小排序
  16         /// </summary>
  17         public static void SortCard(List<int> cards)
  18         {
  19             //牌面从大到小排序
  20             cards.Sort((b, a) =>
  21             {
  22                 int result = (a % 100) - (b % 100);
  23                 if (result == 0)
  24                 {
  25                     result = (a / 100) - (b / 100);
  26                 }
  27                 return result;
  28             });
  29         }
  30 
  31         /// <summary>
  32         /// 牌面从小到大排序
  33         /// </summary>
  34         public static void SortCardMinToMax(List<int> cards)
  35         {
  36             //面大小排序
  37             cards.Sort((a, b) =>
  38             {
  39                 int result = (a % 100) - (b % 100);
  40                 if (result == 0)
  41                 {
  42                     result = (b / 100) - (a / 100);
  43                 }
  44                 return result;
  45             });
  46         }
  47 
  48 
  49         /// <summary>
  50         /// 生成牌信息
  51         /// </summary>
  52         /// <param name="IsCheat">是否带大小王(癞子)</param>
  53         /// <param name="AddColor">加几色玩法</param>
  54         /// <returns></returns>
  55         public static List<int> RuffleCard(bool IsCheat, int AddColor)
  56         {
  57             int[] idArr;
  58 
  59             if (IsCheat)
  60             {
  61                 //带王
  62                 if (AddColor == 1)
  63                 {
  64                     idArr = new int[] {
  65                     102,103,104,105,106,107,108,109,110,111,112,113,114,
  66                     202,203,204,205,206,207,208,209,210,211,212,213,214,
  67                     302,303,304,305,306,307,308,309,310,311,312,313,314,
  68                     402,403,404,405,406,407,408,409,410,411,412,413,414,
  69                     404,405,406,407,408,409,410,411,412,413,414,
  70                     616,
  71                     717
  72                     };
  73                 }
  74                 else if (AddColor == 2)
  75                 {
  76                     idArr = new int[] {
  77                     102,103,104,105,106,107,108,109,110,111,112,113,114,
  78                     202,203,204,205,206,207,208,209,210,211,212,213,214,
  79                     302,303,304,305,306,307,308,309,310,311,312,313,314,
  80                     302,303,304,305,306,307,308,309,310,311,312,313,314,
  81                     402,403,404,405,406,407,408,409,410,411,412,413,414,
  82                     404,405,406,407,408,409,410,411,412,413,414,
  83                     616,
  84                     717
  85                     };
  86                 }
  87                 else
  88                 {
  89                     idArr = new int[] {
  90                     102,103,104,105,106,107,108,109,110,111,112,113,114,
  91                     202,203,204,205,206,207,208,209,210,211,212,213,214,
  92                     302,303,304,305,306,307,308,309,310,311,312,313,314,
  93                     404,405,406,407,408,409,410,411,412,413,414,
  94                     616,
  95                     717
  96                     };
  97                 }
  98 
  99             }
 100             else
 101             {
 102                 if (AddColor == 1)
 103                 {
 104                     idArr = new int[] {
 105                     102,103,104,105,106,107,108,109,110,111,112,113,114,
 106                     202,203,204,205,206,207,208,209,210,211,212,213,214,
 107                     302,303,304,305,306,307,308,309,310,311,312,313,314,
 108                     402,403,404,405,406,407,408,409,410,411,412,413,414,
 109                     402,403,404,405,406,407,408,409,410,411,412,413,414
 110                     };
 111                 }
 112                 else if (AddColor == 2)
 113                 {
 114                     idArr = new int[] {
 115                     102,103,104,105,106,107,108,109,110,111,112,113,114,
 116                     202,203,204,205,206,207,208,209,210,211,212,213,214,
 117                     302,303,304,305,306,307,308,309,310,311,312,313,314,
 118                     302,303,304,305,306,307,308,309,310,311,312,313,314,
 119                     402,403,404,405,406,407,408,409,410,411,412,413,414,
 120                     402,403,404,405,406,407,408,409,410,411,412,413,414
 121                     };
 122                 }
 123                 else
 124                 {
 125                     idArr = new int[] {
 126                     102,103,104,105,106,107,108,109,110,111,112,113,114,
 127                     202,203,204,205,206,207,208,209,210,211,212,213,214,
 128                     302,303,304,305,306,307,308,309,310,311,312,313,314,
 129                     402,403,404,405,406,407,408,409,410,411,412,413,414
 130                     };
 131                 }
 132 
 133             }
 134 
 135             List<int> resCardIds = new List<int>();
 136             List<int> cardIds = idArr.ToList<int>();
 137             Random rand = new Random();
 138             while (cardIds.Count > 0)
 139             {
 140                 int randNum = rand.Next(cardIds.Count);
 141                 resCardIds.Add(cardIds[randNum]);
 142                 cardIds.RemoveAt(randNum);
 143             }
 144 
 145             return resCardIds;
 146         }
 147 
 148         /// <summary>
 149         /// 获取一个玩家的手牌 13张
 150         /// </summary>
 151         /// <returns></returns>
 152         public static List<int> GetOnePlayerCard()
 153         {
 154             List<int> CardList = RuffleCard(true, 0);
 155 
 156             return CardList.GetRange(0, 13);
 157         }
 158 
 159         /// <summary>
 160         /// 计算一副牌里面的所有可能牌型
 161         /// </summary>
 162         /// <param name="cardList"></param>
 163         /// <returns></returns>
 164         public static List<TypeCard> GetMaxCardType(List<int> cardList)
 165         {
 166             List<TypeCard> typeCardList = new List<TypeCard>();
 167 
 168             //从大到小排序
 169             SortCard(cardList);
 170 
 171             //复制一份从小到大的牌
 172             List<int> cardList2 = cardList.GetRange(0, cardList.Count);
 173             SortCardMinToMax(cardList2);
 174 
 175             //鬼牌数量
 176             int smallKingNum = 616;//小王
 177             int bigKingNum = 717;//大王
 178             int KingNum = 0;
 179             KingNum += cardList.Contains(smallKingNum) ? 1 : 0;
 180             KingNum += cardList.Contains(bigKingNum) ? 1 : 0;
 181             int king = cardList.Contains(smallKingNum) ? smallKingNum : bigKingNum;//单王
 182 
 183             List<int> FiveNum = cardList.GroupBy(p => p % 100).Where(p => p.Count() >= 5).Select(p => p.Key).ToList();
 184             List<int> FourNum = cardList.GroupBy(p => p % 100).Where(p => p.Count() >= 4).Select(p => p.Key).ToList();
 185             List<int> ThreeNum = cardList.GroupBy(p => p % 100).Where(p => p.Count() >= 3).Select(p => p.Key).ToList();
 186             List<int> TwoNum = cardList.GroupBy(p => p % 100).Where(p => p.Count() >= 2).Select(p => p.Key).ToList();
 187 
 188             #region 五炸
 189             foreach (var Fiveitem in FiveNum)
 190             {
 191                 List<int> tempList = new List<int>();
 192                 foreach (var item in cardList)
 193                 {
 194                     if (item % 100 == Fiveitem)
 195                     {
 196                         tempList.Add(item);
 197                     }
 198 
 199                     if (tempList.Count >= 5)
 200                     {
 201                         TypeCard typeCard = new TypeCard() { cardList = tempList, cardType = DeckTypeEnum.FiveBomb };
 202                         typeCardList.Add(typeCard);
 203                         break;
 204                     }
 205                 }
 206 
 207             }
 208 
 209             if (KingNum >= 1)
 210             {
 211                 foreach (var FourItem in FourNum)
 212                 {
 213                     List<int> tempList = new List<int>();
 214                     foreach (var item in cardList)
 215                     {
 216                         if (item % 100 == FourItem)
 217                         {
 218                             tempList.Add(item);
 219                         }
 220 
 221                         if (tempList.Count >= 4)
 222                         {
 223                             tempList.Add(king);
 224                             TypeCard typeCard = new TypeCard() { cardList = tempList, cardType = DeckTypeEnum.FiveBomb };
 225                             typeCardList.Add(typeCard);
 226                             break;
 227                         }
 228                     }
 229 
 230                 }
 231             }
 232 
 233             if (KingNum == 2)
 234             {
 235                 foreach (var ThreeItem in ThreeNum)
 236                 {
 237                     List<int> tempList = new List<int>();
 238                     foreach (var item in cardList)
 239                     {
 240                         if (item % 100 == ThreeItem)
 241                         {
 242                             tempList.Add(item);
 243                         }
 244 
 245                         if (tempList.Count >= 3)
 246                         {
 247                             tempList.Add(smallKingNum);
 248                             tempList.Add(bigKingNum);
 249 
 250                             TypeCard typeCard = new TypeCard() { cardList = tempList, cardType = DeckTypeEnum.FiveBomb };
 251                             typeCardList.Add(typeCard);
 252                             break;
 253                         }
 254                     }
 255 
 256                 }
 257             }
 258             #endregion
 259 
 260             #region 同花顺
 261             foreach (var item in cardList2)
 262             {
 263                 if (item % 100 >= 11)
 264                 {
 265                     break;
 266                 }
 267 
 268                 List<int> tempList = new List<int>();
 269                 tempList.Add(item);
 270 
 271                 foreach (var item2 in cardList2)
 272                 {
 273                     if (item2 - 1 == tempList.Last())
 274                     {
 275                         tempList.Add(item2);
 276                     }
 277 
 278                     if (tempList.Count >= 5)
 279                     {
 280                         TypeCard typeCard = new TypeCard() { cardList = tempList, cardType = DeckTypeEnum.TongHuaShun };
 281                         typeCardList.Add(typeCard);
 282                         break;
 283                     }
 284                 }
 285             }
 286 
 287             if (KingNum >= 1)
 288             {
 289                 foreach (var item in cardList2)
 290                 {
 291                     if (item % 100 >= 12)
 292                     {
 293                         break;
 294                     }
 295 
 296                     List<int> tempList = new List<int>();
 297                     tempList.Add(item);
 298 
 299                     foreach (var item2 in cardList2)
 300                     {
 301                         if (item2 - 1 == tempList.Last())
 302                         {
 303                             tempList.Add(item2);
 304                         }
 305 
 306                         if (tempList.Count >= 4)
 307                         {
 308                             tempList.Add(king);
 309                             TypeCard typeCard = new TypeCard() { cardList = tempList, cardType = DeckTypeEnum.TongHuaShun };
 310                             typeCardList.Add(typeCard);
 311                             break;
 312                         }
 313                     }
 314                 }
 315             }
 316 
 317             if (KingNum == 2)
 318             {
 319                 foreach (var item in cardList2)
 320                 {
 321                     if (item % 100 >= 13)
 322                     {
 323                         break;
 324                     }
 325 
 326                     List<int> tempList = new List<int>();
 327                     tempList.Add(item);
 328 
 329                     foreach (var item2 in cardList2)
 330                     {
 331                         if (item2 - 1 == tempList.Last())
 332                         {
 333                             tempList.Add(item2);
 334                         }
 335 
 336                         if (tempList.Count >= 3)
 337                         {
 338                             tempList.Add(smallKingNum);
 339                             tempList.Add(bigKingNum);
 340 
 341                             TypeCard typeCard = new TypeCard() { cardList = tempList, cardType = DeckTypeEnum.TongHuaShun };
 342                             typeCardList.Add(typeCard);
 343                             break;
 344                         }
 345                     }
 346                 }
 347             }
 348             #endregion
 349 
 350             #region 炸弹
 351             foreach (var FourItem in FourNum)
 352             {
 353                 List<int> tempList = new List<int>();
 354                 foreach (var item in cardList)
 355                 {
 356                     if (item % 100 == FourItem)
 357                     {
 358                         tempList.Add(item);
 359                     }
 360 
 361                     if (tempList.Count >= 4)
 362                     {
 363                         TypeCard typeCard = new TypeCard() { cardList = tempList, cardType = DeckTypeEnum.Bomb };
 364                         typeCardList.Add(typeCard);
 365                         break;
 366                     }
 367                 }
 368 
 369             }
 370 
 371             if (KingNum >= 1)
 372             {
 373                 foreach (var ThreeItem in ThreeNum)
 374                 {
 375                     List<int> tempList = new List<int>();
 376                     foreach (var item in cardList)
 377                     {
 378                         if (item % 100 == ThreeItem)
 379                         {
 380                             tempList.Add(item);
 381                         }
 382 
 383                         if (tempList.Count >= 3)
 384                         {
 385 
 386                             tempList.Add(king);
 387                             TypeCard typeCard = new TypeCard() { cardList = tempList, cardType = DeckTypeEnum.Bomb };
 388                             typeCardList.Add(typeCard);
 389                             break;
 390                         }
 391                     }
 392 
 393                 }
 394             }
 395 
 396             if (KingNum == 2)
 397             {
 398                 foreach (var TwoeeItem in TwoNum)
 399                 {
 400                     List<int> tempList = new List<int>();
 401                     foreach (var item in cardList)
 402                     {
 403                         if (item % 100 == TwoeeItem)
 404                         {
 405                             tempList.Add(item);
 406                         }
 407 
 408                         if (tempList.Count >= 2)
 409                         {
 410                             tempList.Add(smallKingNum);
 411                             tempList.Add(bigKingNum);
 412 
 413                             TypeCard typeCard = new TypeCard() { cardList = tempList, cardType = DeckTypeEnum.Bomb };
 414                             typeCardList.Add(typeCard);
 415                             break;
 416                         }
 417                     }
 418 
 419                 }
 420             }
 421             #endregion
 422 
 423             #region 葫芦
 424             foreach (var ThreeItem in ThreeNum)
 425             {
 426                 List<int> tempList = new List<int>();
 427                 foreach (var item in cardList)
 428                 {
 429                     if (item % 100 == ThreeItem)
 430                     {
 431                         tempList.Add(item);
 432                     }
 433 
 434                     if (tempList.Count >= 3)
 435                     {
 436                         //找两对
 437                         foreach (var TwoItem in TwoNum)
 438                         {
 439                             if (ThreeItem != TwoItem)
 440                             {
 441                                 foreach (var AllItem in cardList)
 442                                 {
 443                                     if (AllItem % 100 != ThreeItem && AllItem % 100 == TwoItem)
 444                                     {
 445                                         tempList.Add(AllItem);
 446                                     }
 447 
 448                                     if (tempList.Count >= 5)
 449                                     {
 450                                         TypeCard typeCard = new TypeCard() { cardList = tempList, cardType = DeckTypeEnum.Gourd };
 451                                         typeCardList.Add(typeCard);
 452                                         tempList = typeCard.cardList.GetRange(0, 3);
 453                                         break;
 454                                     }
 455                                 }
 456 
 457 
 458                             }
 459 
 460                         }
 461                         break;
 462                     }
 463                 }
 464             }
 465 
 512             if (KingNum >= 1)
 513             {
 514                 foreach (var ThreeItem in TwoNum)
 515                 {
 516                     List<int> tempList = new List<int>();
 517                     foreach (var item in cardList)
 518                     {
 519                         if (item % 100 == ThreeItem)
 520                         {
 521                             tempList.Add(item);
 522                         }
 523 
 524                         if (tempList.Count >= 2)
 525                         {
 526                             //找两对
 527                             foreach (var TwoItem in TwoNum)
 528                             {
 529                                 if (ThreeItem != TwoItem)
 530                                 {
 531                                     foreach (var AllItem in cardList)
 532                                     {
 533                                         if (AllItem % 100 != ThreeItem && AllItem % 100 == TwoItem)
 534                                         {
 535                                             tempList.Add(AllItem);
 536                                         }
 537 
 538                                         if (tempList.Count >= 4)
 539                                         {
 540                                             tempList.Add(king);
 541                                             TypeCard typeCard = new TypeCard() { cardList = tempList, cardType = DeckTypeEnum.Gourd };
 542                                             typeCardList.Add(typeCard);
 543                                             tempList = typeCard.cardList.GetRange(0, 2);
 544                                             break;
 545                                         }
 546                                     }
 547 
 548 
 549                                 }
 550 
 551                             }
 552                             break;
 553                         }
 554                     }
 555                 }
 556             }
 557 
 558             if (KingNum == 2)
 559             {
 560                 //不用算
 561             }
 562 
 563             #endregion
 564 
 565             #region 同花
 566             List<int> FiveColor = cardList.GroupBy(p => p / 100).Where(p => p.Count() >= 5).Select(p => p.Key).ToList();
 567             List<int> FourColor = cardList.GroupBy(p => p / 100).Where(p => p.Count() >= 4).Select(p => p.Key).ToList();
 568             List<int> ThreeColor = cardList.GroupBy(p => p / 100).Where(p => p.Count() >= 3).Select(p => p.Key).ToList();
 569 
 570             foreach (var Fiveitem in FiveColor)
 571             {
 572                 List<int> tempList = new List<int>();
 573                 foreach (var item in cardList)
 574                 {
 575                     if (item / 100 == Fiveitem)
 576                     {
 577                         tempList.Add(item);
 578                     }
 579                 }
 580 
 581                 while (true)
 582                 {
 583                     if (tempList.Count >= 5)
 584                     {
 585                         TypeCard typeCard = new TypeCard() { cardList = tempList.GetRange(0, 5), cardType = DeckTypeEnum.TongHua };
 586                         typeCardList.Add(typeCard);
 587                         tempList.RemoveAt(0);
 588                     }
 589                     else
 590                     {
 591                         break;
 592                     }
 593                 }
 594             }
 595 
 596             if (KingNum >= 1)
 597             {
 598                 foreach (var Fouritem in FourColor)
 599                 {
 600                     List<int> tempList = new List<int>();
 601                     foreach (var item in cardList)
 602                     {
 603                         if (item / 100 == Fouritem)
 604                         {
 605                             tempList.Add(item);
 606                         }
 607                     }
 608 
 609                     while (true)
 610                     {
 611                         if (tempList.Count >= 4)
 612                         {
 613                             TypeCard typeCard = new TypeCard() { cardList = tempList.GetRange(0, 4), cardType = DeckTypeEnum.TongHua };
 614                             typeCard.cardList.Add(king);
 615                             typeCardList.Add(typeCard);
 616                             tempList.RemoveAt(0);
 617                         }
 618                         else
 619                         {
 620                             break;
 621                         }
 622                     }
 623                 }
 624             }
 625 
 626             if (KingNum == 2)
 627             {
 628                 foreach (var Threeitem in ThreeColor)
 629                 {
 630                     List<int> tempList = new List<int>();
 631                     foreach (var item in cardList)
 632                     {
 633                         if (item / 100 == Threeitem)
 634                         {
 635                             tempList.Add(item);
 636                         }
 637                     }
 638 
 639                     while (true)
 640                     {
 641                         if (tempList.Count >= 3)
 642                         {
 643                             TypeCard typeCard = new TypeCard() { cardList = tempList.GetRange(0, 3), cardType = DeckTypeEnum.TongHua };
 644                             typeCard.cardList.Add(smallKingNum);
 645                             typeCard.cardList.Add(bigKingNum);
 646                             typeCardList.Add(typeCard);
 647                             tempList.RemoveAt(0);
 648                         }
 649                         else
 650                         {
 651                             break;
 652                         }
 653                     }
 654                 }
 655             }
 656 
 657             #endregion
 658 
 659             #region 顺子
 660 
 661             foreach (var MaxItem in cardList)
 662             {
 663 
 664                 if (MaxItem % 100 <= 5)
 665                 {
 666                     break;//从大到小,后面的也不需要循环了
 667                 }
 668 
 669                 List<int> tempList = new List<int>();
 670                 tempList.Add(MaxItem);
 671 
 672                 while (tempList.Count < 5)
 673                 {
 674 
 675                     bool flag = false;
 676 
 677                     foreach (var item in cardList)
 678                     {
 679                         if (item % 100 + 1 == tempList.Last() % 100)
 680                         {
 681                             tempList.Add(item);
 682                             flag = true;
 683                             break;
 684                         }
 685                     }
 686                     if (!flag)
 687                     {
 688                         break;
 689                     }
 690                 }
 691 
 692                 if (tempList.Count >= 5)
 693                 {
 694                     TypeCard typeCard = new TypeCard() { cardList = tempList, cardType = DeckTypeEnum.ShunZi };
 695                     typeCardList.Add(typeCard);
 696                 }
 697             }
 698 
 699             if (KingNum >= 1)
 700             {
 701                 foreach (var MaxItem in cardList)
 702                 {
 703 
 704                     if (MaxItem % 100 <= 4)
 705                     {
 706                         break;//从大到小,后面的也不需要循环了
 707                     }
 708 
 709                     List<int> tempList = new List<int>();
 710                     tempList.Add(MaxItem);
 711 
 712                     while (tempList.Count < 4)
 713                     {
 714 
 715                         bool flag = false;
 716 
 717                         foreach (var item in cardList)
 718                         {
 719                             if (item % 100 + 1 == tempList.Last() % 100)
 720                             {
 721                                 tempList.Add(item);
 722                                 flag = true;
 723                                 break;
 724                             }
 725                         }
 726                         if (!flag)
 727                         {
 728                             break;
 729                         }
 730                     }
 731 
 732                     tempList.Add(king);
 733                     if (tempList.Count >= 5)
 734                     {
 735                         TypeCard typeCard = new TypeCard() { cardList = tempList, cardType = DeckTypeEnum.ShunZi };
 736                         typeCardList.Add(typeCard);
 737                     }
 738 
 739 
 740                 }
 741             }
 742 
 743             if (KingNum == 2)
 744             {
 745                 foreach (var MaxItem in cardList)
 746                 {
 747 
 748                     if (MaxItem % 100 <= 3)
 749                     {
 750                         break;//从大到小,后面的也不需要循环了
 751                     }
 752 
 753                     List<int> tempList = new List<int>();
 754                     tempList.Add(MaxItem);
 755 
 756                     while (tempList.Count < 3)
 757                     {
 758 
 759                         bool flag = false;
 760 
 761                         foreach (var item in cardList)
 762                         {
 763                             if (item % 100 + 1 == tempList.Last() % 100)
 764                             {
 765                                 tempList.Add(item);
 766                                 flag = true;
 767                                 break;
 768                             }
 769                         }
 770                         if (!flag)
 771                         {
 772                             break;
 773                         }
 774                     }
 775 
 776                     tempList.Add(smallKingNum);
 777                     tempList.Add(bigKingNum);
 778                     if (tempList.Count >= 5)
 779                     {
 780                         TypeCard typeCard = new TypeCard() { cardList = tempList, cardType = DeckTypeEnum.ShunZi };
 781                         typeCardList.Add(typeCard);
 782                     }
 783 
 784 
 785                 }
 786             }
 787 
 788             #endregion
 789 
 790             #region 三张
 791             foreach (var Threeitem in ThreeNum)
 792             {
 793                 List<int> tempList = new List<int>();
 794                 foreach (var item in cardList)
 795                 {
 796                     if (item % 100 == Threeitem)
 797                     {
 798                         tempList.Add(item);
 799                     }
 800 
 801                     if (tempList.Count >= 3)
 802                     {
 803                         TypeCard typeCard = new TypeCard() { cardList = tempList, cardType = DeckTypeEnum.Three };
 804                         typeCardList.Add(typeCard);
 805                         break;
 806                     }
 807                 }
 808             }
 809 
 810             if (KingNum >= 1)
 811             {
 812                 foreach (var Twoitem in TwoNum)
 813                 {
 814                     List<int> tempList = new List<int>();
 815                     foreach (var item in cardList)
 816                     {
 817                         if (item % 100 == Twoitem)
 818                         {
 819                             tempList.Add(item);
 820                         }
 821 
 822                         if (tempList.Count >= 2)
 823                         {
 824                             tempList.Add(king);
 825                             TypeCard typeCard = new TypeCard() { cardList = tempList, cardType = DeckTypeEnum.Three };
 826                             typeCardList.Add(typeCard);
 827                             break;
 828                         }
 829                     }
 830                 }
 831             }
 832 
 833             if (KingNum == 2)
 834             {
 835                 foreach (var item in cardList)
 836                 {
 837                     List<int> tempList = new List<int>();
 838                     if (item != smallKingNum || item != bigKingNum)
 839                     {
 840                         tempList.Add(smallKingNum);
 841                         tempList.Add(bigKingNum);
 842                         TypeCard typeCard = new TypeCard() { cardList = tempList, cardType = DeckTypeEnum.Three };
 843                         typeCardList.Add(typeCard);
 844                     }
 845                 }
 846             }
 847 
 848             #endregion
 849 
 850             #region 两对
 851 
 852             for (int i = 0; i < TwoNum.Count; i++)
 853             {
 854                 List<int> tempList = new List<int>();
 855 
 856                 foreach (var item in cardList)
 857                 {
 858                     if (item % 100 == TwoNum[i])
 859                     {
 860                         tempList.Add(item);
 861                     }
 862                 }
 863 
 864                 while (tempList.Count >= 2)
 865                 {
 866 
 867                     for (int j = i + 1; j < TwoNum.Count; j++)
 868                     {
 869                         List<int> tempList2 = new List<int>();
 870                         foreach (var item in cardList)
 871                         {
 872                             if (item % 100 == TwoNum[j])
 873                             {
 874                                 tempList2.Add(item);
 875                             }
 876                         }
 877 
 878                         while ((tempList2.Count >= 2))
 879                         {
 880                             TypeCard typeCard = new TypeCard() { cardList = tempList.GetRange(0, 2), cardType = DeckTypeEnum.TwoDouble };
 881                             typeCard.cardList.AddRange(tempList2.GetRange(0, 2));
 882                             typeCardList.Add(typeCard);
 883                             tempList2.RemoveAt(0);
 884                         }
 885 
 886                     }
 887 
 888                     tempList.RemoveAt(0);
 889 
 890                 }
 891 
 892             }
 893 
 894             if (KingNum >= 1)
 895             {
 896                 //不需要
 897             }
 898 
 899             if (KingNum == 2)
 900             {
 901                 //不需要
 902             }
 903 
 904             #endregion
 905 
 906             #region 对子
 907 
 908             foreach (var Twoitem in TwoNum)
 909             {
 910                 List<int> tempList = new List<int>();
 911 
 912                 foreach (var item in cardList)
 913                 {
 914                     if (item % 100 == Twoitem)
 915                     {
 916                         tempList.Add(item);
 917                     }
 918                 }
 919 
 920                 while (tempList.Count >= 2)
 921                 {
 922                     TypeCard typeCard = new TypeCard() { cardList = tempList.GetRange(0, 2), cardType = DeckTypeEnum.Double };
 923                     typeCardList.Add(typeCard);
 924                     tempList.RemoveAt(0);
 925                 }
 926             }
 927 
 928 
 929             if (KingNum >= 1)
 930             {
 931                 foreach (var item in cardList)
 932                 {
 933                     List<int> tempList = new List<int>();
 934                     if (item != smallKingNum || item != bigKingNum)
 935                     {
 936                         tempList.Add(king);
 937                         TypeCard typeCard = new TypeCard() { cardList = tempList, cardType = DeckTypeEnum.Double };
 938                         typeCardList.Add(typeCard);
 939                     }
 940                 }
 941             }
 942 
 943             if (KingNum == 2)
 944             {
 945                 //不用算
 946             }
 947 
 948             #endregion
 949 
 950 
 951             //乌龙-只拿三张牌
 952             if (true)
 953             {
 954                 if (cardList.Count < 3)
 955                 {
 956                     Console.WriteLine("*****************************************************************最后剩牌不足三张");
 957                 }
 958                 else
 959                 {
 960                     List<int> tempList = new List<int>();
 961                     tempList = cardList.GetRange(0, 3);
 962                     TypeCard typeCard = new TypeCard() { cardList = tempList, cardType = DeckTypeEnum.Single };
 963                     typeCardList.Add(typeCard);
 964                 }
 965 
 966 
 967             }
 968 
 969             return typeCardList;
 970         }
 971 
 972 
 973         /// <summary>
 974         /// 删除元素
 975         /// </summary>
 976         /// <param name="list"></param>
 977         /// <param name="element"></param>
 978         public static void DeleteListElement(List<int> list, List<int> element)
 979         {
 980             foreach (var item in element)
 981             {
 982                 list.Remove(item);
 983             }
 984         }
 985 
 986 
 987         /// <summary>
 988         /// 得到所有自动牌型
 989         /// </summary>
 990         /// <param name="cmlist"></param>
 991         /// <param name="CardList"></param>
 992         /// <returns></returns>
 993         public static List<CardModel> GetAllResult(List<CardModel> cmlist, List<int> CardList)
 994         {
 995             CardModel cm = new CardModel();
 996             List<TypeCard> typeCardList = new List<TypeCard>();
 997 
 998             //新建一个副本
 999             List<int> newCardlist = CardList.Select(p => p).ToList();
1000 
1001             //第一个
1002             if (cmlist.Count == 0)
1003             {
1004                 while (true)
1005                 {
1006 
1007                     typeCardList = GetMaxCardType(newCardlist);
1008 
1009                     //首道只能有三张牌
1010                     if (cm.typeCardList.Count == 2 && typeCardList[0].cardList.Count > 3)
1011                     {
1012                         typeCardList = GetMaxCardType(typeCardList[0].cardList.GetRange(0, 3));
1013                     }
1014 
1015                     cm.typeCardList.Add(typeCardList[0]);
1016                     DeleteListElement(newCardlist, typeCardList[0].cardList);
1017 
1018                     //补全少的牌
1019                     if (cm.typeCardList.Count >= 3)
1020                     {
1021                         for (int i = 0; i < cm.typeCardList.Count; i++)
1022                         {
1023                             if (i == 2)
1024                             {
1025                                 while (cm.typeCardList[i].cardList.Count < 3)
1026                                 {
1027                                     cm.typeCardList[i].cardList.Add(newCardlist[0]);
1028                                     newCardlist.RemoveAt(0);
1029                                 }
1030                             }
1031                             else
1032                             {
1033                                 while (cm.typeCardList[i].cardList.Count < 5)
1034                                 {
1035                                     cm.typeCardList[i].cardList.Add(newCardlist[0]);
1036                                     newCardlist.RemoveAt(0);
1037                                 }
1038                             }
1039 
1040                         }
1041 
1042                         break;
1043                     }
1044                 }
1045 
1046                 cmlist.Add(cm);
1047 
1048                 GetAllResult(cmlist, CardList);
1049             }
1050 
1051 
1052             //其他自动
1053             if (cmlist.Count < 4)
1054             {
1055                 int SingleCount = 0;//乌龙次数
1056                 while (true)
1057                 {
1058                     typeCardList = GetMaxCardType(newCardlist);
1059                     int index = cmlist.Count;//第几组自动
1060                     int typeCardListIndex = 0;
1061 
1062                     if (cm.typeCardList.Count == 3)
1063                     {
1064 
1065                     }
1066                     else if (cm.typeCardList.Count == 0)
1067                     {
1068                         if (typeCardList.Count < index)
1069                         {
1070                             return cmlist;
1071                         }
1072 
1073                         cm.typeCardList.Add(typeCardList[index]);
1074                         DeleteListElement(newCardlist, typeCardList[index].cardList);
1075                     }
1076                     else
1077                     {
1078                         //前道的牌不能大于后道
1079                         while (true)
1080                         {
1081                             if (typeCardList[typeCardListIndex].cardType > cm.typeCardList.Last().cardType)
1082                             {
1083                                 typeCardListIndex++;
1084                                 continue;
1085                             }
1086                             if (typeCardList[typeCardListIndex].cardType == cm.typeCardList.Last().cardType)
1087                             {
1088                                 if (typeCardList[typeCardListIndex].cardList.Max() >= cm.typeCardList.Last().cardList.Max())
1089                                 {
1090                                     return cmlist;
1091                                 }
1092                             }
1093 
1094                             break;
1095                         }
1096 
1097                         //首道只能有三张牌
1098                         if (cm.typeCardList.Count == 2 && typeCardList[typeCardListIndex].cardList.Count > 3)
1099                         {
1100                             typeCardList = GetMaxCardType(typeCardList[typeCardListIndex].cardList.GetRange(0, 3));
1101                             cm.typeCardList.Add(typeCardList[0]);
1102                             DeleteListElement(newCardlist, typeCardList[0].cardList);
1103                         }
1104                         else
1105                         {
1106                             cm.typeCardList.Add(typeCardList[typeCardListIndex]);
1107                             DeleteListElement(newCardlist, typeCardList[typeCardListIndex].cardList);
1108                         }
1109 
1110                     }
1111 
1112                     //补全不够牌的
1113                     if (cm.typeCardList.Count >= 3)
1114                     {
1115                         for (int i = 0; i < cm.typeCardList.Count; i++)
1116                         {
1117                             if (i == 2)
1118                             {
1119                                 while (cm.typeCardList[i].cardList.Count < 3)
1120                                 {
1121                                     cm.typeCardList[i].cardList.Add(newCardlist[0]);
1122                                     newCardlist.RemoveAt(0);
1123                                 }
1124                             }
1125                             else
1126                             {
1127                                 while (cm.typeCardList[i].cardList.Count < 5)
1128                                 {
1129                                     cm.typeCardList[i].cardList.Add(newCardlist[0]);
1130                                     newCardlist.RemoveAt(0);
1131                                 }
1132                             }
1133 
1134                             if (cm.typeCardList[i].cardType == DeckTypeEnum.Single)
1135                             {
1136                                 SingleCount++;
1137                             }
1138 
1139                         }
1140 
1141                         break;
1142                     }
1143                 }
1144 
1145                 cmlist.Add(cm);
1146 
1147                 if (SingleCount >= 2)
1148                 {
1149                     return cmlist;
1150                 }
1151 
1152                 GetAllResult(cmlist, CardList);
1153 
1154             }
1155 
1156             return cmlist;
1157         }
1158 
1159 
1160 
1161         static void Main(string[] args)
1162         {
1163 
1164             List<CardModel> cmlist = new List<CardModel>();
1165             List<int> CardList = GetOnePlayerCard();//13张手牌
1166             //List<int> CardList = new List<int>() { 102,203,105,104,306,211,312,413,208,204,311,106,313 };//13张手牌
1167             cmlist = GetAllResult(cmlist, CardList);
1168 
1169             Console.WriteLine("玩家手牌信息为:");
1170             SortCardMinToMax(CardList);
1171             foreach (var item in CardList)
1172             {
1173                 Console.WriteLine(item);
1174             }
1175 
1176             Console.WriteLine("自动计算的牌型信息:");
1177             Console.WriteLine("");
1178 
1179             foreach (var item in cmlist)
1180             {
1181                 Console.WriteLine("按顺序输出:");
1182                 foreach (var item2 in item.typeCardList)
1183                 {
1184                     Console.WriteLine(item2.cardType);
1185                     Console.WriteLine(string.Join(",", item2.cardList));
1186                 }
1187                 Console.WriteLine("");
1188             }
1189         }
1190 
1191 
1192     }
1193 
1194 }

 

最终输出结果如下:

技术分享

 

  思路是:找到手牌中,所有的牌型,之后将最大的拿出来,然后剩下的牌再去寻找最大的牌型,直到三道都有牌型。第二个推荐牌型,拿取所有牌型中第二大的,剩下的牌型再依次拿取,以此类推。

  代码写得有点长,自己对这个算法也不算满意,感觉推荐的牌型还是不够完美,不过也不打算再优化了,下次碰上再重新写吧。


以上是关于十三水游戏 自动理牌算法的主要内容,如果未能解决你的问题,请参考以下文章

H5十三水棋牌架设

十三水,十三张,斗地主,牛牛的牌型搜索思考

房卡麻将源码下载十三水房卡麻将源码搭建教程

福建十三水房卡源码下载搭建教程

从片段调用 Google Play 游戏服务

php算法----直接插入排序