예제 #1
0
        public static PokerPile Pile(int numType, int count)
        {
            var pile = new PokerPile(numType);

            pile.AddPoker(count);
            return(pile);
        }
예제 #2
0
        public PokerPattern SelectSuperABCDE(PokerPattern lastABCDE, int heartHostCount, bool chaiPai)
        {
            if (lastABCDE == null || lastABCDE.IsNull)
            {
                lastABCDE = NULL_ABCDE;
            }

            List <SelectNode> selectChain = BuildSelectChain(lastABCDE);

            PokerPile selected =
                SelectNormal(PatternType.IsStraight(lastABCDE.Type), selectChain, 0, null, heartHostCount, chaiPai, 5);

            while (selected != null)
            {
                List <Poker> pokers = GetAllPokers(selected, true);
                if (pokers != null)
                {
                    return(_matcher.Match(pokers));
                }
                else
                {
                    var validator = (PRSelectRoot)selectChain[0].Validator;
                    validator.NumType = selected.NumType;
                    BuildPokerPool();
                    selected = SelectNormal(PatternType.IsStraight(lastABCDE.Type), selectChain, 0, null,
                                            heartHostCount,
                                            true, 5);
                }
            }

            return(PokerPattern.NULL);
        }
예제 #3
0
        public int GetNextNumType(PokerPile prePile, bool isStraight)
        {
            if (prePile == null)
            {
                return(PokerNumType.NULL);
            }

            int number;

            if (isStraight)
            {
                if (prePile.NumType == PokerNumType.PA)
                {
                    number = 1;
                }
                else
                {
                    number = prePile.NumType;
                }
            }
            else
            {
                if (prePile.NumType == PokerNumType.PA)
                {
                    return(PokerNumType.NULL);
                }
                else
                {
                    number = prePile.NumType;
                }
            }

            return(number + 1);
        }
예제 #4
0
        public PokerPattern Match(PokerPile pile, int heartHostCount, List <Poker> pokers)
        {
            PokerPileChain ppc     = new PokerPileChain(pile, heartHostCount);
            MatchResult    matched = MatchTree.Match(ppc);

            PostProcess(matched, pokers);
            return(new PokerPattern(matched.PatternType, ppc.FirstPile, matched.MajorNumType, pokers));
        }
예제 #5
0
        public MatchResult Match(PokerPileChain ppc)
        {
            if (ppc.IsEmpty)
            {
                return(new MatchResult(PatternType.BUCHU, PokerNumType.NULL));
            }

            if (ppc.PileCount == 0)
            {
                if (!ppc.ContainHeartHost)
                {
                    return(new MatchResult(PatternType.NULL, PokerNumType.NULL));
                }

                MatchNode root = GetRoot(ppc.HeartHostCount);
                if (root == null)
                {
                    return(new MatchResult(PatternType.NULL, PokerNumType.NULL));
                }

                return(root.MatchHeartHost(ppc.Head, ppc.HeartHostCount));
            }

            PokerPile firstPile = ppc.FirstPile;

            if (!ppc.ContainHeartHost)
            {
                MatchNode root = GetRoot(firstPile.Count);
                if (root != null)
                {
                    return(root.Match(null, firstPile, 0));
                }
                else
                {
                    return(new MatchResult(PatternType.NULL, PokerNumType.NULL));
                }
            }
            else
            {
                MatchResult result = new MatchResult(PatternType.NULL, PokerNumType.NULL);
                for (int i = ppc.HeartHostCount; i >= 0; i--)
                {
                    int       pokerCount = firstPile != null ? firstPile.Count : 0;
                    MatchNode root       = GetRoot(pokerCount + i);
                    if (root != null)
                    {
                        result = root.Match(null, firstPile, ppc.HeartHostCount);
                        if (!result.IsPatternNull)
                        {
                            break;
                        }
                    }
                }

                return(result);
            }
        }
예제 #6
0
        public PokerPattern(int type, PokerPile headPile, int majorNumType, List <Poker> pokers)
        {
            Type         = type;
            HeadPile     = headPile;
            MajorNumType = majorNumType;

            Pokers.Clear();
            if (pokers != null)
            {
                Pokers.AddRange(pokers);
            }
        }
예제 #7
0
        public void RestoreChaiPaiPile(int numType, int fromPool, int count)
        {
            List <PokerPile> origin = null;

            if (_pilePool.ContainsKey(fromPool))
            {
                origin = _pilePool[fromPool];
            }

            List <PokerPile> to = null;

            if (_pilePool.ContainsKey(fromPool - count))
            {
                to = _pilePool[fromPool - count];
            }

            int leftCount = 0;

            for (int i = 0; i < to.Count; i++)
            {
                PokerPile cur = to[i];
                if (cur.NumType == numType)
                {
                    leftCount = cur.Count;
                    to.RemoveAt(i);
                    break;
                }
            }

            var found = false;

            for (int i = 0; i < origin.Count; i++)
            {
                PokerPile cur = origin[i];
                if (cur.NumType == numType)
                {
                    cur.AddPoker(count + leftCount);
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                InsertPile(new PokerPile(numType, count + leftCount), origin);
            }
        }
예제 #8
0
        private PokerPattern SelectBiggerXXXX(PokerPattern lastChuPai, int fromPokerCount, int toPokerCount,
                                              int heartHostCount)
        {
            for (int availableHeartHost = 0; availableHeartHost <= heartHostCount; availableHeartHost++)
            {
                for (int pokerCount = fromPokerCount - availableHeartHost; pokerCount <= toPokerCount; pokerCount++)
                {
                    List <PokerPile> piles = null;
                    if (_pilePool.ContainsKey(pokerCount))
                    {
                        piles = _pilePool[pokerCount];
                    }

                    if (piles != null && piles.Count > 0)
                    {
                        if (pokerCount + availableHeartHost == lastChuPai.PokerCount)
                        {
                            for (int n = 0; n < piles.Count; n++)
                            {
                                PokerPile pile = piles[n];
                                if (_value.Compare(pile.NumType, lastChuPai.MajorNumType) == CompareResult.BIGGER)
                                {
                                    pile.AddPoker(availableHeartHost);
                                    return(new PokerPattern(
                                               PatternType.XXXX,
                                               pile,
                                               pile.NumType,
                                               GetAllPokers(pile, false)));
                                }
                            }
                        }
                        else if (pokerCount + availableHeartHost > lastChuPai.PokerCount)
                        {
                            PokerPile pile = piles[0];
                            pile.AddPoker(availableHeartHost);
                            return(new PokerPattern(
                                       PatternType.XXXX,
                                       pile,
                                       pile.NumType,
                                       GetAllPokers(pile, false)));
                        }
                    }
                }
            }

            return(PokerPattern.NULL);
        }
예제 #9
0
        public List <PokerPile> RemoveXD(List <Poker> pokers)
        {
            List <Poker> xd = new List <Poker>();
            PokerPile    x  = new PokerPile(PokerNumType.PX, 0);
            PokerPile    d  = new PokerPile(PokerNumType.PD, 0);

            for (int i = 0; i < pokers.Count; i++)
            {
                Poker p = pokers[i];
                if (p.NumType == PokerNumType.PX || p.NumType == PokerNumType.PD)
                {
                    xd.Add(p);
                    if (p.NumType == PokerNumType.PX)
                    {
                        x.AddPoker(1);
                    }
                    else
                    {
                        d.AddPoker(1);
                    }
                }
            }

            if (xd.Count > 0)
            {
                foreach (var p in xd)
                {
                    pokers.Remove(p);
                }
            }

            List <PokerPile> xdPile = new List <PokerPile>();

            if (x.Count > 0)
            {
                xdPile.Add(x);
            }
            if (d.Count > 0)
            {
                xdPile.Add(d);
            }

            return(xdPile);
        }
예제 #10
0
 public PRVerifyResult ValidateRelation(PokerPile previous, PokerPile current, int pokerCount,
                                        int heartHostCount)
 {
     if (Validator == null)
     {
         return(new PRVerifyResult(true, 0, 0, null));
     }
     else
     {
         if (previous == null)
         {
             return(Validator.VerifyRoot(current, pokerCount, heartHostCount));
         }
         else
         {
             return(Validator.Verify(previous, current, pokerCount, heartHostCount));
         }
     }
 }
예제 #11
0
        public void InsertPile(PokerPile pile, List <PokerPile> list)
        {
            bool added = false;

            for (int i = 0; i < list.Count; i++)
            {
                if (_value.Compare(pile.NumType, list[i].NumType) == CompareResult.SMALLER)
                {
                    list.Insert(i, pile);
                    added = true;
                    break;
                }
            }

            if (!added)
            {
                list.Add(pile);
            }
        }
예제 #12
0
        public override MatchResult Match(PokerPile previous, PokerPile current, int heartHostCount)
        {
            if (previous == null || current == null)
            {
                return(base.Match(previous, current, heartHostCount));
            }

            int numType1 = previous.NumType;
            int numType2 = current.NumType;

            if ((numType1 == PokerNumType.PX || numType1 == PokerNumType.PD) && previous.Count == 2)
            {
                if ((numType2 == PokerNumType.PX || numType2 == PokerNumType.PD) && current.Count == 2)
                {
                    return(new MatchResult(PokerLogic.PatternType.XXDD, PokerNumType.PD));
                }
            }

            return(base.Match(previous, current, heartHostCount));
        }
예제 #13
0
        public PokerPile AdjustPileOrder(PokerPile head, int patternType)
        {
            var       piles = new List <PokerPile>();
            PokerPile next  = head;

            while (next != null)
            {
                piles.Add(next);
                next = next.Next;
            }

            int majorCount = PatternType.GetMajorPileCount(patternType);

            if (piles.Count < majorCount)
            {
                return(null);
            }

            for (int i = majorCount - 1; i > 0; i--)
            {
                piles[i].Next = piles[i - 1];
            }

            piles[0].Next = null;

            if (piles.Count > majorCount)
            {
                for (int i = piles.Count - 1; i > majorCount; i--)
                {
                    piles[i].Next = piles[i - 1];
                }

                piles[majorCount].Next = null;
                piles[0].Next          = piles[piles.Count - 1];
            }

            return(piles[majorCount - 1]);
        }
예제 #14
0
        public PRVerifyResult Verify(SelectNode node, PokerPile previous, PokerPile current, int heartHostCount)
        {
            IPileRelation  validator = node.Validator;
            PRVerifyResult result;

            if (validator != null)
            {
                if (previous == null)
                {
                    result = validator.VerifyRoot(current, node.PokerCount, heartHostCount);
                }
                else
                {
                    result = validator.Verify(previous, current, node.PokerCount, heartHostCount);
                }
            }
            else
            {
                result = new PRVerifyResult(true, 0, 0, null);
            }

            return(result);
        }
예제 #15
0
        public PokerPile AdjustChaiPaiPile(PokerPile selected, List <PokerPile> from, int pokerCount, int needCount)
        {
            int newCount = pokerCount - needCount;

            from.Remove(selected);
            selected.RemovePoker(needCount);

            List <PokerPile> list = null;

            if (_pilePool.ContainsKey(newCount))
            {
                list = _pilePool[newCount];
            }

            if (list == null)
            {
                list = new List <PokerPile>();
                _pilePool[newCount] = list;
            }

            InsertPile(selected, list);

            return(new PokerPile(selected.NumType, needCount));
        }
예제 #16
0
        public PokerPile SelectNormal(bool isStraight, List <SelectNode> selectChain, int curSelectIndex,
                                      PokerPile prePile, int heartHostCount, bool chaiPai, int endSelectSize)
        {
            if (curSelectIndex >= selectChain.Count)
            {
                return(null);
            }

            var selectNode = selectChain[curSelectIndex];

            for (int availableHeartHost = 0; availableHeartHost <= heartHostCount; availableHeartHost++)
            {
                int start = selectNode.PokerCount - availableHeartHost;
                if (start > endSelectSize)
                {
                    continue;
                }

                int[] selectSeq = new int[endSelectSize + 1 - start];
                selectSeq[0] = selectNode.PokerCount;

                if (selectSeq.Length > 1)
                {
                    int seqIdx = 1;
                    for (int pokerCount = start; pokerCount <= endSelectSize; pokerCount++)
                    {
                        if (pokerCount == selectNode.PokerCount)
                        {
                            continue;
                        }

                        selectSeq[seqIdx] = pokerCount;
                        seqIdx++;
                    }
                }

                for (int idx = 0; idx < selectSeq.Length; idx++)
                {
                    int pokerCount = selectSeq[idx];
                    if (!chaiPai && selectNode.PokerCount < pokerCount)
                    {
                        continue;
                    }

                    List <PokerPile> piles = null;
                    if (_pilePool.ContainsKey(pokerCount))
                    {
                        piles = _pilePool[pokerCount];
                    }

                    if (piles == null || piles.Count <= 0)
                    {
                        continue;
                    }

                    for (int i = 0; i < piles.Count; i++)
                    {
                        PokerPile      pile  = piles[i];
                        PRVerifyResult valid = Verify(selectNode, prePile, pile, availableHeartHost);

                        PokerPile selected = null;
                        if (valid.Success)
                        {
                            selected = pile;
                        }

                        if (selected != null)
                        {
                            bool isChaiPai = selectNode.PokerCount < pokerCount;
                            if (curSelectIndex < selectChain.Count - 1)
                            {
                                piles.Remove(selected);
                                if (isChaiPai)
                                {
                                    selected = AdjustChaiPaiPile(selected, piles, pokerCount, selectNode.PokerCount);
                                }

                                PokerPile result = SelectNormal(
                                    isStraight,
                                    selectChain,
                                    curSelectIndex + 1,
                                    selected,
                                    availableHeartHost - valid.UsedHeartHostCount,
                                    chaiPai,
                                    endSelectSize
                                    );

                                if (result != null)
                                {
                                    selected.Next = result;
                                    return(selected);
                                }
                                else
                                {
                                    if (isChaiPai)
                                    {
                                        RestoreChaiPaiPile(selected.NumType, pokerCount, selectNode.PokerCount);
                                    }
                                    else
                                    {
                                        InsertPile(selected, piles);
                                    }

                                    selected.RemovePoker(valid.UsedHeartHostCount);
                                }
                            }
                            else
                            {
                                piles.Remove(selected);
                                if (isChaiPai)
                                {
                                    selected = AdjustChaiPaiPile(selected, piles, pokerCount, selectNode.PokerCount);
                                }

                                return(selected);
                            }
                        }
                    }
                }

                if (selectNode.PokerCount <= availableHeartHost)
                {
                    int currentNumType;
                    if (!isStraight)
                    {
                        currentNumType = PokerNumType.PHost;
                    }
                    else
                    {
                        currentNumType = GetNextNumType(prePile, true);
                        if (currentNumType == PokerNumType.NULL)
                        {
                            currentNumType = PokerNumType.PHost;
                        }
                    }

                    PokerPile      selected = new PokerPile(currentNumType, selectNode.PokerCount);
                    PRVerifyResult valid    = Verify(selectNode, prePile, selected, availableHeartHost);

                    if (!valid.Success)
                    {
                        continue;
                    }

                    if (curSelectIndex < selectChain.Count - 1)
                    {
                        PokerPile result = SelectNormal(
                            isStraight,
                            selectChain,
                            curSelectIndex + 1,
                            selected,
                            availableHeartHost - selectNode.PokerCount,
                            chaiPai,
                            endSelectSize
                            );

                        if (result != null)
                        {
                            selected.Next = result;
                            return(selected);
                        }
                    }
                    else
                    {
                        return(selected);
                    }
                }
            }

            return(null);
        }
예제 #17
0
        public void BuildPokerPool()
        {
            _pokerPool.Clear();
            _pilePool.Clear();
            _xxdd = null;
            _heartHosts.Clear();

            if (_myPokers.Count <= 0)
            {
                return;
            }

            var piles = new Dictionary <int, PokerPile>();

            foreach (var p in _myPokers)
            {
                if (_value.IsHeartHost(p))
                {
                    _heartHosts.Add(p);
                }
                else
                {
                    List <Poker> list = null;
                    if (_pokerPool.ContainsKey(p.NumType))
                    {
                        list = _pokerPool[p.NumType];
                    }

                    if (list == null)
                    {
                        list = new List <Poker>();
                        _pokerPool[p.NumType] = list;
                    }

                    list.Add(p);

                    PokerPile pile = null;
                    if (piles.ContainsKey(p.NumType))
                    {
                        pile = piles[p.NumType];
                    }

                    if (pile == null)
                    {
                        pile             = new PokerPile(p.NumType);
                        piles[p.NumType] = pile;
                    }

                    pile.AddPoker(1);
                }
            }

            foreach (var pp in piles.Values)
            {
                List <PokerPile> list = null;
                if (_pilePool.ContainsKey(pp.Count))
                {
                    list = _pilePool[pp.Count];
                }

                if (list == null)
                {
                    list = new List <PokerPile>();
                    _pilePool[pp.Count] = list;
                }

                var added = false;
                for (int i = 0; i < list.Count; i++)
                {
                    if (_value.ValueOf(pp.NumType) < _value.ValueOf(list[i].NumType))
                    {
                        list.Insert(i, pp);
                        added = true;
                        break;
                    }
                }

                if (!added)
                {
                    list.Add(pp);
                }
            }

            List <PokerPile> piles2 = null;

            if (_pilePool.ContainsKey(2))
            {
                piles2 = _pilePool[2];
            }

            if (piles2 != null && piles2.Count >= 2)
            {
                var pd = piles2[piles2.Count - 1];
                var px = piles2[piles2.Count - 2];

                if (px.NumType == PokerNumType.PX &&
                    pd.NumType == PokerNumType.PD)
                {
                    var pw = new List <PokerPile>();
                    pw.Add(px);
                    pw.Add(pd);
                    px.Next = pd;
                    _xxdd   = px;
                    piles2.Remove(px);
                    piles2.Remove(pd);

                    if (piles2.Count <= 0)
                    {
                        _pilePool.Remove(2);
                    }
                }
            }
        }
예제 #18
0
        public MatchResult MatchHeartHost(PokerPile current, int heartHostCount)
        {
            if (current == null)
            {
                return(new MatchResult(PokerLogic.PatternType.NULL, PokerNumType.NULL));
            }

            if (PokerCount > heartHostCount)
            {
                return(new MatchResult(PokerLogic.PatternType.NULL, PokerNumType.NULL));
            }

            if (PokerCount <= heartHostCount)
            {
                if (heartHostCount % PokerCount != 0)
                {
                    return(new MatchResult(PokerLogic.PatternType.NULL, PokerNumType.NULL));
                }

                int       pileCount = heartHostCount / PokerCount;
                PokerPile cur       = current;
                for (int i = 0; i < pileCount; i++)
                {
                    PokerPile newPile = new PokerPile(PokerNumType.PHost);
                    newPile.AddPoker(PokerCount);
                    cur.Next = newPile;
                    cur      = newPile;
                }

                MatchNode nextMatchNode = this;
                for (int i = 0; i < pileCount - 1; i++)
                {
                    if (nextMatchNode != null)
                    {
                        nextMatchNode = nextMatchNode.Next(PokerCount);
                    }
                    else
                    {
                        break;
                    }
                }

                if (nextMatchNode != null)
                {
                    int majorType = nextMatchNode.IsMajor
                        ? (current.NumType == PokerNumType.NULL ? PokerNumType.PHost : current.NumType)
                        : PokerNumType.NULL;

                    if (majorType != PokerNumType.NULL &&
                        majorType != PokerNumType.PHost &&
                        majorType != PokerNumType.PA &&
                        PokerLogic.PatternType.IsStraight(nextMatchNode.PatternType))
                    {
                        int maxPokerNumber =
                            majorType + PokerLogic.PatternType.GetMajorPileCount(nextMatchNode.PatternType);
                        if (maxPokerNumber > PokerNumType.PA)
                        {
                            majorType = majorType - (maxPokerNumber - PokerNumType.PA - 1);
                        }
                    }
                    return(new MatchResult(nextMatchNode.PatternType, majorType));
                }
                else
                {
                    return(new MatchResult(PokerLogic.PatternType.NULL, PokerNumType.NULL));
                }
            }

            if (OrderedNodes.Count <= 0)
            {
                return(new MatchResult(PokerLogic.PatternType.NULL, PokerNumType.NULL));
            }

            for (int i = 0; i < OrderedNodes.Count; i++)
            {
                MatchResult result = OrderedNodes[i].MatchHeartHost(current, heartHostCount);
                if (!result.IsPatternNull)
                {
                    return(result);
                }
            }

            return(new MatchResult(PokerLogic.PatternType.NULL, PokerNumType.NULL));
        }
예제 #19
0
        public virtual MatchResult Match(PokerPile previous, PokerPile current, int heartHostCount)
        {
            if (PokerCount < current.Count)
            {
                // 匹配节点的数量小于牌堆牌的数量,是无效节点。
                return(new MatchResult(PokerLogic.PatternType.NULL, PokerNumType.NULL));
            }

            if (PokerCount - current.Count > heartHostCount)
            {
                // 主牌数量不够,匹配失败。
                return(new MatchResult(PokerLogic.PatternType.NULL, PokerNumType.NULL));
            }

            PRVerifyResult verifyResult = ValidateRelation(previous, current, PokerCount, heartHostCount);

            if (!verifyResult.Success)
            {
                return(new MatchResult(PokerLogic.PatternType.NULL, PokerNumType.NULL));
            }

            // 关系verify成功,此时需要用剩下的主牌进行匹配。
            int leftHeartHost = heartHostCount - verifyResult.UsedHeartHostCount;
            // 计算在本次verify之间,跳过了多少个匹配节点。
            int stepCount = verifyResult.StepCount;

            // 如果下一个节点存在,则尝试继续匹配。
            PokerPile nextPile = current.Next;

            if (nextPile != null)
            {
                // 用下一个匹配节点检查一下牌堆。
                MatchResult result = new MatchResult(PokerLogic.PatternType.NULL, PokerNumType.NULL);

                // 在下一个节点,以当前的pokerCount为起始,尝试选择匹配节点进行匹配。
                for (int nextPokerCount = PokerCount; nextPokerCount > 0; nextPokerCount--)
                {
                    MatchNode nextMatchNode = null;
                    if (NextNodes.ContainsKey(nextPokerCount))
                    {
                        nextMatchNode = NextNodes[nextPokerCount];
                    }

                    for (int i = 0; i < stepCount; i++)
                    {
                        if (nextMatchNode != null)
                        {
                            nextMatchNode = nextMatchNode.Next(nextPokerCount);
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (nextMatchNode != null)
                    {
                        result = nextMatchNode.Match(current, nextPile, leftHeartHost);
                        if (IsMajor &&
                            !result.IsPatternNull &&
                            !result.IsMajorNumTypeSet)
                        {
                            result.MajorNumType = current.NumType;
                        }
                    }
                    else
                    {
                        result = new MatchResult(PokerLogic.PatternType.NULL, PokerNumType.NULL);
                    }

                    if (!result.IsPatternNull)
                    {
                        break;
                    }
                }

                if (result.IsPatternNull)
                {
                    if (previous != null)
                    {
                        previous.Next = current;
                    }
                    current.RemovePoker(verifyResult.UsedHeartHostCount - (verifyResult.StepCount * PokerCount));
                }

                return(result);
            }
            else
            {
                if (leftHeartHost == 0)
                {
                    MatchNode curMatchNode = this;
                    for (int i = 0; i < stepCount; i++)
                    {
                        if (curMatchNode != null)
                        {
                            curMatchNode = curMatchNode.Next(PokerCount);
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (curMatchNode != null)
                    {
                        return(new MatchResult(curMatchNode.PatternType,
                                               curMatchNode.IsMajor ? current.NumType : PokerNumType.NULL));
                    }
                    else
                    {
                        return(new MatchResult(PokerLogic.PatternType.NULL, PokerNumType.NULL));
                    }
                }
                else
                {
                    MatchResult result = new MatchResult(PokerLogic.PatternType.NULL, PokerNumType.NULL);
                    for (int nextPokerCount = leftHeartHost; nextPokerCount > 0; nextPokerCount--)
                    {
                        MatchNode nextMatchNode = null;
                        if (NextNodes.ContainsKey(nextPokerCount))
                        {
                            nextMatchNode = NextNodes[nextPokerCount];
                        }

                        for (int i = 0; i < stepCount; i++)
                        {
                            if (nextMatchNode != null)
                            {
                                nextMatchNode = nextMatchNode.Next(nextPokerCount);
                            }
                            else
                            {
                                break;
                            }
                        }

                        if (nextMatchNode != null)
                        {
                            result = nextMatchNode.MatchHeartHost(current, leftHeartHost);
                            if (nextMatchNode.IsMajor &&
                                !result.IsPatternNull &&
                                !result.IsMajorNumTypeSet)
                            {
                                result.MajorNumType = current.NumType;
                            }
                        }
                        else
                        {
                            result = new MatchResult(PokerLogic.PatternType.NULL, PokerNumType.NULL);
                        }

                        if (!result.IsPatternNull)
                        {
                            break;
                        }
                    }

                    return(result);
                }
            }
        }
예제 #20
0
        public List <Poker> GetAllPokers(PokerPile pile, bool needFlush)
        {
            var pokers = new List <Poker>();
            var next   = pile;

            int flushSuit = PokerSuitType.NULL;

            if (needFlush)
            {
                var suitCount = new int[] { 0, 0, 0, 0, 0 };
                var suitFound = new Boolean[suitCount.Length];

                while (next != null)
                {
                    for (int i = 0; i < suitFound.Length; i++)
                    {
                        suitFound[i] = false;
                    }

                    List <Poker> list = null;
                    if (_pokerPool.ContainsKey(next.NumType))
                    {
                        list = _pokerPool[next.NumType];
                    }

                    if (list != null)
                    {
                        for (int i = 0; i < list.Count; i++)
                        {
                            suitFound[list[i].SuitType] = true;
                        }
                    }

                    for (int i = 0; i < suitCount.Length; i++)
                    {
                        if (suitFound[i])
                        {
                            suitCount[i]++;
                        }
                    }

                    next = next.Next;
                }

                for (int i = 0; i < suitCount.Length; i++)
                {
                    if (suitCount[i] + _heartHosts.Count >= 5)
                    {
                        flushSuit = PokerSuitType.ValueOf(i);
                    }
                }

                if (flushSuit == PokerSuitType.NULL)
                {
                    return(null);
                }
            }

            next = pile;
            while (next != null)
            {
                List <Poker> list = null;
                if (_pokerPool.ContainsKey(next.NumType))
                {
                    list = _pokerPool[next.NumType];
                }

                if (list != null)
                {
                    int needHeartHost = next.Count - list.Count;
                    if (needHeartHost < 0)
                    {
                        needHeartHost = 0;
                    }

                    if (needHeartHost > _heartHosts.Count)
                    {
                        return(null);
                    }

                    if (flushSuit != PokerSuitType.NULL)
                    {
                        int needCount = next.Count - needHeartHost;
                        var sameSuit  = new List <Poker>();

                        for (int i = 0; i < list.Count; i++)
                        {
                            if (list[i].SuitType == flushSuit)
                            {
                                sameSuit.Add(list[i]);
                            }
                        }

                        if (sameSuit.Count < needCount)
                        {
                            int n = needCount - sameSuit.Count;
                            if (n > _heartHosts.Count)
                            {
                                return(null);
                            }
                            else
                            {
                                for (int c = 0; c < n; c++)
                                {
                                    var p = _heartHosts[0];
                                    sameSuit.Add(p);

                                    _heartHosts.RemoveAt(0);
                                }
                            }
                        }

                        for (int i = 0; i < needCount; i++)
                        {
                            var p = sameSuit[i];
                            sameSuit.RemoveAt(i);
                            list.Remove(p);
                            pokers.Add(p);
                        }
                    }
                    else
                    {
                        for (int i = 0, n = next.Count - needHeartHost; i < n; i++)
                        {
                            var p = list[0];
                            pokers.Add(p);

                            list.RemoveAt(0);
                        }
                    }

                    if (needHeartHost > _heartHosts.Count)
                    {
                        return(null);
                    }

                    for (int i = 0; i < needHeartHost; i++)
                    {
                        var p = _heartHosts[0];
                        pokers.Add(p);

                        _heartHosts.RemoveAt(0);
                    }
                }
                else
                {
                    int needHeartHost = next.Count;
                    if (needHeartHost > _heartHosts.Count)
                    {
                        return(null);
                    }

                    for (int i = 0; i < needHeartHost; i++)
                    {
                        var p = _heartHosts[0];
                        pokers.Add(p);

                        _heartHosts.RemoveAt(0);
                    }
                }

                next = next.Next;
            }

            return(pokers);
        }
예제 #21
0
        public PokerPattern GetSmallestPatternBiggerThan(PokerPattern lastChuPai, bool chaiPai, bool needBomb,
                                                         int endSelectSize)
        {
            BuildPokerPool();

            if (lastChuPai == null ||
                lastChuPai.IsNull ||
                lastChuPai.Type == PatternType.BUCHU)
            {
                return(SelectChuPai());
            }

            if (lastChuPai.Type == PatternType.XXDD)
            {
                // 如果是王炸就没必要再找下去了。
                return(PokerPattern.NULL);
            }

            if (PatternType.IsBomb(lastChuPai.Type))
            {
                if (needBomb)
                {
                    return(SelectBiggerXXXXOrSuperABCDE(lastChuPai, _heartHosts.Count, chaiPai));
                }
                else
                {
                    return(PokerPattern.NULL);
                }
            }

            var selectChain = BuildSelectChain(lastChuPai);

            if (selectChain == null)
            {
                return(PokerPattern.NULL);
            }

            PokerPile result = SelectNormal(PatternType.IsStraight(lastChuPai.Type), selectChain, 0, null,
                                            _heartHosts.Count, chaiPai, endSelectSize);

            if (result != null)
            {
                int majorNumType = result.NumType == PokerNumType.PHost ? _value.CurrentHost : result.NumType;
                result = AdjustPileOrder(result, lastChuPai.Type);
                return(new PokerPattern(
                           lastChuPai.Type,
                           result,
                           majorNumType,
                           GetAllPokers(result, false)
                           ));
            }
            else
            {
                if (needBomb)
                {
                    return(SelectBiggerXXXXOrSuperABCDE(null, _heartHosts.Count, chaiPai));
                }
                else
                {
                    return(PokerPattern.NULL);
                }
            }
        }
예제 #22
0
        public PokerPileChain BuildPPC(List <Poker> pokers)
        {
            List <PokerPile> pileList = new List <PokerPile>();

            List <Poker> pokerList = new List <Poker>();

            if (pokers != null && pokers.Count > 0)
            {
                pokerList.AddRange(pokers);
            }

            List <Poker> heartHosts = RemoveHeartHost(pokerList);

            if (pokerList.Count > 0)
            {
                List <PokerPile> xd = RemoveXD(pokerList);

                Dictionary <int, PokerPile> piles = new Dictionary <int, PokerPile>();
                foreach (var p in pokerList)
                {
                    PokerPile pi = null;

                    if (piles.ContainsKey(p.NumType))
                    {
                        pi = piles[p.NumType];
                    }

                    if (pi == null)
                    {
                        pi = new PokerPile(p.NumType);
                        if (piles.ContainsKey(p.NumType))
                        {
                            piles[p.NumType] = pi;
                        }
                        else
                        {
                            piles.Add(p.NumType, pi);
                        }
                    }

                    pi.AddPoker(1);
                }

                pileList.AddRange(piles.Values);

                if (heartHosts.Count <= 0)
                {
                    ListUtil.QuickSort(pileList, _fullStraightComparer);
                }
                else
                {
                    if (pileList.Count == 2)
                    {
                        if (pokerList.Count + heartHosts.Count == 5)
                        {
                            if (Math.Abs(pileList[0].Count - pileList[1].Count) <= heartHosts.Count)
                            {
                                ListUtil.QuickSort(pileList, _halfComparer);
                            }
                            else
                            {
                                ListUtil.QuickSort(pileList, _fullComparer);
                            }
                        }
                        else if (pokerList.Count + heartHosts.Count == 6)
                        {
                            ListUtil.QuickSort(pileList, _halfStraightComparer);
                        }
                    }
                    else if (pileList.Count >= 3)
                    {
                        ListUtil.QuickSort(pileList, _halfStraightComparer);
                    }
                    else
                    {
                    }
                }

                pileList.AddRange(xd);

                for (int i = 0; i < pileList.Count - 1; i++)
                {
                    pileList[i].Next = pileList[i + 1];
                }
            }

            PokerPile      pile = pileList.Count > 0 ? pileList[0] : null;
            PokerPileChain ppc  = new PokerPileChain(heartHosts.Count);

            ppc.FirstPile = pile;
            return(ppc);
        }