計算機科学のブログ

inheritance - Your object's family tree - implement, base class, sub class

Head First C#: A Learner’s Guide to Real-World Programming with C# and .NET Core (Andrew Stellman(著)、Jennifer Greene(著)、O’Reilly Media)のChapter 6(inheritance - Your object’s family tree)、p.311(Exercise)の解答を求めてみる。

コード

WeaponDamage.cs

using System;
namespace MyFirstConsoleApp
{
    public class WeaponDamage
    {
        private int roll;
        public int Roll
        {
            get { return roll; }
            set
            {
                roll = value;
                CalculateDamage();
            }
        }
        private bool magic;
        public bool Magic
        {
            get
            {
                return magic;
            }
            set
            {
                magic = value;
                CalculateDamage();
            }
        }
        private bool flaming;
        public bool Flaming
        {
            get
            {
                return flaming;
            }
            set
            {
                flaming = value;
                CalculateDamage();
            }
        }
        public int Damage { get; protected set; }

        public WeaponDamage(int roll)
        {
            Roll = roll;
            CalculateDamage();
        }
        protected virtual void CalculateDamage() { }
    }
}

コード

SwordDamage.cs

using System;
namespace MyFirstConsoleApp
{
    public class SwordDamage:WeaponDamage
    {
        private const int BASE_DAMAGE = 3;
        private const int FLAME_DAMAGE = 2;
        public SwordDamage(int roll):base(roll)
        {
        }
        protected override void CalculateDamage()
        {
            decimal magicMultiplier = 1M;
            if (Magic)
            {
                magicMultiplier = 1.75M;
            }
            Damage = BASE_DAMAGE + (int)(Roll * magicMultiplier);
            if (Flaming)
            {
                Damage += FLAME_DAMAGE;
            }
        }
    }
}

コード

ArrowDamage.cs

using System;
namespace MyFirstConsoleApp
{
    public class ArrowDamage:WeaponDamage
    {
        private const decimal BASE_MULTIPLER = 0.35M;
        private const decimal MAGIC_MULTIPLER = 2.5M;
        private const decimal FLAME_DAMAGE = 1.25M;
        
        public ArrowDamage(int roll) : base(roll)
        {
        }
        protected override void CalculateDamage()
        {
            decimal baseDamage = Roll * BASE_MULTIPLER;
            if (Magic)
            {
                baseDamage *= MAGIC_MULTIPLER;
            }
            if (Flaming)
            {
                Damage = (int)Math.Ceiling(baseDamage + FLAME_DAMAGE);
            }
            else
            {
                Damage = (int)Math.Ceiling(baseDamage);
            }
        }
    }
}

入出力結果(Terminal, Zsh)


S for sword, A for Arrow, anything else to quit: s
0 for no magic/flaming, 1 for magic, 2 for flaming, 3 for both: 0
Rolled 12 for 15 HP

S for sword, A for Arrow, anything else to quit: s
0 for no magic/flaming, 1 for magic, 2 for flaming, 3 for both: 1
Rolled 7 for 15 HP

S for sword, A for Arrow, anything else to quit: S
0 for no magic/flaming, 1 for magic, 2 for flaming, 3 for both: 0
Rolled 10 for 13 HP

S for sword, A for Arrow, anything else to quit: S
0 for no magic/flaming, 1 for magic, 2 for flaming, 3 for both: 0
Rolled 8 for 11 HP

S for sword, A for Arrow, anything else to quit: S
0 for no magic/flaming, 1 for magic, 2 for flaming, 3 for both: 1
Rolled 12 for 24 HP

S for sword, A for Arrow, anything else to quit: S
0 for no magic/flaming, 1 for magic, 2 for flaming, 3 for both: 1
Rolled 16 for 31 HP

S for sword, A for Arrow, anything else to quit: A
0 for no magic/flaming, 1 for magic, 2 for flaming, 3 for both: 3
Rolled 11 for 11 HP

S for sword, A for Arrow, anything else to quit: A
0 for no magic/flaming, 1 for magic, 2 for flaming, 3 for both: 2
Rolled 9 for 5 HP

S for sword, A for Arrow, anything else to quit: A
0 for no magic/flaming, 1 for magic, 2 for flaming, 3 for both: 1
Rolled 7 for 7 HP

S for sword, A for Arrow, anything else to quit: S
0 for no magic/flaming, 1 for magic, 2 for flaming, 3 for both: 3
Rolled 14 for 29 HP

S for sword, A for Arrow, anything else to quit: S
0 for no magic/flaming, 1 for magic, 2 for flaming, 3 for both: 3
Rolled 14 for 29 HP

S for sword, A for Arrow, anything else to quit: S
0 for no magic/flaming, 1 for magic, 2 for flaming, 3 for both: 3
Rolled 11 for 24 HP

S for sword, A for Arrow, anything else to quit: S
0 for no magic/flaming, 1 for magic, 2 for flaming, 3 for both: 3
Rolled 11 for 24 HP

S for sword, A for Arrow, anything else to quit: S
0 for no magic/flaming, 1 for magic, 2 for flaming, 3 for both: 3
Rolled 13 for 27 HP

S for sword, A for Arrow, anything else to quit: q