C# – Derived Classes && !Dictionaries

Spent some time tonight cleaning up the character class from Tactics Sharp. When I have some time later I’ll try to push it everything I have out to github. As for new additions I, finally implemented the character class as a base class and made to two derived classes, human and dragon. Also, I added two dictionaries but I’m not getting the iteration process right. I’m sure the Python gods are looking down at me now.

I left out my bitwise operations on bit flags. Check one of my older posts if you want to see how to do that.

 
Character Class Source Code:

using System;
using System.Collections.Generic;

namespace Tactics_Sharp {
    internal class Character : IComparable {
        // - Character's Vitals && Skills
        private Dictionary<String, Int32> _skills;
        private Dictionary<String, Int32> _vitals;

        public Character() {
            this._skills = new Dictionary<String, Int32>() { 
                { "Punch", 0 }, { "Weapon", 0 }, { "Evade", 0}, { "Steal", 0 }, 
                { "Block", 0 }, { "Fire", 0 }, { "Lighting", 0 }, { "Heal", 0 },  
                { "Bite", 0 }, { "Claw", 0 } };
            this._vitals = new Dictionary<String, Int32>() { { "HP", 0 }, { "MP", 0 }, { "LVL", 0 } };
        }

        // - Virtual Derived Classes
        public virtual Int32 CompareTo(Object _obj) { return this.CompareTo(_obj); }
        public virtual Dictionary<String, Int32> getVitals() { return this._vitals; }
        public virtual Dictionary<String, Int32> getSkills() { return this._skills; }
        public virtual void incrementSkill(String key)  { this._skills[key]++; }
        public virtual void incrementVitals(String key) { this._vitals[key]++; }
        public virtual Int32 indexSkill(String key) { return this._skills[key]; }
        public virtual Int32 indexVital(String key) { return this._vitals[key]; }

        public virtual void setSkill(String key, Int32 val) {
            if (val >= 0 && val <= 100)
                this._skills[key] = val;
        }
        public virtual void setVital(String key, Int32 val) {
            if (val >= 0)
                this._vitals[key] = val;
        }
        
    }

    internal sealed class Human : Character, IComparable {
        [Flags]
        public enum CharClass {
            Squire = 0x0,
            Knight = 0x1,
            Monk = 0x2,
            Mage = 0x4,
            Archer = 0x8,
            Theif = 0x10
        }

        public String _name;
        public CharClass _class;
        public Human(String _name) { 
            this._name = _name;
            this._class = new CharClass();
        }
        

        public override Int32 CompareTo(object _obj)        { return base.CompareTo(_obj); }
        public override Dictionary<String, Int32> getSkills() { return base.getSkills(); }
        public override Dictionary<String, Int32> getVitals() { return base.getVitals(); }
        public override void incrementSkill(String key)      { base.incrementSkill(key); }
        public override void incrementVitals(String key)     { base.incrementVitals(key); }
        public override Int32 indexSkill(String key) { return base.indexSkill(key); }
        public override Int32 indexVital(String key) { return base.indexVital(key); }
        public override void setSkill(String key, Int32 val) { base.setSkill(key,val); }
        public override void setVital(string key, Int32 val)   { base.setVital(key, val); }
    }

    internal sealed class Dragon : Character, IComparable {
        [Flags]
        public enum CharClass
        {
            Compiler = 0x0
        }
        public String _name;
        public CharClass _class;

        public Dragon( String _name ) { 
            this._name = _name;
            this._class = new CharClass();
        }

        public override Int32 CompareTo(object _obj) { return base.CompareTo(_obj); }
        public override Dictionary<String, Int32> getSkills() { return base.getSkills(); }
        public override Dictionary<String, Int32> getVitals() { return base.getVitals(); }
        public override void incrementSkill(String key) { base.incrementSkill(key); }
        public override void incrementVitals(String key) { base.incrementVitals(key); }
        public override Int32 indexSkill(String key) { return base.indexSkill(key); }
        public override Int32 indexVital(String key) { return base.indexVital(key); }
        public override void setSkill(String key, Int32 val) { base.setSkill(key, val); }
        public override void setVital(string key, Int32 val) { base.setVital(key, val); }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Human BitDiddler = new Human("Bit Diddler");
            Dragon Aho = new Dragon("Aho");
            
            // - BitDiddler is a master of iteration, But I'm not . . . 
            //foreach (String skill in BitDiddler.getSkills().Keys )
            //    BitDiddler.incrementSkill(skill);
            //foreach (String vital in BitDiddler.getVitals().Keys)
            //    BitDiddler.setVital(vital, 100);
           
            // - This is fine, Dragons are dumb anyway.
            Aho.setSkill("Fire", 5);
            Aho.setVital("HP", 300);

            // - Test Block
            Console.WriteLine(BitDiddler._name + " vs " + Aho._name);
            Console.WriteLine(">>>>>---------------------------<<<<<");
            Console.WriteLine(BitDiddler._name + "'s Pre-Fight Stats:");
            Console.WriteLine("Class: " + BitDiddler._class);
            Console.Write("Vitals:");
            foreach (var vital in BitDiddler.getVitals())
                if (vital.Value > 0)
                    Console.Write(" " + vital.Key + ": " + vital.Value );
            Console.WriteLine();
            Console.Write("Skills:");
            foreach (var skill in BitDiddler.getSkills())
                if (skill.Value > 0)
                    Console.Write(" " + skill.Key + ": " + skill.Value );
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine(Aho._name + "'s Pre-Fight Stats:");
            Console.WriteLine("Class: " + Aho._class);
            Console.Write("Vitals:");
            foreach (var vital in Aho.getVitals())
                if (vital.Value > 0)
                    Console.Write(" " + vital.Key + ": " + vital.Value);
            Console.WriteLine();
            Console.Write("Skills:");
            foreach (var skill in Aho.getSkills())
                if (skill.Value > 0)
                    Console.Write(" " + skill.Key + ": " + skill.Value);
            Console.WriteLine();
            Console.ReadLine();
        }
    }
}

Sample Output:

Bit Diddler vs Aho
>>>>>—————————<<<<<
Bit Diddler’s Pre-Fight Stats:
Class: Squire
Vitals:
Skills:

Aho’s Pre-Fight Stats:
Class: Compiler
Vitals: HP: 300
Skills: Fire: 5

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s