示例#1
0
        public void When_KingCrowned_Expect_True()
        {
            // Arrange
            // Setting all kingdoms except water to compete
            var testGameEngine = new GameEngine(consoleMethods);

            testGameEngine.AllKingdoms[Kingdoms.Air].IsCompeting   = true;
            testGameEngine.AllKingdoms[Kingdoms.Fire].IsCompeting  = true;
            testGameEngine.AllKingdoms[Kingdoms.Ice].IsCompeting   = true;
            testGameEngine.AllKingdoms[Kingdoms.Land].IsCompeting  = true;
            testGameEngine.AllKingdoms[Kingdoms.Space].IsCompeting = true;
            // Updating message list to only contain valid message for water
            // So that we are sure to have a winner in the very first round
            Utility.listOfMessages.Clear();
            Utility.listOfMessages.Add("octopus");
            var highPriest = new HighPriest(6, 100, consoleMethods);

            // Act
            highPriest.HoldBallot(testGameEngine);

            // Explicitly call Utility class' static constructor so as to revert changes to listOfMessages
            System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(Utility).TypeHandle);

            // Assert
            Assert.NotNull(testGameEngine.RulingKingdom);
        }
        /// <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);
        }