public static bool IsOver(this Battle battle)
        {
            var hero = battle.GetHero();

            if (hero == null || hero.Unit.IsDead())
            {
                return(true);
            }

            if (battle.GetEnemies().Count() == 0)
            {
                return(true);
            }

            return(false);
        }
Example #2
0
        public static void Main(string[] args)
        {
            const ConsoleKey CreateNewHero    = ConsoleKey.C;
            const ConsoleKey LoadSavedHeroKey = ConsoleKey.L;

            Console.Clear();
            Console.WriteLine("Menu");
            Console.WriteLine();
            Console.WriteLine($"{CreateNewHero} - Create a new hero");
            Console.WriteLine($"{LoadSavedHeroKey} - Load a saved hero");

            var pressedKey = KeyPresser.WaitFor(new[] { CreateNewHero, LoadSavedHeroKey });

            Unit hero;

            switch (pressedKey)
            {
            case CreateNewHero: hero = Program.CreateNewHero();
                break;

            case LoadSavedHeroKey: hero = LoadSavedHero();
                break;

            default:
                throw new Exception();
            }

            var unitFactory = _container.GetInstance <UnitFactory>();

            var units = new Unit[]
            {
                hero,
                unitFactory.Create("Boris the Brutal"),
                unitFactory.Create("Steve the Shrew"),
                unitFactory.Create("Gary the Goblin")
            };

            var battle = new Battle(units);

            Console.Clear();
            Console.WriteLine($"You are confronted by enemies.");
            Console.WriteLine();

            do
            {
                var attacker = battle.DequeueUnit();

                if (attacker.Unit.Team == Team.Hero)
                {
                    if (attacker.Unit.HasPotions())
                    {
                        const ConsoleKey DrinkPotionKey = ConsoleKey.D;
                        const ConsoleKey AttackKey      = ConsoleKey.A;

                        Console.WriteLine("Choose a turn");
                        Console.WriteLine($"Hit {DrinkPotionKey} to drink a potion. {attacker.Unit.Name}'s health: {attacker.Unit.Health}.");
                        Console.WriteLine($"Hit {AttackKey} to attack an enemy.");
                        Console.WriteLine();

                        var key = KeyPresser.WaitFor(new[] { DrinkPotionKey, AttackKey });
                        Console.WriteLine();

                        switch (key)
                        {
                        case DrinkPotionKey:
                            AttackerDrinksPotion(attacker.Unit);
                            break;

                        case AttackKey:
                            var enemy = GetDefenderToAttack(battle);
                            AttackerAttacksDefender(attacker, enemy, battle);
                            break;

                        default:
                            throw new Exception();
                        }
                    }
                    else
                    {
                        var enemy = GetDefenderToAttack(battle);
                        AttackerAttacksDefender(attacker, enemy, battle);
                    }
                }
                else if (attacker.Unit.Team == Team.Enemy)
                {
                    AttackerAttacksDefender(attacker, battle.GetHero(), battle);
                }
                else
                {
                    throw new Exception();
                }

                battle.QueueUnit(attacker);
            }while (!battle.IsOver());

            Console.WriteLine();
            Console.WriteLine("The battle is over.");

            if (hero.IsAlive())
            {
                Console.WriteLine();
                Console.WriteLine("S - Save");
                KeyPresser.WaitFor(ConsoleKey.S);

                var gameSaver = _container.GetInstance <GameSaver>();
                gameSaver.OverwriteSave(hero);

                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine("Game Saved.");
            }

            Console.ReadKey();
        }