c# - 比较不同对象但具有相同字段的 2 个列表

标签 c# algorithm unity3d comparison

我有一个

class Player 
{
    public Vector3 position;
     public String name;
}

和一个

class Field
{
    public Vector3 position;
    public bool isTarget;
}

我有一个有 3 个玩家、10 个字段和 3 个目标字段的游戏。为了获胜,用户应将所有玩家放在目标 field 上。有没有一种比较 2 个不同对象列表(List 和 List)但具有相同字段(位置)的好方法?

或者我应该从

继承 Player 和 Field
class BaseItem
{
    public Vector3 position;
}

class Player : BaseItem 
{
    public String name;
}

并比较List<BaseItem>

编辑:只是添加一些说明:我有一个 List 和 List,我想检查是否所有 Player 都与所有 isTarget Fields 具有相同的位置。顺序无关紧要。

var listPlayer = new List<Player>();
listPlayer.Add(new Player { position = new Vector3(1,0,0); name = "a"; }
listPlayer.Add(new Player { position = new Vector3(0,1,0); name = "b"; }
listPlayer.Add(new Player { position = new Vector3(0,0,1); name = "c"; }

var listFields = new List<Field>();
listFields.Add(new Field { position = new Vector3(1,0,0);  isTarget = true;}
listFields.Add(new Field { position = new Vector3(0,1,0);  isTarget = true;}
listFields.Add(new Field { position = new Vector3(0,0,1);  isTarget = true;}
listFields.Add(new Field { position = new Vector3(1,1,0);  }
listFields.Add(new Field { position = new Vector3(1,0,1);  }
****

我如何遍历所有玩家和字段并查看是否每个玩家都有相应的字段(它应该具有相同的位置并且 isTarget 应该为真)?


解决方案:我想保留 Aybe 的答案作为解决方案,但我已经完成了 algorytm,所以我想分享它以防它有用:

private void CheckIfWin()
{
    foreach (var target in gameboardTargets)        
        if (!IsAnyPlayerOnTarget(target))
            return;

    MessageBox.Show(HandleMessage,"You made it!", "Hooray!", MessageBoxButtons.OK);
}


private bool IsAnyPlayerOnTarget(Field target)
{
    foreach (var player in playersList)         
        if (target.GridPosition.Equals(player.GridPosition)) // can be replaced with BaseItem.ComparePosition()
            return true;

    return false;
}

最佳答案

不确定比较两种不同的事物是否是正确的方法。

然而,您可以使这两个东西从基类派生:

internal class BoardEntity {
    public Vector3 Position { get; set; }
}

internal class Player : BoardEntity {
    public string Name { get; set; }
}

internal class Field : BoardEntity {
    public bool IsTarget { get; set; }
}

现在出现了其他的东西:

  • 你每次都必须将它们转换为基类,这会随着时间的推移而变得无聊
  • 比较两个不同的事物是很奇怪的,即使它们确实共享相同的属性
  • 不鼓励比较 float ,因为结果可能(将)是wrong

代码:

internal class BoardEntity {
    public Vector3 Position { get; set; }

    protected bool Equals(BoardEntity other) {
        return Position.Equals(other.Position);
    }

    public override bool Equals(object obj) {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        var other = obj as BoardEntity;
        return other != null && Equals(other);
    }

    public override int GetHashCode() {
        return Position.GetHashCode();
    }
}

internal class MyClass {
    public MyClass() {
        var player = new Player();
        var field = new Field();

        BoardEntity boardEntity1 = player;
        BoardEntity boardEntity2 = field;
        bool b = boardEntity1.Equals(boardEntity2);
    }
}

建议:

提供一个 ComparePosition() 方法在我看来更自然,而且由于我们转换为整数,所以比较总是正确的。

internal class BoardEntity {
    public Vector3 Position { get; set; }

    public bool ComparePosition(BoardEntity boardEntity) {
        var v1 = new Vector3(boardEntity.Position.X, boardEntity.Position.Y, boardEntity.Position.Z);
        var v2 = new Vector3(Position.X, Position.Y, Position.Z);
        return v1.Equals(v2);
    }
}

internal class MyClass {
    public MyClass() {
        var player = new Player();
        var field = new Field();

        bool comparePosition = player.ComparePosition(field);
    }
}

关于c# - 比较不同对象但具有相同字段的 2 个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25464966/

相关文章:

C++ 快速排序实现,没有正确的输出

c# - 试图使用 Vector3.distance 摧毁我的玩家

linux - 尝试在 Ubuntu Linux debian 中打包 Unity 游戏

database - Sqlite数据库+unity

c# - 如何从 C# 主机应用程序执行 Javascript 回调函数

c# - 重复遍历列表

c# - 无法加载文件或程序集

C# EF Deep Lambda 非重复计数查询

arrays - 最长递增的连续序列,其中开始元素大于结束元素 div 2

c++ - 如何将变量添加到其当前值,以及随后的用户输入?