通过2维数组搜索多次

时间:2017-02-18 16:50:58

标签: vb.net

Public Function Seeing_Connected_Counties(To_Button As Button, Initial_Button As Button)
    Dim AdjcentCounties(,) As Button = New Button(,) {{Derbyshire, West_Yorkshire, South_Yorkshire, Nottinghamshire, Leicestershire, Warwickshire, Staffordshire, Cheshire, Manchester}, {Buckinhamshire, Hertfordshire, London, Berkshire, Oxfordshire, Northamptonshire, Bedfordshire, Scapegoat, Scapegoat}, {Hertfordshire, Cambridgeshire, Essex, London, Buckinhamshire, Bedfordshire, Scapegoat, Scapegoat, Scapegoat}, {Bedfordshire, Cambridgeshire, Hertfordshire, Buckinhamshire, Northamptonshire, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Cambridgeshire, Lincolnshire, Norfolk, Suffolk, Essex, Hertfordshire, Bedfordshire, Northamptonshire, Scapegoat}, {Northamptonshire, Cambridgeshire, Bedfordshire, Buckinhamshire, Oxfordshire, Warwickshire, Leicestershire, Scapegoat, Scapegoat}, {Herefordshire, Gloucestershire, Worcestershire, Shropshire, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Worcestershire, Warwickshire, Gloucestershire, Herefordshire, Shropshire, Staffordshire, Scapegoat, Scapegoat, Scapegoat}, {Warwickshire, Northamptonshire, Leicestershire, Oxfordshire, Gloucestershire, Worcestershire, Staffordshire, Derbyshire, Scapegoat}, {Shropshire, Cheshire, Staffordshire, Worcestershire, Herefordshire, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Staffordshire, Cheshire, Derbyshire, Warwickshire, Worcestershire, Shropshire, Scapegoat, Scapegoat, Scapegoat}, {Leicestershire, Nottinghamshire, Lincolnshire, Northamptonshire, Warwickshire, Derbyshire, Scapegoat, Scapegoat, Scapegoat}, {Lincolnshire, East_Yorkshire, South_Yorkshire, Nottinghamshire, Leicestershire, Cambridgeshire, Norfolk, Scapegoat, Scapegoat}, {Nottinghamshire, Lincolnshire, Leicestershire, Derbyshire, South_Yorkshire, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Cheshire, Merseyside, Manchester, Derbyshire, Staffordshire, Shropshire, Scapegoat, Scapegoat, Scapegoat}, {South_Yorkshire, North_Yorkshire, East_Yorkshire, Lincolnshire, Nottinghamshire, Derbyshire, West_Yorkshire, Scapegoat, Scapegoat}, {Merseyside, Lancashire, Manchester, Cheshire, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Manchester, West_Yorkshire, Derbyshire, Cheshire, Merseyside, Lancashire, Scapegoat, Scapegoat, Scapegoat}, {West_Yorkshire, North_Yorkshire, South_Yorkshire, Derbyshire, Manchester, Lancashire, Scapegoat, Scapegoat, Scapegoat}, {East_Yorkshire, North_Yorkshire, Lincolnshire, South_Yorkshire, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {North_Yorkshire, County_Durham, East_Yorkshire, South_Yorkshire, West_Yorkshire, Lancashire, Cumbria, Scapegoat, Scapegoat}, {Lancashire, Cumbria, North_Yorkshire, West_Yorkshire, Manchester, Merseyside, Scapegoat, Scapegoat, Scapegoat}, {Cumbria, Northumberland, County_Durham, North_Yorkshire, Lancashire, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {County_Durham, Northumberland, North_Yorkshire, Cumbria, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Northumberland, Cumbria, County_Durham, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Cornwall, Devon, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Devon, Cornwall, Somerset, Dorset, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Somerset, Devon, Dorset, Whiltshire, Gloucestershire, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Dorset, Devon, Somerset, Whiltshire, Hampshire, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Hampshire, Dorset, Whiltshire, Berkshire, Surrey, West_Sussex, Isle_Of_White, Scapegoat, Scapegoat}, {Isle_Of_White, Hampshire, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Whiltshire, Somerset, Dorset, Hampshire, Berkshire, Gloucestershire, Oxfordshire, Scapegoat, Scapegoat}, {Gloucestershire, Herefordshire, Worcestershire, Warwickshire, Oxfordshire, Whiltshire, Somerset, Scapegoat, Scapegoat}, {West_Sussex, Surrey, East_Sussex, Kent, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {East_Sussex, West_Sussex, Kent, Surrey, Hampshire, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Kent, East_Sussex, Surrey, London, Essex, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {London, Surrey, Berkshire, Buckinhamshire, Hertfordshire, Essex, Kent, Scapegoat, Scapegoat}, {Essex, Kent, London, Hertfordshire, Cambridgeshire, Suffolk, Scapegoat, Scapegoat, Scapegoat}, {Suffolk, Essex, Cambridgeshire, Norfolk, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Norfolk, Suffolk, Cambridgeshire, Lincolnshire, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Surrey, Kent, West_Sussex, Hampshire, Berkshire, London, Scapegoat, Scapegoat, Scapegoat}, {Berkshire, London, Surrey, Hampshire, Whiltshire, Oxfordshire, Buckinhamshire, Scapegoat, Scapegoat}, {Oxfordshire, Berkshire, Whiltshire, Gloucestershire, Warwickshire, Northamptonshire, Buckinhamshire, Scapegoat, Scapegoat}}
    Dim N As Integer = 1
    Dim N_button As Button = Initial_Button
    Dim T_button As Button
    For I = 0 To 42
        If N_button Is AdjcentCounties(I, 0) Then
            For f = 0 To 8
                T_button = AdjcentCounties(I, f)
                If T_button.BackColor = To_Button.BackColor Then
                    If T_button Is To_Button Then
                        Return True
                    ElseIf T_button Is Scapegoat Then
                        N_button = AdjcentCounties(I, N)
                        N += 1
                        If N >= 8 Then
                            N = 0
                        End If
                        I = 0
                        Exit For
                    End If
                End If
            Next
        End If
    Next
    Return False
End Function

对于我的计算项目,我正在制作版本风险,这需要在玩家攻击后允许他们将军队从一个县移动到另一个县,如果他们由同一玩家拥有的县连接(在我的游戏中用按钮表示) #39; s backcolour)。在过去的4个小时里,我一直在玩这个代码,并且没有取得任何进展 感谢您的帮助

2 个答案:

答案 0 :(得分:1)

您可能需要考虑面向对象的方法,其中业务/游戏/程序逻辑与UI分离。使用类来表示每个属性的各种属性,其中一个可能是哪些区域相邻,只需要一点点工作,无论它们是否友好。

Public Class Territory
    Public Property Name As String
    Public Property Owner As String
    Public Property Troops As Int32

    Public Property AdjacentTerritories As List(Of String)

    ' map of all the territory names
    Public Shared TerritoryMap As String()() = {
                    New String() {"Derbyshire", "WestYorkshire", "SouthYorkshire", "Nottinghamshire", "Leicestershire", "Warwickshire", "Staffordshire", "Cheshire", "Manchester"},
                    New String() {"Buckinhamshire", "Hertfordshire", "London", "Berkshire", "Oxfordshire", "Northamptonshire", "Bedfordshire"},
                    New String() {"Hertfordshire", "Cambridgeshire", "Essex", "London", "Buckinhamshire", "Bedfordshire"},
                    New String() {"Bedfordshire", "Cambridgeshire", "Hertfordshire", "Buckinhamshire", "Northamptonshire"},
                    New String() {"Cambridgeshire", "Lincolnshire", "Norfolk", "Suffolk", "Essex", "Hertfordshire", "Bedfordshire", "Northamptonshire"},
                    New String() {"Northamptonshire", "Cambridgeshire", "Bedfordshire", "Buckinhamshire", "Oxfordshire", "Warwickshire", "Leicestershire"},
                ... you get the idea

    Public Sub New(n As String)
        Name = n

        Dim tmp = TerritoryMap.
                        Where(Function(t) t(0) = n).
                        SelectMany(Function(k) k).
                        ToArray()

        AdjacentTerritories = New List(Of String)(tmp.Skip(1).
                                 Take(tmp.Length - 1))
    End Sub

    Public Function IsAdjacentTo(otherShire As String) As Boolean
        Return AdjacentTerritories.Contains(otherShire)
    End Function

End Class

领土地图是一个锯齿状的数组,因此所有“替罪羊”占位符都不需要存在。代码使用每个“行”中的第一个名称作为本地区域名称。因此,在创建列表时,会跳过第一个列表。

请注意,相邻地区的智能已封装。由于领土在游戏过程中不移动,存储它们的列表可以防止任何需要循环,迭代,解析或做任何其他旋转。

创建所有地区的机制也使用“地图”:

' main list of territories
Dim TerrList As New List(Of Territory)

' extract the first element from each row for iterating
Dim terrTmp = Territory.TerritoryMap.Select(Function(j) j(0)).ToArray()

For Each tName In terrTmp
    TerrList.Add(New Territory(tName))
Next

并不多,每个人都“知道”他们的邻居是谁:

For Each t As Territory In TerrList
    Console.WriteLine("Here in {0}, our neighbors are:", t.Name)
    Console.WriteLine(String.Join(", ", t.AdjacentTerritories))
Next

结果(部分):

  

在Buckinhamshire,我们的邻居是:
  赫特福德郡,伦敦,伯克郡,牛津郡,北安普敦郡,贝德福德郡   在赫特福德郡,我们的邻居是:
  剑桥郡,埃塞克斯,伦敦,巴林汉姆郡,贝德福德郡
  在贝德福德郡,我们的邻居是:
  剑桥郡,赫特福德郡,巴林汉姆郡,北安普敦郡

通常,您将检查单个名称(直到您进行路径查找):

Dim rndTerr1 = TerrList.RandomItem()
Dim rndTerr2 = TerrList.RandomItem()

If rndTerr1.IsAdjacentTo(rndTerr2.Name) Then
    Console.WriteLine("Attack {0} from {1}!!!", rndTerr1.Name, rndTerr2.Name)
Else
    Console.WriteLine("{0} cannot attack {1}....YET!", rndTerr1.Name, rndTerr2.Name)
End If

此粗略不包括谁拥有每个领土。这需要访问主要区域列表或AdjacentTerritories作为List(Of Territory)

答案 1 :(得分:0)

如果我理解您的要求,请使用Recursion

但是,您还需要跟踪您已经尝试过的县道路,或者您可以进入无限循环..

Private AdjcentCounties(,) As Button = New Button(,) {{Derbyshire, West_Yorkshire, South_Yorkshire, Nottinghamshire, Leicestershire, Warwickshire, Staffordshire, Cheshire, Manchester}, {Buckinhamshire, Hertfordshire, London, Berkshire, Oxfordshire, Northamptonshire, Bedfordshire, Scapegoat, Scapegoat}, {Hertfordshire, Cambridgeshire, Essex, London, Buckinhamshire, Bedfordshire, Scapegoat, Scapegoat, Scapegoat}, {Bedfordshire, Cambridgeshire, Hertfordshire, Buckinhamshire, Northamptonshire, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Cambridgeshire, Lincolnshire, Norfolk, Suffolk, Essex, Hertfordshire, Bedfordshire, Northamptonshire, Scapegoat}, {Northamptonshire, Cambridgeshire, Bedfordshire, Buckinhamshire, Oxfordshire, Warwickshire, Leicestershire, Scapegoat, Scapegoat}, {Herefordshire, Gloucestershire, Worcestershire, Shropshire, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Worcestershire, Warwickshire, Gloucestershire, Herefordshire, Shropshire, Staffordshire, Scapegoat, Scapegoat, Scapegoat}, {Warwickshire, Northamptonshire, Leicestershire, Oxfordshire, Gloucestershire, Worcestershire, Staffordshire, Derbyshire, Scapegoat}, {Shropshire, Cheshire, Staffordshire, Worcestershire, Herefordshire, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Staffordshire, Cheshire, Derbyshire, Warwickshire, Worcestershire, Shropshire, Scapegoat, Scapegoat, Scapegoat}, {Leicestershire, Nottinghamshire, Lincolnshire, Northamptonshire, Warwickshire, Derbyshire, Scapegoat, Scapegoat, Scapegoat}, {Lincolnshire, East_Yorkshire, South_Yorkshire, Nottinghamshire, Leicestershire, Cambridgeshire, Norfolk, Scapegoat, Scapegoat}, {Nottinghamshire, Lincolnshire, Leicestershire, Derbyshire, South_Yorkshire, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Cheshire, Merseyside, Manchester, Derbyshire, Staffordshire, Shropshire, Scapegoat, Scapegoat, Scapegoat}, {South_Yorkshire, North_Yorkshire, East_Yorkshire, Lincolnshire, Nottinghamshire, Derbyshire, West_Yorkshire, Scapegoat, Scapegoat}, {Merseyside, Lancashire, Manchester, Cheshire, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Manchester, West_Yorkshire, Derbyshire, Cheshire, Merseyside, Lancashire, Scapegoat, Scapegoat, Scapegoat}, {West_Yorkshire, North_Yorkshire, South_Yorkshire, Derbyshire, Manchester, Lancashire, Scapegoat, Scapegoat, Scapegoat}, {East_Yorkshire, North_Yorkshire, Lincolnshire, South_Yorkshire, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {North_Yorkshire, County_Durham, East_Yorkshire, South_Yorkshire, West_Yorkshire, Lancashire, Cumbria, Scapegoat, Scapegoat}, {Lancashire, Cumbria, North_Yorkshire, West_Yorkshire, Manchester, Merseyside, Scapegoat, Scapegoat, Scapegoat}, {Cumbria, Northumberland, County_Durham, North_Yorkshire, Lancashire, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {County_Durham, Northumberland, North_Yorkshire, Cumbria, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Northumberland, Cumbria, County_Durham, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Cornwall, Devon, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Devon, Cornwall, Somerset, Dorset, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Somerset, Devon, Dorset, Whiltshire, Gloucestershire, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Dorset, Devon, Somerset, Whiltshire, Hampshire, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Hampshire, Dorset, Whiltshire, Berkshire, Surrey, West_Sussex, Isle_Of_White, Scapegoat, Scapegoat}, {Isle_Of_White, Hampshire, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Whiltshire, Somerset, Dorset, Hampshire, Berkshire, Gloucestershire, Oxfordshire, Scapegoat, Scapegoat}, {Gloucestershire, Herefordshire, Worcestershire, Warwickshire, Oxfordshire, Whiltshire, Somerset, Scapegoat, Scapegoat}, {West_Sussex, Surrey, East_Sussex, Kent, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {East_Sussex, West_Sussex, Kent, Surrey, Hampshire, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Kent, East_Sussex, Surrey, London, Essex, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {London, Surrey, Berkshire, Buckinhamshire, Hertfordshire, Essex, Kent, Scapegoat, Scapegoat}, {Essex, Kent, London, Hertfordshire, Cambridgeshire, Suffolk, Scapegoat, Scapegoat, Scapegoat}, {Suffolk, Essex, Cambridgeshire, Norfolk, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Norfolk, Suffolk, Cambridgeshire, Lincolnshire, Scapegoat, Scapegoat, Scapegoat, Scapegoat, Scapegoat}, {Surrey, Kent, West_Sussex, Hampshire, Berkshire, London, Scapegoat, Scapegoat, Scapegoat}, {Berkshire, London, Surrey, Hampshire, Whiltshire, Oxfordshire, Buckinhamshire, Scapegoat, Scapegoat}, {Oxfordshire, Berkshire, Whiltshire, Gloucestershire, Warwickshire, Northamptonshire, Buckinhamshire, Scapegoat, Scapegoat}}
Private TriedCounties As New List(Of String)

Public Function Seeing_Connected_Counties(To_Button As Button, Initial_Button As Button, Optional Recursion As Boolean = False) As Boolean

    If Not Recursion Then TriedCounties.Clear() ',use a list to ensure we don't try a county twice and get into an infinite loop
    TriedCounties.Add(Initial_Button.Name)

    Dim N As Integer = 1
    Dim N_button As Button = Initial_Button
    Dim T_button As Button
    For I As Integer = 0 To UBound(AdjcentCounties, 1)
        If N_button Is AdjcentCounties(I, 0) Then

           'First Pass, see if destination is in list

            For f As Integer = 0 To UBound(AdjcentCounties, 2)
                T_button = AdjcentCounties(I, f)
                If T_button Is To_Button Then
                    Return T_button.BackColor = To_Button.BackColor
                End If
            Next

           'Second Pass, check alternate paths

            For f As Integer = 0 To UBound(AdjcentCounties, 2)
                T_button = AdjcentCounties(I, f)
                If T_button IsNot ScapeGoat AndAlso T_button.BackColor = To_Button.BackColor Andalso Not TriedCounties.Contains(T_button.Name) Then
                    If Seeing_Connected_Counties(To_Button, T_button, True) Then Return True
                End If
            Next
            Return False
        Else
    Next
    Return False
End Function

如果是我,我会将AdjacentCounties移出例程并使其成为一个类(表单)变量,如图所示。