简体   繁体   中英

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.

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. 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.

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.

The AddEnemyAndUpdatePoints method is called in the following method:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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