多个列表中的参考元素在一起

时间:2012-04-14 12:04:05

标签: .net vb.net list inheritance combinatorics

对于我的第一篇文章,我有一个棘手的问题。

我已经过去了解教程,但还不足以加入开源社区。

感谢Question我正在尝试创建一个跟踪Alchemy Games(通常在手机等)中使用的组合的程序

我有以下课程

  • 类游戏包含元素和组合的属性
  • 类元素继承List(Of Element)
  • 类组合继承列表(组合)
  • 类元素
  • 班级组合

组合包含元素1和元素2以及结果列表(元素)

我的问题:

使用Game.Elements如何为该元素的组合添加readonly属性。 即

假设元素

  • 地球
  • Air
  • 能源

组合已经

  • 火+地球=能量
  • 地球+空气=泥

我想要

Game.Elements(“Energy”)。返回包含1场比赛的组合的组合。

  • 火+地球=能量

到目前为止,我似乎无法通过Game类访问组合

我对任何建议或建议都非常开放

到目前为止,我的完整代码是

Option Explicit On


'TODO: Add classes for Combinations
'TODO: Edit Elements class for Various filters (Filter by Category, Generation etc)

Public Class Game

    Private pElements As Elements
    Private pOptions As Options


    Public Property Options() As Options
        Get
            Return pOptions
        End Get
        Set(ByVal value As Options)
            pOptions = value
        End Set
    End Property

    Public Property Elements() As Elements
        Get
            Return pElements
        End Get
        Set(ByVal value As Elements)
            pElements = value
        End Set
    End Property

    Public Sub New()
        Me.Elements = New Elements()
        Me.Options = New Options()
    End Sub

    Public Sub New(ByVal TotalElements As Long, _
                   Optional ByVal TotalCategories As Long = 1, _
                   Optional ByVal Categories As Boolean = False, _
                   Optional ByVal Terminated As Boolean = False, _
                   Optional ByVal SelfCombination As Boolean = True, _
                   Optional ByVal ReverseCombination As Boolean = False)
        Me.Elements = New Elements()
        Me.Options = New Options(TotalElements, _
                                 TotalCategories, _
                                 Categories, _
                                 Terminated, _
                                 SelfCombination, _
                                 ReverseCombination)
    End Sub


End Class

#Region "Combinations"

Public Class Combinations
    Inherits List(Of Combination)
    Implements IEnumerable(Of Combination)

End Class

Public Class Combination

    Private pElements As List(Of Element)
    Private pResult As List(Of Element)


    Public Property Elements() As List(Of Element)
        Get
            Return pElements
        End Get
        Set(ByVal value As List(Of Element))
            pElements = value
        End Set
    End Property

    Public Property Result() As List(Of Element)
        Get
            Return pResult
        End Get
        Set(ByVal value As List(Of Element))
            pResult = value
        End Set
    End Property


End Class

#End Region

#Region "Elements"

Public Class Elements
    Inherits List(Of Element)
    Implements System.Collections.Generic.IComparer(Of Element)
    Implements IEnumerable(Of Element)


    Public Function Compare(ByVal e1 As Element, ByVal e2 As Element) As Integer Implements IComparer(Of Element).Compare
#If CONFIG = "Debug" Then
        Debug.WriteLine("{0} - {1}", e1.Name, e2.Name)
#End If
        Select Case e1.Generation
            Case Is = e2.Generation
                Select Case e1.Category.Name
                    Case Is = e2.Category.Name
                        Select Case e1.Name
                            Case Is = e2.Name
                                Return 0
                            Case Is > e2.Name
                                Return 1
                            Case Else '< e2.Name
                                Return -1
                        End Select
                    Case Is > e2.Category.Name
                        Return 1
                    Case Else '< e2.Category
                        Return -1
                End Select
            Case Is > e2.Generation
                Return 1
            Case Else '< e2.Generation
                Return -1
        End Select
    End Function

    Default Public Overloads ReadOnly Property Item(ByVal Name As String) As Element
        Get
            Return Me.Where(Function(k As Element) k.Name = Name).ElementAt(0)
        End Get
    End Property


