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