Esempio n. 1
0
        static void Main(string[] args)
        {
            List <Soldier> tournament = new List <Soldier>();
            Soldier        warrior1 = new Spearman();
            int            cont1 = -1, cont2 = -1;

            tournament.Add(warrior1);
            warrior1 = new Spearman();
            tournament.Add(warrior1);
            warrior1 = new Legionary();
            tournament.Add(warrior1);
            warrior1 = new Legionary();
            tournament.Add(warrior1);
            warrior1 = new Cataphract();
            tournament.Add(warrior1);
            warrior1 = new Cataphract();
            tournament.Add(warrior1);


            foreach (Soldier warrior in tournament)
            {
                warrior.SayHello();
            }
            Console.WriteLine();
            Console.WriteLine(new string('-', 30));
            Console.WriteLine("\nSoldiers sorted by damage: ");
            tournament.Sort();
            foreach (Soldier warrior in tournament)
            {
                warrior.ShowInfo();
            }
            Console.WriteLine();
            Console.WriteLine(new string('-', 30));
            Console.WriteLine("\nSoldiers sorted by id: ");
            tournament.Sort(new IdComparer());
            foreach (Soldier warrior in tournament)
            {
                warrior.ShowInfo();
            }


            while (tournament.Count > 1)
            {
                Console.WriteLine("\nWho do you want to see fighting for glory? Enter the number of the first contestant! (not id)");
                while (true)
                {
                    try
                    {
                        cont1 = int.Parse(Console.ReadLine());
                    }
                    catch
                    {
                        Console.WriteLine("Wrong input!");
                    }
                    if (cont1 < 1 || cont1 > tournament.Count)
                    {
                        Console.WriteLine("Try again");
                    }
                    else
                    {
                        break;
                    }
                }
                Console.WriteLine("Enter the number of the second contestant! (not id)");
                while (true)
                {
                    try
                    {
                        cont2 = int.Parse(Console.ReadLine());
                    }
                    catch
                    {
                        Console.WriteLine("Wrong input!");
                    }
                    if (cont2 < 1 || cont2 > tournament.Count || cont2 == cont1)
                    {
                        Console.WriteLine("Try again");
                    }
                    else
                    {
                        break;
                    }
                }
                Console.WriteLine("Let's see them fight!");
                cont1--;
                cont2--;

                int count = 0;

                while (tournament[cont1].IsAlive && tournament[cont2].IsAlive)
                {
                    count++;

                    tournament[cont2].GetHit(tournament[cont1].Attack()); if (count == 10)
                    {
                        tournament[cont2].IsAlive = false;
                        Console.WriteLine("\nThe warriors got tired and the second contestant gave up");
                    }
                    Console.WriteLine();
                    if (tournament[cont2].IsAlive)
                    {
                        tournament[cont1].GetHit(tournament[cont2].Attack());
                    }
                }
                if (!tournament[cont1].IsAlive)
                {
                    tournament.RemoveAt(cont1);
                }
                else
                if (!tournament[cont2].IsAlive)
                {
                    tournament.RemoveAt(cont2);
                }
                if (tournament.Count < 2)
                {
                    Console.WriteLine("HEY!\n Seems like you running out fighters, aren't you?\nWanna add some more fresh meat?\n\n" +
                                      "1) Yes\n" +
                                      "Any other key) No");
                    if (Console.ReadKey(true).KeyChar == '1')
                    {
                        bool quit = false;
                        while (!quit)
                        {
                            Console.WriteLine("1)  Spearman\n" +
                                              "2)  Legionary\n" +
                                              "3)  Cataphract\n");
                            switch (Console.ReadKey(true).KeyChar)
                            {
                            case '1':
                            {
                                warrior1 = new Spearman();
                                warrior1.ShowInfo();
                                warrior1.SayHello();
                                tournament.Add(warrior1);
                                quit = true;
                                break;
                            }

                            case '2':
                            {
                                warrior1 = new Legionary();
                                warrior1.ShowInfo();
                                warrior1.SayHello();
                                tournament.Add(warrior1);
                                quit = true;
                                break;
                            }

                            case '3':
                            {
                                warrior1 = new Cataphract();
                                warrior1.ShowInfo();
                                warrior1.SayHello();
                                tournament.Add(warrior1);
                                quit = true;
                                break;
                            }

                            default:
                            {
                                break;
                            }
                            }
                        }
                    }
                }
                Console.WriteLine("\n" + new string('-', 30) + "\n");
                foreach (Soldier soldier in tournament)
                {
                    soldier.ShowInfo();
                }
            }
            Console.WriteLine("\nAND THIS IS THE WINNER!");
            Console.ReadKey();
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Random         rand       = new Random();
            List <Soldier> tournament = new List <Soldier>();
            Soldier        warrior;
            int            cont1 = -1, cont2 = -1;

            for (int i = 0; i < 3; i++)
            {
                if (rand.Next(0, 10) == 0)
                {
                    warrior         = new Spearman("Garold", 22, Human.Genders.Male, 100, 100, 100, 100, Soldier.Qualities.Heavy, Soldier.Kingdoms.Empire);
                    warrior.IsAlive = false;
                }
                else
                {
                    warrior = new Spearman();
                }
                if (warrior.IsAlive)
                {
                    tournament.Add(warrior);
                }
                else
                {
                    throw new InvalidOperationException("You can't add a dead warrior to the tournament list..");
                }
                warrior = new Legionary();
                tournament.Add(warrior);
                warrior = new Cataphract();
                tournament.Add(warrior);
            }

            Console.WriteLine("Here is the warriors participating in the tournament:");

            foreach (Soldier soldier in tournament)
            {
                soldier.SayHello();
            }
            Console.WriteLine("\n" + new string('-', 30));
            Console.WriteLine("\nSoldiers sorted by damage: ");
            tournament.Sort();
            foreach (Soldier soldier in tournament)
            {
                soldier.ShowInfo();
            }
            Console.WriteLine("\n" + new string('-', 30));
            Console.WriteLine("\nSoldiers sorted by id: ");
            tournament.Sort(new IdComparer());
            foreach (Soldier soldier in tournament)
            {
                soldier.ShowInfo();
            }


            while (tournament.Count > 1)
            {
                Console.WriteLine("\nWho do you want to see fighting for glory? Enter the number of the first contestant! (not id)");
                while (true)
                {
                    try
                    {
                        cont1 = int.Parse(Console.ReadLine());
                    }
                    catch
                    {
                        Console.WriteLine("Wrong input!");
                    }
                    if (cont1 < 1 || cont1 > tournament.Count)
                    {
                        Console.WriteLine("Try again");
                    }
                    else
                    {
                        break;
                    }
                }
                Console.WriteLine("Enter the number of the second contestant! (not id)");
                while (true)
                {
                    try
                    {
                        cont2 = int.Parse(Console.ReadLine());
                    }
                    catch
                    {
                        Console.WriteLine("Wrong input!");
                    }
                    if (cont2 < 1 || cont2 > tournament.Count || cont2 == cont1)
                    {
                        Console.WriteLine("Try again");
                    }
                    else
                    {
                        break;
                    }
                }
                Console.WriteLine("Let's see them fight!");
                cont1--;
                cont2--;
                int count = 0;

                while (tournament[cont1].IsAlive && tournament[cont2].IsAlive)
                {
                    count++;
                    tournament[cont2].GetHit(tournament[cont1].Attack());
                    if (count == 10)
                    {
                        if (tournament[cont2].Curhp < tournament[cont1].Curhp)
                        {
                            tournament[cont2].IsAlive = false;
                            Console.WriteLine("\nThe warriors got tired and the second contestant gave up");
                        }
                        else
                        {
                            tournament[cont1].IsAlive = false;
                            Console.WriteLine("\nThe warriors got tired and the first contestant gave up");
                        }
                    }
                    Console.WriteLine();
                    if (tournament[cont2].IsAlive)
                    {
                        tournament[cont1].GetHit(tournament[cont2].Attack());
                    }
                    Thread.Sleep(500);
                }
                if (!tournament[cont1].IsAlive)
                {
                    tournament[cont1].Died += delegate(Soldier dead)
                    {
                        Console.WriteLine(dead.Name + " (" + dead.Age + "): AAARGH!...\n");
                    };
                    tournament[cont1].OnDied(tournament[cont1]);
                    tournament.RemoveAt(cont1);
                }
                else
                if (!tournament[cont2].IsAlive)
                {
                    tournament[cont2].Died += delegate(Soldier dead)
                    {
                        Console.WriteLine(dead.Name + " (" + dead.Age + "): AAARGH!...\n");
                    };
                    tournament[cont2].OnDied(tournament[cont2]);
                    tournament.RemoveAt(cont2);
                }
                if (tournament.Count < 2)
                {
                    Console.WriteLine("HEY!\n Seems like you are running out fighters, aren't you?\nWanna add some more fresh meat?\n\n" +
                                      "1) Yes\n" +
                                      "Any other key) No, let's and this bloodshee.., I mean tournament");
                    if (Console.ReadKey(true).KeyChar == '1')
                    {
                        bool quit = false;
                        while (!quit)
                        {
                            Console.WriteLine("1)  Spearman\n" +
                                              "2)  Legionary\n" +
                                              "3)  Cataphract\n");
                            switch (Console.ReadKey(true).KeyChar)
                            {
                            case '1':
                            {
                                warrior = new Spearman();
                                warrior.ShowInfo();
                                warrior.SayHello();
                                tournament.Add(warrior);
                                quit = true;
                                break;
                            }

                            case '2':
                            {
                                warrior = new Legionary();
                                warrior.ShowInfo();
                                warrior.SayHello();
                                tournament.Add(warrior);
                                quit = true;
                                break;
                            }

                            case '3':
                            {
                                warrior = new Cataphract();
                                warrior.ShowInfo();
                                warrior.SayHello();
                                tournament.Add(warrior);
                                quit = true;
                                break;
                            }

                            default:
                            {
                                break;
                            }
                            }
                        }
                    }
                }

                Console.WriteLine("\n" + new string('-', 30) + "\n");
                foreach (Soldier soldier in tournament)
                {
                    soldier.ShowInfo();
                }
            }
            //EVENT WITH LAMBDA-EXPRESSION CALLING
            Soldier.Win += (dead) => Console.WriteLine("\n" + new string('-', 30) + "\n\n" + dead.Name + " (" + dead.Age + "): HOOORAAAAY!!! I am the WINNER in todays tournament!!!!" + "\n\n" + new string('-', 30));
            Soldier.OnWin(tournament[0]);
            Console.ReadKey();
        }