123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- 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;
- }
- }
- }
|