Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            string kingName = Console.ReadLine();
            King   king     = new King(kingName);

            List <Soldier> soldiers = new List <Soldier>();

            GetSoldiers(king, "Royal");
            GetSoldiers(king, "Footman");

            string input = string.Empty;

            while ((input = Console.ReadLine()) != "End")
            {
                string[] commandArgs = input.Split();
                string   command     = commandArgs[0];

                switch (command)
                {
                case "Attack":
                    king.OnAttack();
                    break;

                case "Kill":
                    string soldierName = commandArgs[1];
                    var    soldier     = king.Soldiers.FirstOrDefault(s => s.Name == soldierName);
                    soldier.TakeAttack();
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        public static void Main()
        {
            string kingName = Console.ReadLine();

            string[] guardsNames  = Console.ReadLine().Split();
            string[] footmenNames = Console.ReadLine().Split();

            King           king     = new King(kingName);
            List <Soldier> soldiers = new List <Soldier>();

            foreach (var name in guardsNames)
            {
                RoyalGuard guard = new RoyalGuard(name);
                soldiers.Add(guard);
                king.UnderAttack += guard.KingUnderAttack;
            }

            foreach (var name in footmenNames)
            {
                Footman footman = new Footman(name);
                soldiers.Add(footman);
                king.UnderAttack += footman.KingUnderAttack;
            }

            while (true)
            {
                string[] input = Console.ReadLine().Split();

                if (input[0] == "End")
                {
                    break;
                }
                else if (input[0] == "Kill")
                {
                    Soldier soldier = soldiers.Find(s => s.Name == input[1]);
                    soldiers.Remove(soldier);
                    king.UnderAttack -= soldier.KingUnderAttack;
                }
                else if (input[0] == "Attack")
                {
                    king.OnAttack();
                }
            }
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            string kingName     = Console.ReadLine();
            var    guardsNames  = Console.ReadLine().Split(' ').ToList();
            var    footmenNames = Console.ReadLine().Split(' ').ToList();

            var king     = new King(kingName);
            var servants = new List <Servant>();

            guardsNames.ForEach(n => servants.Add(new RoyalGuard(n)));
            footmenNames.ForEach(n => servants.Add(new Footman(n)));
            servants.ForEach(s => king.Attacked += s.React);

            while (true)
            {
                string line = Console.ReadLine();
                if (line == "End")
                {
                    break;
                }

                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }

                try
                {
                    var    inputs  = line.Split(' ');
                    string command = inputs[0];
                    string target  = inputs[1];

                    switch (command)
                    {
                    case "Attack":
                    {
                        if (target != "King")
                        {
                            throw new Exception("You can only attack the king.");
                        }

                        king.OnAttack();
                        break;
                    }

                    case "Kill":
                    {
                        var servant = servants.SingleOrDefault(s => s.Name == target);

                        if (servant == null)
                        {
                            throw new Exception($"Failed to find a single servant {target} to kill.");
                        }

                        servant.Die();
                        break;
                    }

                    default: throw new Exception("Invalid command.");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            Console.ReadKey();
        }