|
@@ -0,0 +1,384 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
+
|
|
|
+namespace ET.Server
|
|
|
+{
|
|
|
+ /// <summary>
|
|
|
+ /// 黄冈晃晃胡牌工具类
|
|
|
+ /// </summary>
|
|
|
+ public static class HGHuangHuangHelper
|
|
|
+ {
|
|
|
+ /// <summary>
|
|
|
+ /// 统计每个的数量
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="cards"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static int[] CountCardsNum(int[] cards) {
|
|
|
+ int[] tmp = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
|
|
+ foreach (int value in cards)
|
|
|
+ {
|
|
|
+ tmp[value] += 1;
|
|
|
+ }
|
|
|
+ return tmp;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int CountCardNum(int[] target, int card) {
|
|
|
+ int i = 0;
|
|
|
+ foreach (int tar in target)
|
|
|
+ {
|
|
|
+ if (tar == card)
|
|
|
+ {
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return i;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 摸牌是否杠
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="player"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static List<Struct.Kezi> IsDrawGang(Player player) {
|
|
|
+ List<Struct.Kezi> list = new List<Struct.Kezi>();
|
|
|
+ int[] count = CountCardsNum(player.RemainCards);
|
|
|
+ // 暗杠
|
|
|
+ for (int val = 0; val < count.Length; val++)
|
|
|
+ {
|
|
|
+ if (count[val] == 4)
|
|
|
+ {
|
|
|
+ // 4张牌暗杠
|
|
|
+ list.Add(new Struct.Kezi((int)HGHuangHuangConst.KeziType.AN_GANG, val, player.Id));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 回杠
|
|
|
+ foreach (Struct.Kezi kz in player.KeZi)
|
|
|
+ {
|
|
|
+ if (kz.Type == (int)HGHuangHuangConst.KeziType.PENG)
|
|
|
+ {
|
|
|
+ if (count[kz.Card] == 1)
|
|
|
+ {
|
|
|
+ list.Add(new Struct.Kezi((int)HGHuangHuangConst.KeziType.HUI_GANG, kz.Card, player.Id));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 出牌是否杠
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="player">可杠玩家</param>
|
|
|
+ /// <param name="card">可杠牌</param>
|
|
|
+ /// <param name="oth_uid">杠谁的</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static List<Struct.Kezi> IsDiscardGang(Player player, int card, long oth_uid) {
|
|
|
+ List<Struct.Kezi> list = new List<Struct.Kezi>();
|
|
|
+ int num = CountCardNum(player.RemainCards, card);
|
|
|
+ if (num == 3)
|
|
|
+ {
|
|
|
+ list.Add(new Struct.Kezi((int)HGHuangHuangConst.KeziType.MING_GANG, card, oth_uid));
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 出牌是否碰
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="player">可碰玩家</param>
|
|
|
+ /// <param name="card">可碰牌</param>
|
|
|
+ /// <param name="oth_uid">碰谁的</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static List<Struct.Kezi> IsDiscardPeng(Player player, int card, long oth_uid) {
|
|
|
+ List<Struct.Kezi> list = new List<Struct.Kezi>();
|
|
|
+ int num = CountCardNum(player.RemainCards, card);
|
|
|
+ if (num >= 2)
|
|
|
+ {
|
|
|
+ list.Add(new Struct.Kezi((int)HGHuangHuangConst.KeziType.PENG, card, oth_uid));
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 出牌是否吃
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="player"></param>
|
|
|
+ /// <param name="card"></param>
|
|
|
+ /// <param name="oth_uid"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static List<Struct.Kezi> IsDiscardChi(Player player, int card, long oth_uid) {
|
|
|
+ List<Struct.Kezi> list = new List<Struct.Kezi>();
|
|
|
+ int[] count = CountCardsNum(player.RemainCards);
|
|
|
+ if (card < HGHuangHuangConst.DONG_FENG)
|
|
|
+ {
|
|
|
+ if ((card - 2) >= HGHuangHuangConst.YI_WAN && count[card - 2] >= 1 && count[card - 1] >= 1)
|
|
|
+ {
|
|
|
+ list.Add(new Struct.Kezi((int)HGHuangHuangConst.KeziType.CHI, card - 2, oth_uid));
|
|
|
+ }
|
|
|
+ if ((card - 1) >= HGHuangHuangConst.YI_WAN && count[card - 1] >= 1 && count[card + 1] >= 1)
|
|
|
+ {
|
|
|
+ list.Add(new Struct.Kezi((int)HGHuangHuangConst.KeziType.CHI, card - 1, oth_uid));
|
|
|
+ }
|
|
|
+ if (count[card + 1] >= 1 && count[card + 2] >= 1)
|
|
|
+ {
|
|
|
+ list.Add(new Struct.Kezi((int)HGHuangHuangConst.KeziType.CHI, card, oth_uid));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 检测胡牌
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="cards"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static Struct.HuRes CheckHu(int[] cards) {
|
|
|
+ Struct.HuRes res = new ();
|
|
|
+ int[] tem = new int[cards.Length];
|
|
|
+ Array.Copy(cards, tem, cards.Length);
|
|
|
+ Array.Sort(tem);
|
|
|
+ if (IsHu7Dui(tem))
|
|
|
+ {
|
|
|
+ // 先检查七对
|
|
|
+ res.Type = HGHuangHuangConst.HU_7DUI;
|
|
|
+ res.Jiang = 0;
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+ int jiang = IsHuDuiDui(tem);
|
|
|
+ if (jiang != 0)
|
|
|
+ {
|
|
|
+ // 再检查对对胡
|
|
|
+ res.Type = HGHuangHuangConst.HU_DUIDUI;
|
|
|
+ res.Jiang = jiang;
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+ jiang = IsHuBase(tem);
|
|
|
+ if (jiang != 0)
|
|
|
+ {
|
|
|
+ // 最后检查平胡
|
|
|
+ res.Type = HGHuangHuangConst.HU_BASE;
|
|
|
+ res.Jiang = jiang;
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+ res.Type = HGHuangHuangConst.HU_DEFAULT;
|
|
|
+ res.Jiang = 0;
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 是否满足清一色(没有宝牌)
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="list">刻子集合</param>
|
|
|
+ /// <param name="cards">手牌加最后摸得牌</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private static bool CheckQingYiSe(List<Struct.Kezi> list, int[] cards) {
|
|
|
+ int[] tempCards = new int[cards.Length];
|
|
|
+ Array.Copy(cards, 0, tempCards, 0, tempCards.Length);
|
|
|
+ tempCards = list.Aggregate(tempCards, (current, kezi) => CardHelper.Add(current, kezi.Card));
|
|
|
+ int tong = 0, tiao = 0, feng = 0;
|
|
|
+ foreach (int i in tempCards)
|
|
|
+ {
|
|
|
+ int high = i >> 4;
|
|
|
+ switch (high)
|
|
|
+ {
|
|
|
+ case 1:
|
|
|
+ tong = 1;
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ tiao = 1;
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ feng = 1;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return feng == 0 && (tong + tiao == 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 对对胡
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="cards">手上的牌</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private static int IsHuDuiDui(int[] cards) {
|
|
|
+ int[] count = CountCardsNum(cards);
|
|
|
+ int jiang = 0;
|
|
|
+ for (int i = 0; i < cards.Length;)
|
|
|
+ {
|
|
|
+ int value = cards[i];
|
|
|
+ if (count[value] >= 3)
|
|
|
+ {
|
|
|
+ count[value] -= 3;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (jiang == 0)
|
|
|
+ {
|
|
|
+ if (count[value] == 2)
|
|
|
+ {
|
|
|
+ count[value] -= 2;
|
|
|
+ jiang = value;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ foreach (int aCount in count)
|
|
|
+ {
|
|
|
+ if (aCount > 0)
|
|
|
+ {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return jiang;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 七对
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="cards">手上的牌</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private static bool IsHu7Dui(int[] cards) {
|
|
|
+ if (cards.Length != 14)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ int[] num = CountCardsNum(cards);
|
|
|
+ int count = 0;
|
|
|
+ foreach (int n in num)
|
|
|
+ {
|
|
|
+ if (n == 3 || n == 1)
|
|
|
+ {
|
|
|
+ count += 1;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return count == 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 校验基本胡(查找可胡牌的将)
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="cards">升序排列</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static int IsHuBase(int[] cards) {
|
|
|
+ int[] tmpCount = CountCardsNum(cards);
|
|
|
+ for (int i = 0; i < cards.Length; i++)
|
|
|
+ {
|
|
|
+ int card = cards[i];
|
|
|
+ int[] count = new int[tmpCount.Length];
|
|
|
+ Array.Copy(tmpCount, 0, count, 0, tmpCount.Length);
|
|
|
+ int jiang = 0;
|
|
|
+ if (count[card] >= 2)
|
|
|
+ {
|
|
|
+ jiang = card;
|
|
|
+ count[card] -= 2;
|
|
|
+ int[] countDesc = new int[count.Length];
|
|
|
+ Array.Copy(count, 0, countDesc, 0, count.Length);
|
|
|
+ bool b = PingHuNoAnyCardAsc(cards, count);
|
|
|
+ if (b)
|
|
|
+ {
|
|
|
+ return jiang;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ b = PingHuNoAnyCardDesc(cards, countDesc);
|
|
|
+ if (b)
|
|
|
+ {
|
|
|
+ return jiang;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static bool PingHuNoAnyCardDesc(int[] cards, int[] count) {
|
|
|
+ for (int j = cards.Length - 1; j >= 0;)
|
|
|
+ {
|
|
|
+ int val = cards[j];
|
|
|
+ if (count[val] >= 3)
|
|
|
+ {
|
|
|
+ count[val] = count[val] - 3;
|
|
|
+ }
|
|
|
+ else if (count[val] > 0 && val > 2)
|
|
|
+ {
|
|
|
+ if (count[val - 1] > 0 && count[val - 2] > 0 && val >> 4 != 3)
|
|
|
+ {
|
|
|
+ // 顺子
|
|
|
+ count[val] -= 1;
|
|
|
+ count[val - 1] -= 1;
|
|
|
+ count[val - 2] -= 1;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ j--;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ bool hu = true;
|
|
|
+ foreach (int v in cards)
|
|
|
+ {
|
|
|
+ if (count[v] > 0)
|
|
|
+ {
|
|
|
+ hu = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return hu;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static bool PingHuNoAnyCardAsc(int[] cards, int[] count) {
|
|
|
+ for (int j = 0; j < cards.Length;)
|
|
|
+ {
|
|
|
+ int val = cards[j];
|
|
|
+ if (count[val] >= 3)
|
|
|
+ {
|
|
|
+ count[val] = count[val] - 3;
|
|
|
+ }
|
|
|
+ else if (count[val] > 0 && val < count.Length - 2 && val >> 4 != 3)
|
|
|
+ {
|
|
|
+ if (count[val + 1] > 0 && count[val + 2] > 0)
|
|
|
+ {
|
|
|
+ // 顺子
|
|
|
+ count[val] -= 1;
|
|
|
+ count[val + 1] -= 1;
|
|
|
+ count[val + 2] -= 1;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ j++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ bool hu = true;
|
|
|
+ foreach (int v in cards)
|
|
|
+ {
|
|
|
+ if (count[v] > 0)
|
|
|
+ {
|
|
|
+ hu = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return hu;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|