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

时间:2014-08-23 18:16:32

标签: c# algorithm unity3d comparison

我有一个

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

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

我有一个有3个玩家,10个领域和3个目标领域的游戏。对于获胜,用户应将所有玩家放在目标字段上。有没有比较好的方法来比较2个不同的对象列表(List< Player>和List< Field>)但是具有相同的字段(位置)?

或者我应该从

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

class Player : BaseItem 
{
    public String name;
}

并比较List<BaseItem>

编辑:只是添加一些澄清:我有一个列表&lt;播放器&GT;和列表&lt;字段&GT;我想检查所有玩家是否与所有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);  }
****

我怎样才能浏览所有玩家和领域并看看,如果每个玩家都有一个相应的领域(它应该有相同的位置且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;
}

1 个答案:

答案 0 :(得分:0)

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

但是,您可以将这两个内容派生自基类:

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,因为结果可能会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()方法听起来更自然IMO,因为我们转换为整数,所以比较总是正确的。

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