1

したがって、次のようにフォーマットされた数行のテキストファイルがあります。

Name=Fighter、Strength = 10、Cunning = 4、Willpower = 2、Magic = 5、Constitution = 10、Health Formula = 250、Stamina Formula = 200、Magic Formula = 100

このファイルには拡張子がなく、utf-8 でエンコードされており、ファイル名は Classes です。

現在、各「名前」間で統計が変化する4行があります。これらは基本統計であるため、手動で変更しない限り変更されることはありません。

この情報を参照するために 2 つのファイルを使用します。Entity.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace RpgLibrary.CharacterClasses
    {
    public enum EntityGender { Male, Female, Unknown }
    public abstract class Entity
    {
    #region Vital Field and Property Region
    protected string entityType;
    protected EntityGender gender;
    public string EntityType
    {
        get { return entityType; }
    }
    public EntityGender Gender

         {
        get { return gender; }
        protected set { gender = value; }
    }
    #endregion
    #region Basic Attribute and Property Region
    protected int strength;
    protected int dexterity;
    protected int cunning;
    protected int willpower;
    protected int magic;
    protected int constitution;
    protected int strengthModifier;
    protected int dexterityModifier;
    protected int cunningModifier;
    protected int willpowerModifier;
    protected int magicModifier;
    protected int constitutionModifier;

    public int Strength{

        get { return strength + strengthModifier; }
        protected set { strength = value; }
    }

    public int Dexterity{

        get { return dexterity + dexterityModifier; }
        protected set { dexterity = value; }
    }

    public int Cunning{

        get { return cunning + cunningModifier; }
        protected set { cunning = value; }
    }

    public int Willpower{

        get { return willpower + willpowerModifier; }
        protected set { willpower = value; }
    }

    public int Magic{

        get { return magic + magicModifier; }
        protected set { magic = value; }
    }

    public int Constitution{

        get { return constitution + constitutionModifier; }
        protected set { constitution = value; }
    }
    #endregion

    #region Calculated Attribute Field and Property Region
    protected AttributePair health;
    protected AttributePair stamina;
    protected AttributePair mana;

    public AttributePair Health{
        get { return health; }
    }

    public AttributePair Stamina
    {
        get { return stamina; }
    }

    public AttributePair Mana
    {
        get { return mana; }
    }

    protected int attack;
    protected int damage;
    protected int defense;
    #endregion

    #region Level Field and Property Region
    protected int level;
    protected long experience;

    public int Level
    {
        get { return level; }
        protected set { level = value; }
    }

    public long Experience
    {
        get { return experience; }
        protected set { experience = value; }
    }
    #endregion

    #region Constructor Region
    private Entity()
    {
        Strength = 0;
        Dexterity = 0;
        Cunning = 0;
        Willpower = 0;
        Magic = 0;
        Constitution = 0;
        health = new AttributePair(0);
        stamina = new AttributePair(0);
        mana = new AttributePair(0);
    }
    public Entity(EntityData entityData)
    {
        entityType = entityData.EntityName;
        Strength = entityData.Strength;
        Dexterity = entityData.Dexterity;
        Cunning = entityData.Cunning;
        Willpower = entityData.Willpower;
        Magic = entityData.Magic;
        Constitution = entityData.Constitution;
        health = new AttributePair(0);
        stamina = new AttributePair(0);
        mana = new AttributePair(0);
    }
    #endregion
     }
    }

そして EntityData.cs:

   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;

   public class EntityData
   {
    #region Field Region
    public string ClassName;
    public int Strength;
    public int Dexterity;
    public int Cunning;
    public int Willpower;
    public int Magic;
    public int Constitution;
    public string HealthFormula;
    public string StaminaFormula;
    public string MagicFormula;
    #endregion
    #region Constructor Region
    private EntityData()
     {
    }
      #endregion
       #region Static Method Region
       public static void ToFile(string Classes)
    {
        string toString = EntityName + ", ";
        toString += Strength.ToString() + ", ";
        toString += Dexterity.ToString() + ", ";
                toString += Cunning.ToString() + ", ";
                toString += Willpower.ToString() + ", ";
        toString += Magic.ToString() + ", ";
                toString += Constitution.ToString() + ", ";
                toString += HealthFormula + ", ";
        toString += StaminaFormula + ", ";
                toString += MagicFormula + ", ";

    return toString;
     }
     public static EntityData FromFile(string Classes)
     {
        EntityData entity = new EntityData();
        return entity;
    }
    #endregion
    }

ファイル「Classes」でフォーマットされたデータを取得し、それを EntityData.cs に入力したい 相互に関連付けられた変数。

4

2 に答える 2

1

json を使用します。XML よりもはるかに短いため、慣れるとはるかに読みやすくなります。

JSON を使用すると、キャラクター全体を 1 つの画面で表示できますが、XML を使用すると、おそらく 10 ページのスクロールが必要になります。

json とは何か、およびその処理方法については、この投稿を参照してください: C# の JSON ライブラリ

于 2013-06-10T17:40:11.940 に答える
0

データを XML ファイルに保存することを強くお勧めします。これにより、柔軟性が向上し、作業がはるかに簡単になります。

現在の形式に固執したい場合は、次の方法で 1 行を手動で解析できます。

// input text
string input = "Name=Fighter, Strength = 10,Cunning = 4, Willpower = 2, Magic = 5, Constitution = 10, Health Formula = 250, Stamina Formula = 200, Magic Formula = 100";

// dictionary with values.. key => value
var dict = input.Split(',').Select(s2 => s2.Replace(" ", "")).ToDictionary(s => s.Substring(0, s.IndexOf('=')), s => s.Substring(s.IndexOf('=') + 1));

// retrieving single values
int willpower = int.Parse(dict["Willpower"]);
...
于 2012-10-28T00:04:19.753 に答える