1

所以我有一个文本文件,其中有几行格式如下:

名称=战士,力量=10,狡猾=4,意志=2,魔法=5,体质=10,健康公式=250,耐力公式=200,魔法公式=100

这个文件没有扩展名,是 utf-8 编码的,文件名是 Classes。

目前它有 4 行,其中每个“名称”之间的统计数据发生变化,这些是基本统计数据,因此除非我手动执行,否则它们永远不会改变。

我使用 2 个文件来引用此信息。实体.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
    }

我想提取文件“类”中的格式化数据并将其输入到相互关联的 EntityData.cs 变量中。

4

2 回答 2

1

使用 json。它比 XML 短得多,因此,一旦你习惯了它,就会更容易阅读。

使用 JSON,您将能够在一个屏幕上查看整个角色,而使用 XML,您可能需要滚动 10 页。

至于什么是json以及如何处理,请看这篇文章:JSON library for C#

于 2013-06-10T17:40:11.940 回答
0

我绝对建议将数据存储在 XML 文件中。它会给你更大的灵活性,并且更容易使用。

如果你想坚持当前的格式,你可以通过这种方式手动解析一行:

// 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 回答