public bool AddDun(SssDun dun) { if (IsFullDun() || !IsEffectDun(dun)) { return(false); } Duns[++Index] = dun; return(true); }
public TreeNode AddChild(SssDun dun) { if (Dun.Compare(dun) >= 0) { TreeNode node = new TreeNode(this, dun); Children.AddLast(node); return(node); } return(null); }
public bool IsEffectDun(SssDun dun) { if (IsEmpty()) { return(true); } else { return(Duns[Index].Compare(dun) <= 0); } }
/// <summary> /// 与其他SssDun比较大小 /// </summary> /// <param name="dun"></param> /// <returns></returns> public int CompareLine(SssDun dun) { if (CardType != dun.CardType) { return(CardType - dun.CardType); } else { VnList list1 = new VnList(Cards); VnList list2 = new VnList(dun.Cards); return(list1.CompareLine(list2)); } }
public bool CheckShunZi(List <int> cards, List <SssDun> outList, int count, bool tonghua) { //cards.Sort((x, y) => -(GetValue(x) - GetValue(y))); SortList(cards, false); bool isShunzi = false; for (int i = 0; i < cards.Count - count + 1; i++) { int cardVi = GetValue(cards[i]); List <int> shunzi = new List <int> { cards[i] }; for (int j = i + 1; j < cards.Count; j++) { int cardVj = GetValue(cards[j]); if (cardVj == cardVi) { continue; } if (cardVi - 1 == cardVj) { shunzi.Add(cards[j]); cardVi = cardVj; } else { break; } //如果够数了 if (shunzi.Count >= count) { SssDun dun = new SssDun { CardType = tonghua ? CardType.tonghuashun : CardType.shunzi, Cards = shunzi }; outList.Add(dun); isShunzi = true; break; } } } return(isShunzi); }
public void AddDunToTree(List <int> cards, TreeNode treeNode, SssDun dun, int deep) { TreeNode node = treeNode.AddChild(dun); if (node != null) { List <int> cardClone = cards.GetRange(0, cards.Count); //删除已使用过的牌 foreach (int card in dun.Cards) { if (card != 0) { cardClone.Remove(card); } } //将剩余的牌继续检测 SetTreeNode(cardClone, node, deep); } else { treeNode.Parent.Children.Remove(treeNode); } }
//help public void SetTreeNode(List <int> cards, TreeNode treeNode, int deep) { if (deep > 3) { return; } int cardNum = deep == 3 ? 3 : 5; CnList cnList = new CnList(cards); VnList vnList = new VnList(cards); CnList cn5 = cnList.GetMoreThan(5, true, false); VnList vn2 = vnList.GetMoreThan(2, true); //如果没有关系的牌(有多个 或者 连续) 能组成任何牌型 后面的就不需要去确认别的牌 bool isCheckType = true; //bool isTongHuaShun = false; //同花顺 if (treeNode.Dun.CardType - CardType.tonghuashun >= 0 && deep <= 2) { if (cn5.Count > 0) { foreach (CnItem item in cn5) { List <SssDun> shunzi = new List <SssDun>(); if (CheckShunZi(item.Cards, shunzi, 5, true) && !CheckContainCard(vn2, shunzi, shunzi.Count - 1)) { isCheckType = false; } if (shunzi.Count > 0) { foreach (SssDun dun in shunzi) { AddDunToTree(cards, treeNode, dun, deep + 1); } break; } } } } //铁支 if (isCheckType && treeNode.Dun.CardType - CardType.tiezhi >= 0 && deep <= 2) { VnList vn4 = vnList.GetEqual(4, true); if (vn4.Count > 0) { foreach (VnItem item in vn4) { SssDun dun = new SssDun { CardType = CardType.tiezhi, Cards = vn4[0].Cards }; dun.Cards.Add(0); AddDunToTree(cards, treeNode, dun, deep + 1); if (!CheckLianXu(vnList, item.Cards[0])) { isCheckType = false; break; } } } } VnList vn3 = vnList.GetMoreThan(3, true); //葫芦 if (isCheckType && treeNode.Dun.CardType - CardType.hulu >= 0 && deep <= 2) { if (vn3.Count > 0 && vn2.Count > 0) { foreach (VnItem item3 in vn3) { foreach (VnItem item2 in vn2) { if (item3.Val == item2.Val) { continue; } SssDun dun = new SssDun { CardType = CardType.hulu, Cards = new List <int>(item3.Cards.GetRange(0, 3)) }; dun.Cards.AddRange(item2.Cards.GetRange(0, 2)); AddDunToTree(cards, treeNode, dun, deep + 1); } } } } //同花 if (isCheckType && treeNode.Dun.CardType - CardType.tonghua >= 0 && deep <= 2) { if (cn5.Count > 0) { //截取最前面的5个位最大的5个 //int loopcount while (cn5.Count > 0) { List <int> subCards = cn5[0].Cards.GetRange(0, 5); SssDun dun = new SssDun { CardType = CardType.tonghua, Cards = new List <int>(subCards) }; //如果当前牌型不是同花顺 if (CheckCardType(dun.Cards, false) != CardType.tonghuashun) { AddDunToTree(cards, treeNode, dun, deep + 1); int index = vn2.ContainsValues(dun.Cards); if (index != -1) { cn5[0].Cards.RemoveRange(0, 5); cn5 = cn5.GetMoreThan(5, true, true); } else { isCheckType = false; break; } } else { //移除 牌中最小的那个牌 然后重新确认cn5 cn5[0].Cards.Remove(dun.Cards[dun.Cards.Count - 1]); cn5 = cn5.GetMoreThan(5, true, true); } } } } //顺子 if (isCheckType && treeNode.Dun.CardType - CardType.shunzi >= 0 && deep <= 2) { List <SssDun> shunzi = new List <SssDun>(); if (CheckShunZi(cards, shunzi, 5, false) && !CheckContainCard(vn2, shunzi, shunzi.Count - 1)) { isCheckType = false; } if (shunzi.Count > 0) { foreach (SssDun dun in shunzi) { if (CheckCardType(dun.Cards, false) != CardType.tonghuashun) { AddDunToTree(cards, treeNode, dun, deep + 1); } } } } //三条 if (isCheckType && treeNode.Dun.CardType - CardType.shunzi >= 0) { if (vn3.Count > 0) { SssDun dun = new SssDun { CardType = CardType.santiao, Cards = new List <int>(vn3[0].Cards.GetRange(0, 3)) }; if (cardNum == 5) { dun.Cards.Add(0); dun.Cards.Add(0); } AddDunToTree(cards, treeNode, dun, deep + 1); isCheckType = false; } } //两对 if (isCheckType && treeNode.Dun.CardType - CardType.liangdui >= 0 && deep <= 2) { if (vn2.Count > 1) { SssDun dun = new SssDun { CardType = CardType.liangdui, Cards = vn2[0].Cards.GetRange(0, 2) }; dun.Cards.AddRange(vn2[1].Cards.GetRange(0, 2)); dun.Cards.Add(0); AddDunToTree(cards, treeNode, dun, deep + 1); } } //对子 if (isCheckType && treeNode.Dun.CardType - CardType.yidui >= 0) { if (vn2.Count > 0) { SssDun dun = new SssDun { CardType = CardType.yidui, Cards = vn2[0].Cards.GetRange(0, vn2[0].Cards.Count) }; if (cardNum == 5) { dun.Cards.Add(0); dun.Cards.Add(0); } dun.Cards.Add(0); AddDunToTree(cards, treeNode, dun, deep + 1); isCheckType = false; } } //散牌 if (isCheckType) { cardNum = cardNum > cards.Count ? cards.Count : cardNum; SssDun dun = new SssDun { CardType = CardType.sanpai, Cards = new List <int>(cards.GetRange(0, cardNum)) }; AddDunToTree(cards, treeNode, dun, deep + 1); } }
public TreeNode(TreeNode parent, SssDun dun) { Parent = parent; Dun = dun; Children = new LinkedList <TreeNode>(); }
//获取玩家的dun public List <PlayerDuns> getPlayerDuns(List <int> cards) { //筛选出来合适的牌 List <PlayerDuns> dunsOut = new List <PlayerDuns>(); SssDun dun = new SssDun { Cards = cards, CardType = CardType.none }; Root = new TreeNode(null, dun); SetTreeNode(cards, Root, 1); List <PlayerDuns> duns = new List <PlayerDuns>(); GetTreeDuns(duns, Root); dunsOut.Add(duns[0]); if (duns.Count > 1) { for (int i = 1; i < duns.Count; i++) { PlayerDuns dunTemp = duns[i]; bool isAdd = true; foreach (PlayerDuns outDun in dunsOut) { if (dunTemp.CheckAllBig(outDun) != 1) { isAdd = false; } } if (isAdd) { dunsOut.Add(dunTemp); } } } //将牌型补全 for (int i = 0; i < dunsOut.Count; i++) { List <int> temp = new List <int>(cards); PlayerDuns playerDun = dunsOut[i]; for (int j = 0; j < 3; j++) { SssDun pdun = playerDun.Duns[j]; if (pdun == null) { continue; } int cardCount = j == 0 ? 3 : 5; if (pdun.Cards.Count > cardCount) { pdun.Cards = pdun.Cards.GetRange(0, cardCount); } for (int z = 0; z < pdun.Cards.Count; z++) { if (pdun.Cards[z] == 0) { continue; } temp.Remove(pdun.Cards[z]); } } if (temp.Count <= 0) { continue; } int count = 0; for (int j = 0; j < 3; j++) { SssDun pdun = playerDun.Duns[j]; if (pdun == null) { pdun = new SssDun { Cards = temp.GetRange(0, j == 0 ? 3 : 5) }; pdun.CardType = CheckCardType(pdun.Cards, true); playerDun.Duns[j] = pdun; } for (int z = 0; z < pdun.Cards.Count; z++) { if (pdun.Cards[z] == 0) { pdun.Cards[z] = temp[count++]; } } } } //加入特殊牌型 GetSpecialCardType(cards, dunsOut); return(dunsOut); }