Exemplo n.º 1
0
        private static ShowCardResult ShowCard(IUser user, List <DealResult <DoudizhuUser> > results, ShowCardResult prevShowResult = null)
        {
            var pokers = results.First(x => x.User.Id == user.Id).Pokers;

            Console.WriteLine($"{user.Display},{string.Join(",", pokers.Select(x => $"{x.Display}({x.Key})"))}");
            Console.Write($"[{(user as DoudizhuUser).UserRole.ToString()}]{user.Display}[{pokers.Count}] select pokers:");

            var readString = Console.ReadLine();

            if (string.IsNullOrEmpty(readString))
            {
                //if (prevShowResult == null)
                //{
                //    Console.WriteLine("出牌不符合规则,请重新出!");
                //    ShowCard(user, results);
                //}

                //return new ShowCardResult
                //{
                //    RuleMatcher = new RuleMatchResult { CheckResult = true },
                //    Result = ShowCardResult.ShowCardResultType.NotGiven
                //};
                readString = "P";
            }

            if (readString.ToUpper() == "P")
            {
                if (prevShowResult != null)
                {
                    prevShowResult.Result = ShowCardResult.ShowCardResultType.Pass;
                    return(prevShowResult);
                }
                else
                {
                    Console.WriteLine("头家不能PASS,请重新出!");
                    return(ShowCard(user, results));
                }
            }

            var selectedPokerKeys = readString.Split(",").ToList();
            var selectedPokers    = pokers.Where(x => selectedPokerKeys.Any(y => y == x.Key)).ToList();

            if (selectedPokers.Count() != selectedPokerKeys.Count)
            {
                Console.WriteLine("你恐怕出了你不存在的牌吧,请重新出!");
                return(ShowCard(user, results, prevShowResult));
            }

            if (prevShowResult == null)
            {
                var ruleMatcher = RuleChecker.Check(selectedPokers);
                //验证是否正确
                if (!ruleMatcher.CheckResult)
                {
                    Console.WriteLine("出牌不符合规则,请重新出!");
                    return(ShowCard(user, results));
                }

                //移除用户手中的牌
                pokers.RemoveAll(x => selectedPokerKeys.Any(y => x.Key == y));
                results.First(x => x.User.Id == user.Id).PokerKeys.RemoveAll(x => selectedPokerKeys.Contains(x.PokerKey));

                return(new ShowCardResult
                {
                    RuleMatcher = ruleMatcher,
                    Pokers = selectedPokers,
                    Result = ShowCardResult.ShowCardResultType.Show,
                    User = user
                });
            }

            var currentRule = RuleHelper.BuildBoomRule(selectedPokers);

            if (currentRule == null)
            {
                currentRule = prevShowResult.RuleMatcher.Rule.New(selectedPokers);
            }

            if (!currentRule.Check())
            {
                Console.WriteLine("出牌不符合规则,请重新出!");
                return(ShowCard(user, results, prevShowResult));
            }
            if (currentRule.CompareTo(prevShowResult.RuleMatcher.Rule) <= 0)
            {
                Console.WriteLine("出牌不符合规则,请重新出!");
                return(ShowCard(user, results, prevShowResult));
            }

            //移除用户手中的牌
            pokers.RemoveAll(x => selectedPokerKeys.Any(y => x.Key == y));
            results.First(x => x.User.Id == user.Id).PokerKeys.RemoveAll(x => selectedPokerKeys.Contains(x.PokerKey));

            return(new ShowCardResult
            {
                RuleMatcher = new RuleMatchResult {
                    Rule = currentRule, CheckResult = true
                },
                Result = ShowCardResult.ShowCardResultType.Show,
                Pokers = selectedPokers,
                User = user
            });
        }