public static void sort(PokerGroup Pokers) //从大到小排序算法 { int i; int j; for (i = 0; i < Pokers.Count; i++) { for (j = i + 1; j < Pokers.Count; j++) { if (Pokers[j] > Pokers[i]) { Poker temp = Pokers[i]; Pokers[i] = Pokers[j]; Pokers[j] = temp; } } } }
/// <summary> /// 对飞机和飞机带翅膀进行排序,把飞机放在前面,翅膀放在后面. /// </summary> /// <param name="PG">牌组</param> /// <returns>是否为连续三张牌</returns> public static PokerGroup SameThreeSort(PokerGroup PG) { Poker FourPoker = null; //如果把4张当三张出并且带4张的另外一张,就需要特殊处理,这里记录出现这种情况的牌的点数. bool FindedThree = false; //已找到三张相同的牌 PokerGroup tempPokerGroup = new PokerGroup(); //记录三张相同的牌 int count = 0; //记录在连续三张牌前面的翅膀的张数 int Four = 0; // 记录是否连续出现三三相同,如果出现这种情况则表明出现把4张牌(炸弹)当中的三张和其他牌配成飞机带翅膀,并且翅膀中有炸弹牌的点数. // 比如有如下牌组: 998887777666 玩家要出的牌实际上应该为 888777666带997,但是经过从大到小的排序后变成了998887777666 一不美观,二不容易比较. for (int i = 2; i < PG.Count; i++) //直接从2开始循环,因为PG[0],PG[1]的引用已经存储在其他变量中,直接比较即可 { if (PG[i] == PG[i - 2] && PG[i] == PG[i - 1]) // 比较PG[i]与PG[i-1],PG[i]与PG[i-2]是否同时相等,如果相等则说明这是三张相同牌 { if (Four >= 1) //默认的Four为0,所以第一次运行时这里为false,直接执行else //一旦连续出现两个三三相等,就会执行这里的if { FourPoker = PG[i]; //当找到四张牌时,记录下4张牌的点数 Poker changePoker; for (int k = i; k > 0; k--) //把四张牌中的一张移动到最前面. { changePoker = PG[k]; PG[k] = PG[k - 1]; PG[k - 1] = changePoker; } count++; //由于此时已经找到三张牌,下面为count赋值的程序不会执行,所以这里要手动+1 } else { Four++; //记录本次循环,因为本次循环找到了三三相等的牌,如果连续两次找到三三相等的牌则说明找到四张牌(炸弹) tempPokerGroup.Add(PG[i]); //把本次循环的PG[i]记录下来,即记录下三张牌的点数 } FindedThree = true; //标记已找到三张牌 } else { Four = 0; //没有找到时,连续找到三张牌的标志Four归零 if (!FindedThree) //只有没有找到三张牌时才让count增加.如果已经找到三张牌,则不再为count赋值. { count = i - 1; } } } foreach (Poker tempPoker in tempPokerGroup) //迭代所有的三张牌点数 { Poker changePoker; //临时交换Poker for (int i = 0; i < PG.Count; i++) //把所有的三张牌往前移动 { if (PG[i] == tempPoker) //当PG[i]等于三张牌的点数时 { if (PG[i] == FourPoker) //由于上面已经把4张牌中的一张放到的最前面,这张牌也会与tempPoker相匹配所以这里进行处理 // 当第一次遇到四张牌的点数时,把记录四张牌的FourPoker赋值为null,并中断本次循环.由于FourPoker已经为Null,所以下次再次遇到四张牌的点数时会按照正常情况执行. { FourPoker = null; continue; } changePoker = PG[i - count]; PG[i - count] = PG[i]; PG[i] = changePoker; } } } return(PG); }
/// <summary> /// 验证所出牌组是否符合游戏规则 /// </summary> public static bool IsRules(PokerGroup leadPokers) //判断所出牌组类型以及其是否符合规则 { bool isRule = false; Player.sort(leadPokers); switch (leadPokers.Count) { case 0: isRule = false; break; case 1: isRule = true; leadPokers.type = PokerGroupType.单张; break; case 2: if (IsSame(leadPokers, 2)) { isRule = true; leadPokers.type = PokerGroupType.对子; } else { if (leadPokers[0] == PokerNum.大王 && leadPokers[1] == PokerNum.小王) { leadPokers.type = PokerGroupType.双王; isRule = true; } else { isRule = false; } } break; case 3: if (IsSame(leadPokers, 3)) { leadPokers.type = PokerGroupType.相同; isRule = true; } else { isRule = false; } break; case 4: if (IsSame(leadPokers, 4)) { leadPokers.type = PokerGroupType.炸弹; isRule = true; } else { if (IsThreeLinkPokers(leadPokers)) { leadPokers.type = PokerGroupType.带一; isRule = true; } else { isRule = false; } } break; case 5: if (IsStraight(leadPokers)) { leadPokers.type = PokerGroupType.五张顺子; isRule = true; } else { isRule = false; } break; case 6: if (IsStraight(leadPokers)) { leadPokers.type = PokerGroupType.六张顺子; isRule = true; } else { if (IsLinkPair(leadPokers)) { leadPokers.type = PokerGroupType.连对; isRule = true; } else { if (IsSame(leadPokers, 4)) { leadPokers.type = PokerGroupType.四带二; isRule = true; } else { if (IsThreeLinkPokers(leadPokers)) { leadPokers.type = PokerGroupType.二连飞机; isRule = true; } else { isRule = false; } } } } break; case 7: if (IsStraight(leadPokers)) { leadPokers.type = PokerGroupType.七张顺子; isRule = true; } else { isRule = false; } break; case 8: if (IsStraight(leadPokers)) { leadPokers.type = PokerGroupType.八张顺子; isRule = true; } else { if (IsLinkPair(leadPokers)) { leadPokers.type = PokerGroupType.四连对; isRule = true; } else { if (IsThreeLinkPokers(leadPokers)) { leadPokers.type = PokerGroupType.飞机带翅膀; isRule = true; } else { isRule = false; } } } break; case 9: if (IsStraight(leadPokers)) { leadPokers.type = PokerGroupType.九张顺子; isRule = true; } else { if (IsThreeLinkPokers(leadPokers)) { leadPokers.type = PokerGroupType.连飞机; } else { isRule = false; } } break; case 10: if (IsStraight(leadPokers)) { leadPokers.type = PokerGroupType.十张顺子; isRule = true; } else { if (IsLinkPair(leadPokers)) { leadPokers.type = PokerGroupType.五连对; isRule = true; } else { isRule = false; } } break; case 11: if (IsStraight(leadPokers)) { leadPokers.type = PokerGroupType.十一张顺子; isRule = true; } else { isRule = false; } break; case 12: if (IsStraight(leadPokers)) { leadPokers.type = PokerGroupType.十二张顺子; isRule = true; } else { if (IsLinkPair(leadPokers)) { leadPokers.type = PokerGroupType.六连对; isRule = true; } else { if (IsThreeLinkPokers(leadPokers)) { //12有三连飞机带翅膀和四连飞机两种情况,所以在IsThreeLinkPokers中做了特殊处理,此处不用给type赋值. isRule = true; } else { isRule = false; } } } break; case 13: isRule = false; break; case 14: if (IsLinkPair(leadPokers)) { leadPokers.type = PokerGroupType.七连对; isRule = true; } else { isRule = false; } break; case 15: if (IsThreeLinkPokers(leadPokers)) { leadPokers.type = PokerGroupType.五连飞机; isRule = true; } else { isRule = false; } break; case 16: if (IsLinkPair(leadPokers)) { leadPokers.type = PokerGroupType.八连对; isRule = true; } else { if (IsThreeLinkPokers(leadPokers)) { leadPokers.type = PokerGroupType.四连飞机带翅膀; isRule = true; } else { isRule = false; } } break; case 17: isRule = false; break; case 18: if (IsLinkPair(leadPokers)) { leadPokers.type = PokerGroupType.六连对; isRule = true; } else { if (IsThreeLinkPokers(leadPokers)) { leadPokers.type = PokerGroupType.六连飞机; isRule = true; } else { isRule = false; } } break; case 19: isRule = false; break; case 20: if (IsLinkPair(leadPokers)) { leadPokers.type = PokerGroupType.十连对; isRule = true; } else { if (IsThreeLinkPokers(leadPokers)) { leadPokers.type = PokerGroupType.五连飞机带翅膀; isRule = true; } else { isRule = false; } } break; } #if DEBUG Console.WriteLine("玩家出的牌:"); foreach (Poker Poker in leadPokers) { Write(Poker.pokerColor.ToString() + Poker.ToString()); } #endif return(isRule); }