計算機科学のブログ

encapsulation - Keep your privates...private - well-encapsulated class, properties

Head First C Sharp: A Learner’s Guide to Real-World Programming with C Sharp and .NET (Andrew Stellman(著)、Jennifer Greene(著)、O’Reilly Media)の Chapter 5(encapsulation - Keep your privates…private)、p.267(Exercise)の解答を求めてみる。

コード

Program.cs

namespace ConsoleApp1
{

    class Program
    {
        static Random random = new Random();
        static int RollDice()
        {
            int roll = 0;
            for (int i = 0; i < 3; i++)
            {
                roll += random.Next(1, 7);
            }
            return roll;
        }

        static void Main(string[] args)
        {
            SwordDamage swordDamage = new SwordDamage(RollDice());
            while (true)
            {
                Console.Write("0 fur no magic/flaming, 1 for magic, 2 for flaming, 3 for both, anything else to quit: ");
                char key = Console.ReadKey(false).KeyChar;
                if (key < '0' || '3' < key)
                {
                    break;
                }
                swordDamage.Roll = 0;
                for (int i = 0; i < 3; i++)
                {
                    swordDamage.Roll += Random.Shared.Next(1, 7);
                }
                swordDamage.Magic = key == '1' || key == '3';
                swordDamage.Flaming = key == '2' || key == '3';
                Console.WriteLine($"\nRolled {swordDamage.Roll} for {swordDamage.Damage} HP\n");
            }
        }
    }
}

コード

SwordDamage.cs

using System.Reflection.Metadata;

namespace ConsoleApp1;

public class SwordDamage
{
    private const int BASE_DAMAGE = 3;
    private const int FLAME_DAMAGE = 2;
    private int roll;
    /// <summary>
    /// 3d6 roll
    /// </summary>
    public int Roll
    {
        get { return roll; }
        set
        {
            roll = value;
            CalculateDamage();
        }
    }
    private bool magic;
    /// <summary>
    /// Is the sword is magic?
    /// </summary>
    public bool Magic
    {
        get { return magic; }
        set
        {
            magic = value;
            CalculateDamage();
        }
    }
    private bool flaming;
    /// <summary>
    /// Is the sword is flaming?
    /// </summary>
    public bool Flaming
    {
        get { return flaming; }
        set
        {
            flaming = value;
            CalculateDamage();
        }
    }
    /// <summary>
    /// Calculated damage
    /// </summary>
    public int Damage { get; set; }
    private void CalculateDamage()
    {
        Damage = BASE_DAMAGE;
        if (Magic)
        {

            Damage = (int)(Roll * 1.75M) + BASE_DAMAGE;
        }
        else
        {
            Damage = Roll + BASE_DAMAGE;
        }
        if (Flaming)
        {
            Damage += FLAME_DAMAGE;
        }
    }
    public SwordDamage(int roll)
    {
        this.roll = roll;
        CalculateDamage();
    }
}

入出力結果(Terminal, Zsh)

% dotnet run
0 fur no magic/flaming, 1 for magic, 2 for flaming, 3 for both, anything else to quit: 2
Rolled 8 for 13 HP

0 fur no magic/flaming, 1 for magic, 2 for flaming, 3 for both, anything else to quit: 1
Rolled 8 for 17 HP

0 fur no magic/flaming, 1 for magic, 2 for flaming, 3 for both, anything else to quit: 3
Rolled 6 for 15 HP

0 fur no magic/flaming, 1 for magic, 2 for flaming, 3 for both, anything else to quit: q%