HGHuangHuangHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace ET.Server
  5. {
  6. /// <summary>
  7. /// 黄冈晃晃胡牌工具类
  8. /// </summary>
  9. public static class HGHuangHuangHelper
  10. {
  11. /// <summary>
  12. /// 统计每个的数量
  13. /// </summary>
  14. /// <param name="cards"></param>
  15. /// <returns></returns>
  16. public static int[] CountCardsNum(int[] cards) {
  17. 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};
  18. foreach (int value in cards)
  19. {
  20. tmp[value] += 1;
  21. }
  22. return tmp;
  23. }
  24. public static int CountCardNum(int[] target, int card) {
  25. int i = 0;
  26. foreach (int tar in target)
  27. {
  28. if (tar == card)
  29. {
  30. i++;
  31. }
  32. }
  33. return i;
  34. }
  35. /// <summary>
  36. /// 摸牌是否杠
  37. /// </summary>
  38. /// <param name="player"></param>
  39. /// <returns></returns>
  40. public static List<Struct.Kezi> IsDrawGang(Player player) {
  41. List<Struct.Kezi> list = new List<Struct.Kezi>();
  42. int[] count = CountCardsNum(player.RemainCards);
  43. // 暗杠
  44. for (int val = 0; val < count.Length; val++)
  45. {
  46. if (count[val] == 4)
  47. {
  48. // 4张牌暗杠
  49. list.Add(new Struct.Kezi((int)HGHuangHuangConst.KeziType.AN_GANG, val, player.Id));
  50. }
  51. }
  52. // 回杠
  53. foreach (Struct.Kezi kz in player.KeZi)
  54. {
  55. if (kz.Type == (int)HGHuangHuangConst.KeziType.PENG)
  56. {
  57. if (count[kz.Card] == 1)
  58. {
  59. list.Add(new Struct.Kezi((int)HGHuangHuangConst.KeziType.HUI_GANG, kz.Card, player.Id));
  60. }
  61. }
  62. }
  63. return list;
  64. }
  65. /// <summary>
  66. /// 出牌是否杠
  67. /// </summary>
  68. /// <param name="player">可杠玩家</param>
  69. /// <param name="card">可杠牌</param>
  70. /// <param name="oth_uid">杠谁的</param>
  71. /// <returns></returns>
  72. public static List<Struct.Kezi> IsDiscardGang(Player player, int card, long oth_uid) {
  73. List<Struct.Kezi> list = new List<Struct.Kezi>();
  74. int num = CountCardNum(player.RemainCards, card);
  75. if (num == 3)
  76. {
  77. list.Add(new Struct.Kezi((int)HGHuangHuangConst.KeziType.MING_GANG, card, oth_uid));
  78. }
  79. return list;
  80. }
  81. /// <summary>
  82. /// 出牌是否碰
  83. /// </summary>
  84. /// <param name="player">可碰玩家</param>
  85. /// <param name="card">可碰牌</param>
  86. /// <param name="oth_uid">碰谁的</param>
  87. /// <returns></returns>
  88. public static List<Struct.Kezi> IsDiscardPeng(Player player, int card, long oth_uid) {
  89. List<Struct.Kezi> list = new List<Struct.Kezi>();
  90. int num = CountCardNum(player.RemainCards, card);
  91. if (num >= 2)
  92. {
  93. list.Add(new Struct.Kezi((int)HGHuangHuangConst.KeziType.PENG, card, oth_uid));
  94. }
  95. return list;
  96. }
  97. /// <summary>
  98. /// 出牌是否吃
  99. /// </summary>
  100. /// <param name="player"></param>
  101. /// <param name="card"></param>
  102. /// <param name="oth_uid"></param>
  103. /// <returns></returns>
  104. public static List<Struct.Kezi> IsDiscardChi(Player player, int card, long oth_uid) {
  105. List<Struct.Kezi> list = new List<Struct.Kezi>();
  106. int[] count = CountCardsNum(player.RemainCards);
  107. if (card < HGHuangHuangConst.DONG_FENG)
  108. {
  109. if ((card - 2) >= HGHuangHuangConst.YI_WAN && count[card - 2] >= 1 && count[card - 1] >= 1)
  110. {
  111. list.Add(new Struct.Kezi((int)HGHuangHuangConst.KeziType.CHI, card - 2, oth_uid));
  112. }
  113. if ((card - 1) >= HGHuangHuangConst.YI_WAN && count[card - 1] >= 1 && count[card + 1] >= 1)
  114. {
  115. list.Add(new Struct.Kezi((int)HGHuangHuangConst.KeziType.CHI, card - 1, oth_uid));
  116. }
  117. if (count[card + 1] >= 1 && count[card + 2] >= 1)
  118. {
  119. list.Add(new Struct.Kezi((int)HGHuangHuangConst.KeziType.CHI, card, oth_uid));
  120. }
  121. }
  122. return list;
  123. }
  124. /// <summary>
  125. /// 检测胡牌
  126. /// </summary>
  127. /// <param name="cards"></param>
  128. /// <returns></returns>
  129. public static Struct.HuRes CheckHu(int[] cards) {
  130. Struct.HuRes res = new ();
  131. int[] tem = new int[cards.Length];
  132. Array.Copy(cards, tem, cards.Length);
  133. Array.Sort(tem);
  134. if (IsHu7Dui(tem))
  135. {
  136. // 先检查七对
  137. res.Type = HGHuangHuangConst.HU_7DUI;
  138. res.Jiang = 0;
  139. return res;
  140. }
  141. int jiang = IsHuDuiDui(tem);
  142. if (jiang != 0)
  143. {
  144. // 再检查对对胡
  145. res.Type = HGHuangHuangConst.HU_DUIDUI;
  146. res.Jiang = jiang;
  147. return res;
  148. }
  149. jiang = IsHuBase(tem);
  150. if (jiang != 0)
  151. {
  152. // 最后检查平胡
  153. res.Type = HGHuangHuangConst.HU_BASE;
  154. res.Jiang = jiang;
  155. return res;
  156. }
  157. res.Type = HGHuangHuangConst.HU_DEFAULT;
  158. res.Jiang = 0;
  159. return res;
  160. }
  161. /// <summary>
  162. /// 是否满足清一色(没有宝牌)
  163. /// </summary>
  164. /// <param name="list">刻子集合</param>
  165. /// <param name="cards">手牌加最后摸得牌</param>
  166. /// <returns></returns>
  167. private static bool CheckQingYiSe(List<Struct.Kezi> list, int[] cards) {
  168. int[] tempCards = new int[cards.Length];
  169. Array.Copy(cards, 0, tempCards, 0, tempCards.Length);
  170. tempCards = list.Aggregate(tempCards, (current, kezi) => CardHelper.Add(current, kezi.Card));
  171. int tong = 0, tiao = 0, feng = 0;
  172. foreach (int i in tempCards)
  173. {
  174. int high = i >> 4;
  175. switch (high)
  176. {
  177. case 1:
  178. tong = 1;
  179. break;
  180. case 2:
  181. tiao = 1;
  182. break;
  183. case 3:
  184. feng = 1;
  185. break;
  186. }
  187. }
  188. return feng == 0 && (tong + tiao == 1);
  189. }
  190. /// <summary>
  191. /// 对对胡
  192. /// </summary>
  193. /// <param name="cards">手上的牌</param>
  194. /// <returns></returns>
  195. private static int IsHuDuiDui(int[] cards) {
  196. int[] count = CountCardsNum(cards);
  197. int jiang = 0;
  198. for (int i = 0; i < cards.Length;)
  199. {
  200. int value = cards[i];
  201. if (count[value] >= 3)
  202. {
  203. count[value] -= 3;
  204. }
  205. else
  206. {
  207. if (jiang == 0)
  208. {
  209. if (count[value] == 2)
  210. {
  211. count[value] -= 2;
  212. jiang = value;
  213. }
  214. else
  215. {
  216. i++;
  217. }
  218. }
  219. else
  220. {
  221. i++;
  222. }
  223. }
  224. }
  225. foreach (int aCount in count)
  226. {
  227. if (aCount > 0)
  228. {
  229. return 0;
  230. }
  231. }
  232. return jiang;
  233. }
  234. /// <summary>
  235. /// 七对
  236. /// </summary>
  237. /// <param name="cards">手上的牌</param>
  238. /// <returns></returns>
  239. private static bool IsHu7Dui(int[] cards) {
  240. if (cards.Length != 14)
  241. {
  242. return false;
  243. }
  244. int[] num = CountCardsNum(cards);
  245. int count = 0;
  246. foreach (int n in num)
  247. {
  248. if (n == 3 || n == 1)
  249. {
  250. count += 1;
  251. break;
  252. }
  253. }
  254. return count == 0;
  255. }
  256. /// <summary>
  257. /// 校验基本胡(查找可胡牌的将)
  258. /// </summary>
  259. /// <param name="cards">升序排列</param>
  260. /// <returns></returns>
  261. public static int IsHuBase(int[] cards) {
  262. int[] tmpCount = CountCardsNum(cards);
  263. for (int i = 0; i < cards.Length; i++)
  264. {
  265. int card = cards[i];
  266. int[] count = new int[tmpCount.Length];
  267. Array.Copy(tmpCount, 0, count, 0, tmpCount.Length);
  268. int jiang = 0;
  269. if (count[card] >= 2)
  270. {
  271. jiang = card;
  272. count[card] -= 2;
  273. int[] countDesc = new int[count.Length];
  274. Array.Copy(count, 0, countDesc, 0, count.Length);
  275. bool b = PingHuNoAnyCardAsc(cards, count);
  276. if (b)
  277. {
  278. return jiang;
  279. }
  280. else
  281. {
  282. b = PingHuNoAnyCardDesc(cards, countDesc);
  283. if (b)
  284. {
  285. return jiang;
  286. }
  287. }
  288. }
  289. }
  290. return 0;
  291. }
  292. private static bool PingHuNoAnyCardDesc(int[] cards, int[] count) {
  293. for (int j = cards.Length - 1; j >= 0;)
  294. {
  295. int val = cards[j];
  296. if (count[val] >= 3)
  297. {
  298. count[val] = count[val] - 3;
  299. }
  300. else if (count[val] > 0 && val > 2)
  301. {
  302. if (count[val - 1] > 0 && count[val - 2] > 0 && val >> 4 != 3)
  303. {
  304. // 顺子
  305. count[val] -= 1;
  306. count[val - 1] -= 1;
  307. count[val - 2] -= 1;
  308. }
  309. else
  310. {
  311. break;
  312. }
  313. }
  314. else
  315. {
  316. j--;
  317. }
  318. }
  319. bool hu = true;
  320. foreach (int v in cards)
  321. {
  322. if (count[v] > 0)
  323. {
  324. hu = false;
  325. break;
  326. }
  327. }
  328. return hu;
  329. }
  330. private static bool PingHuNoAnyCardAsc(int[] cards, int[] count) {
  331. for (int j = 0; j < cards.Length;)
  332. {
  333. int val = cards[j];
  334. if (count[val] >= 3)
  335. {
  336. count[val] = count[val] - 3;
  337. }
  338. else if (count[val] > 0 && val < count.Length - 2 && val >> 4 != 3)
  339. {
  340. if (count[val + 1] > 0 && count[val + 2] > 0)
  341. {
  342. // 顺子
  343. count[val] -= 1;
  344. count[val + 1] -= 1;
  345. count[val + 2] -= 1;
  346. }
  347. else
  348. {
  349. break;
  350. }
  351. }
  352. else
  353. {
  354. j++;
  355. }
  356. }
  357. bool hu = true;
  358. foreach (int v in cards)
  359. {
  360. if (count[v] > 0)
  361. {
  362. hu = false;
  363. break;
  364. }
  365. }
  366. return hu;
  367. }
  368. }
  369. }