Example #1
0
        public Fight(Hero hero, Enemy enemy)
        {
            this.hero = hero;
            this.enemy = enemy;

            while (this.hero.IsAlive() || this.enemy.IsAlive())
            {
                if (hero.Weapon != null)
                {
                    HeroAttack("weapon");
                }
                HeroAttack("spell");

                if (enemy.Weapon != null)
                {
                    EnemyAttack("weapon");
                }
                else if(enemy.Spell != null)
                {
                    EnemyAttack("spell");
                }
                EnemyAttack(null);
            }
            if (hero.IsAlive())
            {
                Console.WriteLine("Hero won!");
            }
            else
            {
                Console.WriteLine("Enemy won!");
            }
        }
        /// <summary>
        /// Main method.
        /// </summary>
        public static void Main()
        {
            // TODO : add fights feature
            // TODO : combat method for fights
            // TODO : enemy adding order ?
            // TODO : check addition of new characters, weapons and spells in the game
            // TODO : add validation
            // TODO : refactor with build methods (take damage, take healing etc)
            // TODO : remove magic numbers
            // TODO : bugfixing
            // TODO : dungeon class violates SRP ?
            var defaultHero = new Hero("John", "Jedi", 100, 100, 2);
            var defaultWeapon = new Weapon("Default weapon", 100);
            var defaultSpell = new Spell("Default spell", 120, 50, 2);
            List<Enemy> enemiesList = new List<Enemy>();
            bool isSpawned = false;

            defaultHero.Learn(defaultSpell);
            defaultHero.Equip(defaultWeapon);

            string text = File.ReadAllText(@"C:\Users\Viktor\Desktop\GitHub\HackBulgaria-CSharp\Week 5\OOPExercises\DungeonsAndLizards\game settings\map.txt");
            var dungeon = new Dungeon(text);

            Console.WriteLine("Welcome to Dungeons and Lizards Game !");
            Console.WriteLine("To check game rules enter \"help\"");

            // TODO : equip enemies/heroes with different spells and weapons?
            int enemiesCounter = text.Count(character => character == 'E');

            while (true)
            {
                Console.Write("Enter a command: ");
                string input = Console.ReadLine();
                string[] word = input.Split(' ');
                if (Validation.cs.CharacterValidation.IsInputLongerThan2Words(input))
                {
                    switch (word[0])
                    {
                        case "print":
                            switch (word[1])
                            {
                                case "map":
                                    dungeon.PrintMap();
                                    break;
                                default:
                                    Console.WriteLine("Did you mean \"print map\"?");
                                    break;
                            }

                            break;
                        case "create":
                            switch (word[1])
                            {
                                case "hero":
                                    if (Validation.cs.CharacterValidation.IsLengthWordsLong(input, 7)
                                        && Validation.cs.CharacterValidation.IsWordParsableToInt(input, new[] { 4, 5, 6 })
                                        && Validation.cs.Stats.IsValuePositive(new[] { int.Parse(word[4]), int.Parse(word[5]), int.Parse(word[6]) }))
                                    {
                                        Hero hero = new Hero(word[2], word[3], int.Parse(word[4]), int.Parse(word[5]), int.Parse(word[6]));
                                        defaultHero = hero;
                                        Console.WriteLine("SUCCESS: Hero created !");
                                    }

                                    break;
                                case "enemy":
                                    if (Validation.cs.CharacterValidation.IsLengthWordsLong(input, 5)
                                        && Validation.cs.CharacterValidation.IsWordParsableToInt(input, new[] { 2, 3, 4 })
                                        && Validation.cs.Stats.IsValuePositive(new[] { int.Parse(word[2]), int.Parse(word[3]), int.Parse(word[4]) }))
                                    {
                                        if (enemiesCounter > enemiesList.Count)
                                        {
                                            Enemy enemy = new Enemy(int.Parse(word[2]), int.Parse(word[3]), int.Parse(word[4]));
                                            enemiesList.Add(enemy);
                                            Console.WriteLine("SUCCESS: Enemy created !");
                                        }
                                        else
                                        {
                                            Console.WriteLine("ERROR: You cannot add more enemies because there are too few on the map !");
                                        }
                                    }

                                    break;
                                default:
                                    Console.WriteLine("ERROR: Command \"create\" take \"hero\" or \"enemy\" as parameters !");
                                    break;
                            }

                            break;
                        case "add":
                            switch (word[1])
                            {
                                case "weapon":
                                    if (Validation.cs.CharacterValidation.IsLengthWordsLong(input, 4)
                                        && Validation.cs.CharacterValidation.IsWordParsableToInt(input, new[] { 3 })
                                        && Validation.cs.Stats.IsValuePositive(int.Parse(word[3])))
                                    {
                                        Weapon weapon = new Weapon(word[2], int.Parse(word[3]));
                                        defaultWeapon = weapon;
                                        Console.WriteLine("SUCCESS: Weapon added !");
                                    }

                                    break;
                                case "spell":
                                    if (Validation.cs.CharacterValidation.IsLengthWordsLong(input, 6)
                                        && Validation.cs.CharacterValidation.IsWordParsableToInt(input, new[] { 3, 4, 5 })
                                        && Validation.cs.Stats.IsValuePositive(new[] { int.Parse(word[3]), int.Parse(word[4]), int.Parse(word[4]) }))
                                    {
                                        Spell spell = new Spell(word[2], int.Parse(word[3]), int.Parse(word[4]), int.Parse(word[5]));
                                        defaultSpell = spell;
                                        Console.WriteLine("SUCCESS: Spell added !");
                                    }

                                    break;
                                default:
                                    Console.WriteLine("ERROR: Command \"add\" takes \"weapon\" or \"spell\" as parameters !");
                                    break;
                            }

                            break;
                        case "equip":
                            switch (word[1])
                            {
                                case "hero":
                                    if (Validation.cs.CharacterValidation.IsLengthWordsLong(input, 2))
                                    {
                                        defaultHero.Equip(defaultWeapon);
                                        Console.WriteLine("SUCCESS: Hero equipped !");
                                    }

                                    break;
                                case "enemy":
                                    if (Validation.cs.CharacterValidation.IsLengthWordsLong(input, 2))
                                    {
                                        enemiesList[0].Equip(defaultWeapon);
                                        Console.WriteLine("SUCCESS: Enemy equipped !");
                                    }

                                    break;
                                default:
                                    Console.WriteLine("ERROR: Command \"equip\" takes \"hero\" or \"enemy\" as parameters !");
                                    break;
                            }

                            break;
                        case "learn":
                            switch (word[1])
                            {
                                case "hero":
                                    defaultHero.Learn(defaultSpell);
                                    Console.WriteLine("SUCCESS: {0} learned {1} spell", defaultHero.Name, defaultSpell.Name);
                                    break;
                                case "enemy":
                                    enemiesList[0].Learn(defaultSpell);
                                    Console.WriteLine("SUCCESS: {0} learned {1} spell", enemiesList[0].Name, defaultSpell.Name);
                                    break;
                                default:
                                    Console.WriteLine("ERROR: Command \"learn\" takes \"hero\" or \"enemy\" as parameters !");
                                    break;
                            }

                            break;
                        case "can":
                            if (word[1] == "cast")
                            {
                                Console.WriteLine("Hero can cast {0} : {1}", defaultSpell, defaultHero.CanCast(defaultSpell));
                            }

                            break;
                        case "attack":
                            switch (word[1])
                            {
                                case "weapon":
                                case "spell":
                                    dungeon.HeroAttack(word[1]);
                                    break;
                                default:
                                    Console.WriteLine("ERROR: Command \"attack\" takes \"weapon\" or \"spell\" as parameters");
                                    break;
                            }

                            break;
                        case "move":
                            if (isSpawned)
                            {
                                switch (word[1])
                                {
                                    case "right":
                                        dungeon.MoveHero(Direction.Right);
                                        break;
                                    case "left":
                                        dungeon.MoveHero(Direction.Left);
                                        break;
                                    case "up":
                                        dungeon.MoveHero(Direction.Up);
                                        break;
                                    case "down":
                                        dungeon.MoveHero(Direction.Down);
                                        break;
                                    default:
                                        Console.WriteLine("ERROR: command \"move\" can be used only with \"up\", \"down\", \"left\" or \"right\"");
                                        break;
                                }
                            }
                            else
                            {
                                Console.WriteLine("ERROR: Hero not spawned !");
                            }

                            break;
                        default:
                            Console.WriteLine("ERROR: Unknown command \"{0}\"", input);
                            break;
                    }
                }
                else
                {
                    switch (word[0])
                    {
                        case "help":
                            Console.WriteLine("List of commands : ");
                            Console.WriteLine("Replace \"<>\" with desired stats");
                            Console.WriteLine("----------------------------------------");
                            Console.WriteLine("create hero <hero name> <hero class> <hero heatlh> <hero mana> <hero mana regeneration>");
                            Console.WriteLine("create enemy <enemy health> <enemy mana> <enemy damage>");
                            Console.WriteLine("add weapon <weapon name> <weapon damage>");
                            Console.WriteLine("add spell <spell name> <spell damage> <spell manacost> <spell range>");
                            Console.WriteLine("equip hero");
                            Console.WriteLine("equip enemy");
                            Console.WriteLine("learn hero");
                            Console.WriteLine("learn enemy");
                            Console.WriteLine("known as");
                            Console.WriteLine("is alive");
                            Console.WriteLine("can cast");
                            Console.WriteLine("attack");
                            Console.WriteLine("print map");
                            Console.WriteLine("move <any of the 4 directions>");
                            Console.WriteLine("exit/quit");
                            Console.WriteLine("----------------------------------------");
                            break;
                        case "spawn":
                            // if (defaultHero.Equals(defaultHero))
                            // {
                            //    Console.WriteLine("WARNING: You are using the default hero");
                            // }
                            if (!isSpawned)
                            {
                                dungeon.Spawn(defaultHero);
                                Console.WriteLine("SUCCES: {0} spawned !", defaultHero.Name);
                                isSpawned = true;
                            }
                            else
                            {
                                Console.WriteLine("ERROR: You have already spawned a hero !");
                            }

                            break;
                        case "known":
                        case "knownAs":
                        case "knownas":
                            Console.WriteLine(defaultHero.KnownAs());
                            break;
                        case "isalive":
                        case "isAlive":
                            Console.WriteLine("Hero is alive : {0}", defaultHero.IsAlive());
                            break;
                        case "print":
                        case "printmap":
                            dungeon.PrintMap();
                            break;
                        case "exit":
                        case "quit":
                            Environment.Exit(0);
                            break;
                        default:
                            Console.WriteLine("ERROR: Unknown command \"{0}\"", input);
                            break;
                    }
                }

                Console.WriteLine();
            }
        }
        /// <summary>
        /// Checks if hero is in range with spell.
        /// </summary>
        /// <returns>True if Hero is in range to attack with spell.</returns>
        private bool InRange()
        {
            for (int i = 0; i <= this.hero.Spell.CastRange; i++)
            {
                if (this.currPosition.Key + i < this.matrix.GetLength(0))
                {
                    if (this.matrix[this.currPosition.Key + i, this.currPosition.Value] == 'E')
                    {
                        foreach (var enemy in this.enemiesList)
                        {
                            if (enemy.CurrentPosition.Key == this.currPosition.Key + i && enemy.CurrentPosition.Value == this.currPosition.Value)
                            {
                                this.enemyToFight = enemy;
                            }
                        }

                        return true;
                    }
                }

                if (this.currPosition.Key - i >= 0)
                {
                    if (this.matrix[this.currPosition.Key - i, this.currPosition.Value] == 'E')
                    {
                        foreach (var enemy in this.enemiesList)
                        {
                            if (enemy.CurrentPosition.Key == this.currPosition.Key - i && enemy.CurrentPosition.Value == this.currPosition.Value)
                            {
                                this.enemyToFight = enemy;
                            }
                        }

                        return true;
                    }
                }

                if (this.currPosition.Value + i < this.matrix.GetLength(1))
                {
                    if (this.matrix[this.currPosition.Key, this.currPosition.Value + i] == 'E')
                    {
                        foreach (var enemy in this.enemiesList)
                        {
                            if (enemy.CurrentPosition.Key == this.currPosition.Key && enemy.CurrentPosition.Value + i == this.currPosition.Value)
                            {
                                this.enemyToFight = enemy;
                            }
                        }

                        return true;
                    }
                }

                if (this.currPosition.Value - i >= 0)
                {
                    if (this.matrix[this.currPosition.Key, this.currPosition.Value - i] == 'E')
                    {
                        foreach (var enemy in this.enemiesList)
                        {
                            if (enemy.CurrentPosition.Key == this.currPosition.Key && enemy.CurrentPosition.Value - i == this.currPosition.Value)
                            {
                                this.enemyToFight = enemy;
                            }
                        }

                        return true;
                    }
                }
            }

            return false;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="Dungeon"/> class.
        /// </summary>
        /// <param name="map">String which holds the map of the dungeon.</param>
        public Dungeon(string map)
        {
            string[] lines = map.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
            int row = 0;
            foreach (string line in lines)
            {
                int column = 0;
                foreach (char character in line)
                {
                    if (character == 'E')
                    {
                        Enemy newEnemy = new Enemy(this.enemyStats[0], this.enemyStats[1], this.enemyStats[2]);
                        this.enemiesList.Add(newEnemy);
                        newEnemy.CurrentPosition = new KeyValuePair<int, int>(row, column);
                    }

                    this.matrix[row, column] = character;
                    column++;
                }

                row++;
            }
        }