简体   繁体   English

比较相同枚举类型的对象的枚举

[英]Compare Enums of objects of the same enum type

I've found similar questions to what I'm about to ask, but they unfortunately don't quite fit what I'm trying to do. 我发现了与我要问的问题类似的问题,但不幸的是,这些问题与我要解决的问题不太吻合。

I have multiple objects with an enum type called TypeOfEnum. 我有多个对象,它们的枚举类型称为TypeOfEnum。

object A = TypeOfEnum.Value1,TypeOfEnum.Value2;
object B = TypeOfEnum.Value2;
object C = TypeOfEnum.Value3,TypeOfEnum.Value2;
object D = TypeOfEnum.Value1,TypeOfEnum.Value4;

What I am attempting to do is compare the values of two of those four objects, and if they contain any of the same values (such as A and D, but not D and B) continue through the code. 我试图做的是比较这四个对象中两个对象的值,如果它们包含相同的值(例如A和D,但不包含D和B),则继续执行代码。 If they do not, the result is skipped and a new object comparison is . 如果不是,则跳过结果,并进行新的对象比较。 In the comparison, they will always have the same enum type, the object will always contain enums. 在比较中,它们将始终具有相同的枚举类型,而对象将始终包含枚举。

I've thus far attempted to pass the enums to lists and check to see if they intersect at all as strings rather than ints, but it has had mixed results. 到目前为止,我一直试图将枚举传递给列表,并检查它们是否以字符串而不是整数相交,但是结果却不尽相同。 IE: Object B and Object C will not match up, but Object A and C will, and Object D and A will. IE:对象B和对象C将不匹配,但是对象A和C将匹配,而对象D和A将匹配。

How can I do this? 我怎样才能做到这一点? Or is it not possible? 还是不可能?

EDIT: The actual code up to where the problem might be: Enums: 编辑:实际的代码,直到问题可能出在哪里:枚举:

[Flags]
public enum TerrainFlags
{
    None = 0,
    Land = 1,
    Water = 2,
    Air = 4,
    Snow = 8,
    Desert = 16,
    Forest = 32,
    Mountain = 64
}

Add Enemies to encounter: 添加敌人遇到:

private void AddEnemyAndUpdatePoints(List<Enemy> enemyList, Enemy enemy, ref int points, ref int bonusMoney, Village village)
    {
        List<string> terrainVillages = new List<string>();
        List<string> terrainEnemies = new List<string>();
        terrainVillages.Add(Enum.GetName(typeof(TerrainFlags), village.Terrain));
        foreach(string terrain in terrainVillages)
            Debug.Log("Village "+village.Name+" "+terrain);
        terrainEnemies.Add(Enum.GetName(typeof(TerrainFlags), enemy.Terrain));
        foreach (string terrain in terrainEnemies)
            Debug.Log("enemy: "+enemy.Name+" "+terrain);
        if (terrainVillages.Intersect(terrainEnemies).Any())
        {
            enemyList.Add(enemy);
            bonusMoney += enemy.Bounty;
            points -= enemy.Points;
            Debug.Log("Enemy Added");
        }
        else
        {
            Debug.Log("Enemy and Village Terrains did not match, rerolling.");
            points -= enemy.Points;
        }

Please note, the points -= enemy.Points in the else statement is simply to prevent an infinite loop this problem has caused and can otherwise be ignored. 请注意,点-=敌人。else语句中的点只是为了防止此问题引起的无限循环,否则可以忽略。

The AddEnemyAndUpdatePoints method is called in the following method: 在以下方法中调用AddEnemyAndUpdatePoints方法:

private int BuyPoints(List<Enemy> enemyList, int points, int difficulty, Village village)
    {
        int bonusMoney = 0;
        while (points > 0)
        {
            var enemy = this.GetEnemyWeighted(EnemyListFunction());
            if (difficulty <= 2 && enemy.Points <= 2)
            {
                AddEnemyAndUpdatePoints(enemyList, enemy, ref points, ref bonusMoney, village);
            }
            else if (difficulty > 2)
            {
                AddEnemyAndUpdatePoints(enemyList, enemy, ref points, ref bonusMoney, village);
            }
        }
     return bonusMoney;
    }

More or less, the problem I seem to be having is if an enemy has the terrains of mountain, snow, and Air, the current code means they don't get paired with something if it has mountain only. 或多或少,我似乎遇到的问题是,如果一个敌人拥有高山,雪地和空中的地形,当前的代码意味着如果只有高山,他们就不会与某物配对。 It needs at least snow and mountain, or mountain and air, and will sometimes end up with enemies that are only desert and forest. 它至少需要雪和山,或者山和空,并且有时最终会变成只有沙漠和森林的敌人。

[Flags]
public enum Options 
{
    None    = 0,
    Option1 = 1,
    Option2 = 2,
    Option3 = 4,
    Option4 = 8
}

you can use the approach below for including and excluding enum flags. 您可以使用以下方法来包含和排除枚举标志。 please note that ~ (tilda) is for exclusion 请注意,〜(tilda)除外

var condition = Options.Options1 | Options.Options2 | ~Options.Options3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM