예제 #1
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);
            }
        }
예제 #2
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);
                }
            }
        }