有没有办法轻松循环对象?

时间:2014-05-14 02:29:48

标签: arrays vb.net class loops object

我想针对每个其他坐标列表检查对象的坐标列表。这就是我以前编写的方式:所有这些都是Ship个对象,而CheckForCollisions位于Ship类中。

'AIR CRAFT CARRIER
 If AirCraftCarrier.has_moved And Battleship.has_moved Then
    AirCraftCarrier.CheckForCollisions(Battleship)
 ElseIf AirCraftCarrier.has_moved And Submarine.has_moved Then
    AirCraftCarrier.CheckForCollisions(Submarine)
 ElseIf AirCraftCarrier.has_moved And Destroyer.has_moved Then
    AirCraftCarrier.CheckForCollisions(Destroyer)
 ElseIf AirCraftCarrier.has_moved And PatrolBoat.has_moved Then
    AirCraftCarrier.CheckForCollisions(PatrolBoat)
 End If

'BATTLESHIP
If Battleship.has_moved And AirCraftCarrier.has_moved Then
    Battleship.CheckForCollisions(AirCraftCarrier)
ElseIf Battleship.has_moved And Submarine.has_moved Then
    Battleship.CheckForCollisions(Submarine)
ElseIf Battleship.has_moved And Destroyer.has_moved Then
    Battleship.CheckForCollisions(Destroyer)
ElseIf Battleship.has_moved And PatrolBoat.has_moved Then
    Battleship.CheckForCollisions(PatrolBoat)
End If
'etc., there's 3 more that look exactly this this.

但我绝对宁愿用循环来做这件事。这甚至都没有用。它只会检查第一艘船(AirCraftCarrier对象),然后忽略其余的。只有与ACC相撞的船只才能登记,如果它们相互碰撞,就不会发生任何事情。

这是我写的一个例子,我自己尝试并实现它。我正在考虑使用id并循环遍历它们,但我无法弄清楚如何做到这一点。你们可以向我介绍一下你认为会对我有帮助的概念吗?

Public Class Main
    Dim acc As Ship
    Dim bs As Ship
    Dim sm As Ship
    Dim ds As Ship
    Dim pb As Ship

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        acc = New Ship(1, {{1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}})
        bs = New Ship(2, {{0, 4}, {1, 4}, {2, 4}, {3, 4}})
        sm = New Ship(3, {{4, 5}, {4, 6}, {4, 7}})
        ds = New Ship(4, {{3, 6}, {4, 6}, {5, 6}})
        pb = New Ship(5, {{7, 7}, {7, 8}})
    End Sub

End Class
Public Class Ship

    Dim _id As Integer
    Public Property id() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    Dim _magnitude(,) As Integer
    Public Property magnitude As Integer(,)
        Get
            Return _magnitude
        End Get
        Set(ByVal value As Integer(,))
            _magnitude = value
        End Set
    End Property

    Public Sub New(ByVal idp As Integer, ByVal magnitudep As Integer(,))
        id = idp
        magnitude = magnitudep
    End Sub

End Class

1 个答案:

答案 0 :(得分:1)

你可以这样做吗?

'A list of all the ships
Dim ships As Ship() = _
    { AirCraftCarrier, Battleship, Submarine, _
        Destroyer, PatrolBoat}

Dim query = _
    From ship1 in ships ' iterate thru the ships
    From ship2 in ships ' iterate thru the ships
    Where ship1 <> ship2 ' keep only if both ships are not the same ship
    Where ship1.has_moved 'keep if ship1 has moved
    Where ship2.has_moved 'keep if ship2 has moved
    Select New With { ship1, ship2 }

For Each s in query
    ' for each pair of moved ships check for collision
    s.ship1.CheckForCollisions(s.ship2)
Next
相关问题