예제 #1
0
        static void Main(string[] args)
        {
            var h = new Hero("Bron", "Dragonslayer", 100, 100, 2);
            var w = new Weapon("The Axe of Destiny", 20);
            h.Equip(w);
            var s = new Spell("Fireball", 30, 50, 2);
            h.Learn(s);
            var map = new Dungeon(@"C:\Users\IVAN\Desktop\Programming-101-CSharp\Week 5.2\DungeonsAndLizards\bin\Debug\level1.txt");
            map.Spawn(h);
            map.PrintMap();

            map.MoveHero(Direction.Right);
            map.MoveHero(Direction.Down);
            map.PrintMap();
        }
예제 #2
0
        /// <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>
 /// Learns a spell.
 /// </summary>
 /// <param name="spell">Spell to learn.</param>
 public void Learn(Spell spell)
 {
     this.HasSpell = true;
     this.Spell = spell;
 }
        /// <summary>
        /// Checks if Character has a spell and has enough <![CDATA[mana]]> to cast it.
        /// </summary>
        /// <param name="spell">Spell to cast.</param>
        /// <returns>True if Character has the spell and has <![CDATA[mana]]> for it.</returns>
        public bool CanCast(Spell spell)
        {
            if (!this.HasSpell)
            {
                Console.WriteLine("ERROR: Spell {0} is not equiped!", spell.Name);
                return false;
            }

            if (this.Mana.Value < spell.ManaCost)
            {
                Console.WriteLine("ERROR: You dont have enough mana to cast {0}", spell.Name);
                return false;
            }

            return true;
        }
        /// <summary>
        /// Character attacks with spell if one is learned.
        /// </summary>
        /// <param name="spell">Spell to attack with.</param>
        /// <returns>The damage dealt by the spell.</returns>
        public int Attack(Spell spell)
        {
            if (!this.CanCast(spell))
            {
                return 0;
            }

            return this.Spell.Damage;
        }
예제 #6
0
        public bool TakeTreasure()
        {
            string[] tresures = File.ReadAllLines(treasurePath);
            Random rnd = new Random();
            if (tresures[rnd.Next(0, tresures.Length)] == "health")
            {
                hero.TakeHealing(10);
                return true;
            }

            else if (tresures[rnd.Next(0, tresures.Length)] == "mana")
            {
                hero.TakeMana(10);
                return true;
            }

            else if (tresures[rnd.Next(0, tresures.Length)] == "spell")
            {
                Spell spell = new Spell("FireBall", 30, 50, 2);
                hero.Learn(spell);
                return true;
            }

            else if (tresures[rnd.Next(0, tresures.Length)] == "weapon")
            {
                Weapon weapon = new Weapon("Axe", 10);
                hero.Equip(weapon);
                return true;
            }
            return false;
        }
예제 #7
0
 public void Learn(Spell spell)
 {
     this.spell = spell;
 }
예제 #8
0
 public float Attack(Spell spell)
 {
     if (spell == null)
     {
         return 0;
     }
     return spell.Damage;
 }