/// <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); } } }
/// <summary> /// Finds the ruler of Southeros using a ballot system /// </summary> /// <param name="southeros">Reference to a ISoutheros implemented class object</param> /// <returns>Returns a boolean denoting whether a king was crowned or not</returns> public bool HoldBallot(ISoutheros southeros) { // Randomizer to choose message to write var rnd = new Random(); //Ballot round int round = 0; // The drawing from the ballot goes on until a winner is decided or we reach maxRounds while (southeros.RulingKingdom == null && ++round <= maxRounds) { // Get a new ballot box var ballotBox = new BallotBox(); // Round up all kingdoms competing for the throne var competingKingdoms = southeros.AllKingdoms.Where(kv => kv.Value.IsCompeting).Select(kv => kv.Key).ToList(); // Each competing kingdom composes messages... foreach (var competitor in competingKingdoms) { // ...for all the other kingdoms foreach (var reciver in southeros.AllKingdoms.Keys) { // ...except itself ofcourse ¯\_(ツ)_/¯ if (reciver == competitor) { continue; } var newRequest = new Message(competitor, reciver, Utility.listOfMessages[rnd.Next(Utility.listOfMessages.Count)]); ballotBox.DropMessage(newRequest); } } // The High Priest of Southeros chooses the messages to send out var choosenMessages = PickRandomMessages(ballotBox); // Hand over the messages foreach (var message in choosenMessages) { southeros.AllKingdoms[message.Sender].SendMessage(southeros.AllKingdoms[message.Recipient], message); } // Declare results console.WriteLine($"{Environment.NewLine}Results after round {Utility.IntegerToWritten(round)} ballot count"); // Variable to store max allies by any kingdom var maxAllies = 0; foreach (var competitor in competingKingdoms) { var allyCount = southeros.AllKingdoms[competitor].Allies.Count; console.WriteLine($"Allies for {competitor} : {allyCount}"); maxAllies = allyCount > maxAllies ? allyCount : maxAllies; } // Find the kingdom(s) with the most allies var leadingKingdoms = southeros.AllKingdoms.Where(kv => kv.Value.IsCompeting && kv.Value.Allies.Count == maxAllies); // If there is a tie if (leadingKingdoms.Count() > 1) { // Clear the list of old competitors competingKingdoms.Clear(); // Create a new list comprising of the kingdoms there was a tie between competingKingdoms.AddRange(leadingKingdoms.Select(kv => kv.Key)); // Break all alliances foreach (var kingdom in southeros.AllKingdoms.Values) { kingdom.BreakAlliances(); } } // If not... else { // ...we declare the winner southeros.RulingKingdom = leadingKingdoms.FirstOrDefault().Value; } } return(southeros.RulingKingdom != null); }
public void GetData() { console.WriteLine("Please Enter your Name(only Alphabet)"); _name = console.ReadLine(); console.WriteLine(_name); }