比较具有相同字段的不同对象的两个列表

本文关键字:列表 两个 字段 比较 对象 | 更新日期: 2023-09-27 18:19:15

我有一个

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

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

我有一个游戏,有3个玩家,10个领域和3个目标领域。为了获胜,用户应该将所有玩家放在目标场地上。有没有比较两个不同对象列表的好方法(List and List<字段>),但与相同的字段(位置)?

或者我应该从

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

和比较List<BaseItem> ?

编辑:只是添加一些澄清:我有一个列表 and List和我想检查是否所有的球员有相同的位置作为所有isTarget字段。顺序不重要。
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);  }
****

我如何运行所有的球员和领域,看看,如果每个球员有一个相应的领域(它应该有相等的位置和isttarget应该为真)?


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

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; }
}

现在又出现了其他东西:

  • 你必须每次都将它们转换为基类,这会随着时间的推移而变得无聊
  • 比较两种不同的东西是很奇怪的,即使它们有相同的属性
  • 不鼓励比较floats,因为结果可能(将)错误
代码:

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);
    }
}