C#rpg从文件中读取统计信息

时间:2012-10-27 23:22:25

标签: c# string file-io

所以我有一个带有几行格式的文本文件:

  

名字=战斗机,力量= 10,狡猾= 4,意志力= 2,魔法= 5,体质= 10,健康公式= 250,耐力公式= 200,魔法公式= 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变量中。

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分: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"]);
...