End Class

Public Class Element
    Implements System.IComparable(Of Element)




    Public Name As String
    Public Category As Category
    Public Generation As Integer
    Public Terminal As Boolean

    Public Sub New()
        With Me
            .Name = "Blank"
            .Category = New Category()
            .Generation = 0
            .Terminal = False
        End With
    End Sub

    Public Sub New(ByVal Name As String, Optional ByVal Category As String = "N/A", Optional ByVal Generation As Long = 0, Optional ByVal Terminal As Boolean = False)
        With Me
            .Name = Name
            .Category = New Category(Category)
            .Generation = Generation
            .Terminal = Terminal
        End With
    End Sub

    Public Shared Operator =(ByVal e1 As Element, ByVal e2 As Element) As Boolean
        Return (e1.Category.Name = e2.Category.Name And _
               e1.Name = e2.Name And _
               e1.Generation = e2.Generation And _
               e1.Terminal = e2.Terminal)
    End Operator
    Public Shared Operator <>(ByVal e1 As Element, ByVal e2 As Element) As Boolean
        Return Not e1 = e2
    End Operator
    Public Shared Operator >(ByVal e1 As Element, ByVal e2 As Element) As Boolean
        'Generation
        'Category
        'Name
        Select Case e1.Generation
            Case Is = e2.Generation
                Select Case e1.Category.Name
                    Case Is = e2.Category.Name
                        Select Case e1.Name
                            Case Is = e2.Name
                                Return False
                            Case Is > e2.Name
                                Return True
                            Case Else '< e2.Name
                                Return False
                        End Select
                    Case Is > e2.Category.Name
                        Return True
                    Case Else '< e2.Category
                        Return False
                End Select
            Case Is > e2.Generation
                Return True
            Case Else '< e2.Generation
                Return False
        End Select
    End Operator
    Public Shared Operator <(ByVal e1 As Element, ByVal e2 As Element) As Boolean
        Return Not (e1 >= e2)
    End Operator
    Public Shared Operator >=(ByVal e1 As Element, ByVal e2 As Element) As Boolean
        Return (e1 = e2 OrElse e1 > e2)
    End Operator
    Public Shared Operator <=(ByVal e1 As Element, ByVal e2 As Element) As Boolean
        Return (e1 = e2 OrElse e1 < e2)
    End Operator

    Public Function CompareTo(ByVal e1 As Element) As Integer Implements IComparable(Of Element).CompareTo
        Select Case Me.Generation
            Case Is = e1.Generation
                Select Case Me.Category.Name
                    Case Is = e1.Category.Name
                        Select Case Me.Name
                            Case Is = e1.Name
                                Return 0
                            Case Is > e1.Name
                                Return 1
                            Case Else '< e2.Name
                                Return -1
                        End Select
                    Case Is > e1.Category.Name
                        Return 1
                    Case Else '< e2.Category
                        Return -1
                End Select
            Case Is > e1.Generation
                Return 1
            Case Else '< e2.Generation
                Return -1
        End Select
    End Function
    Public Overrides Function ToString() As String
        With Me
            Return .WL("{0} - {1} - {2} - {3}", .Name, .Category.Name, .Generation, Terminal)
        End With
    End Function
    Private Function WL(ByVal value As String, ByVal ParamArray args As String()) As String
        For i = LBound(args) To UBound(args)
            value = value.Replace("{" & i & "}", args(i))
        Next
        Return value
    End Function
End Class

