Example #1
0
        static void Main(string[] args)
        {
            //This loop will control the player's hand
            do
            {
                int playersIncluded = 0;

                List <Class.HandAnalyzer> handAnalyzer = new List <Class.HandAnalyzer>();

                //Control how many players wants to be included
                Console.WriteLine("How many players do you want to include? (2~10)");
                var entry = Console.ReadLine();

                bool number = int.TryParse(entry, out int qtHand);

                //Control if the user included a number and the quantity allowed
                if (number == false || (qtHand < 2 || qtHand > 10))
                {
                    Console.WriteLine("\nInvalid number. To try again press any key, to exit press ESC.");
                }
                else
                {
                    try
                    {
                        //This loop will receive and analyze each player's hand
                        do
                        {
                            playersIncluded += 1;

                            Console.WriteLine(string.Concat("Player-", playersIncluded.ToString(), ":"));
                            Class.HandAnalyzer PlayerHand = new Class.HandAnalyzer(Console.ReadLine());
                            handAnalyzer.Add(PlayerHand);
                        } while (playersIncluded < qtHand);

                        Console.WriteLine(Environment.NewLine);

                        //IF the user included only wrong hands
                        if (handAnalyzer.Where(c => c.qError == 0).Count() == 0)
                        {
                            Console.WriteLine("All players are disqualified.");
                        }
                        else
                        {
                            Console.WriteLine(string.Format($"The Winner(s) is/are: {WhoWin(handAnalyzer)}. With a(n) {NameOfGame(Game)}"));
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    Console.WriteLine(Environment.NewLine);

                    Console.WriteLine("\nTo play again press any key, to exit press ESC.");
                }
            } while (Console.ReadKey(true).Key != ConsoleKey.Escape);
        }
Example #2
0
        /// <summary>
        /// Compare games and decide who is the winner and if had a tie
        /// </summary>
        /// <param name="handAnalyzer"></param>
        /// <returns></returns>
        public static string WhoWin(IList <Class.HandAnalyzer> handAnalyzer)
        {
            StringBuilder result = new StringBuilder();

            //Order the hands to take the biggest one and remove the players with errors
            handAnalyzer = handAnalyzer.OrderByDescending(c => c.Hand).Where(c => c.qError == 0).ToList();

            Class.HandAnalyzer win = handAnalyzer.FirstOrDefault();

            if (win.Hand == Enums.Hand.HighCard)
            {
                handAnalyzer = handAnalyzer.OrderByDescending(c => c.HighCard()).ToList();
                win          = handAnalyzer.FirstOrDefault();
            }

            result.Append(win.PlayerName);
            Game = win.Hand.ToString();

            for (int i = 1; i < handAnalyzer.Count(); i++)
            {
                if (win.Hand == Enums.Hand.HighCard)
                {
                    if (handAnalyzer[i].HighCard() == win.HighCard())
                    {
                        result.AppendFormat(", {0}", handAnalyzer[i].PlayerName);
                        Game = handAnalyzer[i].Hand.ToString();
                    }
                }
                else if (handAnalyzer[i].Hand == win.Hand)
                {
                    result.AppendFormat(", {0}", handAnalyzer[i].PlayerName);
                    Game = handAnalyzer[i].Hand.ToString();
                }
                else
                {
                    break;
                }
            }

            return(result.ToString());
        }