/// <summary>
        /// Custom Input Action for the 'Breaker of Chains' problem
        /// </summary>
        /// <param name="input">The user input which triggered the custom action</param>
        /// <param name="southeros"></param>
        /// <returns></returns>
        /// <remarks>We don't particularly require the input data in this function but we keep it anyway to match the function signature</remarks>
        public string CustomInputAction(string input, ISoutheros southeros)
        {
            // Once a ruler has been found, we no longer need to hold a ballot
            if (southeros.RulingKingdom != null)
            {
                return(string.Format(Utility.RulerCrownedMessage, southeros.RulingKingdom.Name));
            }

            // Declare local variables
            var output           = string.Empty;
            var noOfCompetitors  = 0;
            var maxBallotRounds  = 1000;
            var messagesToChoose = 6;

            // Read the list of competitors from the user
            var possibleCandidates = console.ReadLine().Trim().Replace(',', ' ').Split(" ", StringSplitOptions.RemoveEmptyEntries);

            // Try parsing the user input into valid kingdoms
            foreach (var candidate in possibleCandidates)
            {
                if (!string.IsNullOrWhiteSpace(candidate) &&
                    Enum.TryParse(candidate, true, out Kingdoms competingKingdom))
                {
                    southeros.AllKingdoms[competingKingdom].IsCompeting = true;
                    noOfCompetitors++;
                }
                else
                {
                    output += string.Format(Utility.InvalidKingdomMessage, candidate);
                }
            }

            // We need atleast two kingdoms competing for the crown
            if (noOfCompetitors < 2)
            {
                output += Utility.NoCompetingKingdomsMessage;
            }
            // and atmost one less than all of them
            else if (noOfCompetitors < Enum.GetValues(typeof(Kingdoms)).Length)
            {
                // Summon high priest
                var highPriest = new HighPriest(messagesToChoose, maxBallotRounds, console);
                // FindRulerByBallot return false if we don't have a ruler after maxBallotRounds

                if (!highPriest.HoldBallot(southeros))
                {
                    output += Utility.BallotTookTooLongMessage;
                }
            }
            // If all the kingdoms compete, we can't proceed
            else
            {
                output += Utility.TooManyKingdomsMessage;
            }

            return(output);
        }
Example #2
0
        /// <summary>
        /// This will run the actual program.
        /// </summary>
        /// <remarks>It will keep reading input from the user until they type exit</remarks>
        public void Execute()
        {
            // Variable to store the input
            string input;

            // Keep reading input from the user unit they enter "exit"
            while (!(input = console.ReadLine().Trim().ToLower()).Contains("exit"))
            {
                // Variable to store the output from the user
                var output = string.Empty;
                if (!string.IsNullOrWhiteSpace(input))
                {
                    output = ProcessInput(input);
                }

                if (!string.IsNullOrWhiteSpace(output))
                {
                    console.WriteLine(output);
                }
            }
        }
 public void GetData()
 {
     console.WriteLine("Please Enter your Name(only Alphabet)");
     _name = console.ReadLine();
     console.WriteLine(_name);
 }