Public Class ElementComparer
    Implements System.Collections.Generic.IComparer(Of Element)
    Public Function Compare(ByVal e1 As Element, ByVal e2 As Element) As Integer Implements IComparer(Of Alchemy.Element).Compare

        Select Case e1.Generation
            Case Is = e2.Generation
                Select Case e1.Category.Name
                    Case Is = e2.Category.Name
                        Select Case e1.Name
                            Case Is = e2.Name
                                Return 0
                            Case Is > e2.Name
                                Return 1
                            Case Else '< e2.Name
                                Return -1
                        End Select
                    Case Is > e2.Category.Name
                        Return 1
                    Case Else '< e2.Category
                        Return -1
                End Select
            Case Is > e2.Generation
                Return 1
            Case Else '< e2.Generation
                Return -1
        End Select
    End Function
End Class

#End Region

Public Class Options

#Region "Properties"
    Private pAllowCategories As Boolean
    Private pAllowTerminatedElements As Boolean
    Private pAllowSelfCombination As Boolean
    Private pAllowReverseCombination As Boolean
    Private pTotalElements As Long
    Private pTotalCategories As Long
#End Region

#Region "Property Set"
    Public Property AllowCategories() As Boolean
        Get
            Return pAllowCategories
        End Get
        Set(ByVal value As Boolean)
            pAllowCategories = value
        End Set
    End Property
    Public Property AllowTerminatedElements() As Boolean
        Get
            Return pAllowTerminatedElements
        End Get
        Set(ByVal value As Boolean)
            pAllowTerminatedElements = value
        End Set
    End Property
    Public Property AllowSelfCombination() As Boolean
        Get
            Return pAllowSelfCombination
        End Get
        Set(ByVal value As Boolean)
            pAllowSelfCombination = value
        End Set
    End Property
    Public Property AllowReverseCombination() As Boolean
        Get
            Return pAllowReverseCombination
        End Get
        Set(ByVal value As Boolean)
            pAllowReverseCombination = value
        End Set
    End Property
    Public Property TotalElements() As Long
        Get
            Return pTotalElements
        End Get
        Set(ByVal value As Long)
            pTotalElements = value
        End Set
    End Property
    Public Property TotalCategories() As Long
        Get
            Return pTotalCategories
        End Get
        Set(ByVal value As Long)
            pTotalCategories = value
        End Set
    End Property
#End Region

#Region "Methods"
    Public Sub New(ByVal TotalElements As Long, _
                   Optional ByVal TotalCategories As Long = 1, _
                   Optional ByVal Categories As Boolean = False, _
                   Optional ByVal Terminated As Boolean = False, _
                   Optional ByVal SelfCombination As Boolean = True, _
                   Optional ByVal ReverseCombination As Boolean = False)
        With Me
            .TotalCategories = TotalCategories
            .TotalElements = TotalElements
            .AllowCategories = Categories
            .AllowTerminatedElements = Terminated
            .AllowSelfCombination = SelfCombination
            .AllowReverseCombination = ReverseCombination
        End With
    End Sub

    Public Sub New()
        With Me
            .TotalCategories = 1
            .TotalElements = 1
            .AllowCategories = False
            .AllowTerminatedElements = False
            .AllowSelfCombination = True
            .AllowReverseCombination = False
        End With
    End Sub

#End Region

End Class

Public Class Category

    Private pName As String
    Private pID As UInteger

    Public Property Name() As String
        Get
            Return pName
        End Get
        Set(ByVal value As String)
            pName = value
        End Set
    End Property

    Public Property ID() As UInteger
        Get
            Return pID
        End Get
        Set(ByVal value As UInteger)
            pID = value
        End Set
    End Property

    Public Sub New()
        Me.Name = "N/A"
        Me.ID = 0
    End Sub

    Public Sub New(ByVal Name As String, Optional ByVal ID As UInteger = 0)
        Me.Name = Name
        Me.ID = ID
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

Public Class cGame
    Public Elements as cElements
    Public Sub New()
        cElements.Parent = Me
    End Sub
End Class
Public Class cElements
    Protected Friend Shared Parent As cGame
End Class
Public Class cCombinations
    Protected Friend Shared Parent As cGame
End Class

然后我用

引用它
MyGame.Elements.Parent

然后可以分支到像

这样的组合
MyGame.Elements.Parent.Combinations

并且它将始终引用相同的数据。

:)