Vb.net 通过二维数组进行多次搜索

Vb.net 通过二维数组进行多次搜索,vb.net,Vb.net,对于我的计算项目,我正在做一个版本风险,这需要玩家攻击后允许他们将部队从一个县转移到另一个县,如果他们由同一玩家拥有的县连接(在我的游戏中由按钮的背景色表示)。在过去的4个小时里,我一直在使用上面的代码,没有取得任何进展 如果我了解您的使用要求,将非常感谢您的帮助 但是,您还需要跟踪您已经尝试过的县路径,或者您可能会进入无限循环 Public Function Seeing_Connected_Counties(To_Button As Button, Initial_Button As But

对于我的计算项目,我正在做一个版本风险,这需要玩家攻击后允许他们将部队从一个县转移到另一个县,如果他们由同一玩家拥有的县连接(在我的游戏中由按钮的背景色表示)。在过去的4个小时里,我一直在使用上面的代码,没有取得任何进展
如果我了解您的使用要求,将非常感谢您的帮助

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

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

如果是我,我会把相邻的区块移出例程,使它成为一个类(表单)变量,如图所示。

你可能想考虑一个面向对象的方法,其中业务/游戏/程序逻辑脱离UI。使用类来表示每个对象的各种属性,其中一个可以是相邻的区域,并且只需要少量工作,不管它们是否友好

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

请注意,相邻区域的smarts是封装的。由于这些区域在游戏过程中不会移动,存储它们的列表可以防止任何循环、迭代、解析或其他旋转的需要

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

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
结果(部分):

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

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


这不包括谁拥有每一块领土。这需要访问主区域列表或作为
列表(区域)
邻接区域

如果您能更好地解释代码,那就好了我的代码适用于隔壁的郡,比如说,如果我想把部队从肯特转移到伦敦,它是有效的,但是如果我想把部队从肯特转移到白金汉郡(玩家拥有伦敦),它不会把从肯特到伦敦再到白金汉郡之间联系起来,返回false。是的,我明白这部分的要点,但这并不是你试图实现它的方式。解释这是如何应用于数据表的,什么是替罪羊…好的,我的想法是找到带有N_按钮的数组,看看to_按钮的项是否在其中,并且具有相同的背景色,然后返回true,否则将N_按钮设置为其数组中的值,然后使用新值重试。替罪羊是一个按钮,我像空值一样使用它,这样每个数组都有8个您可能感兴趣的项,特别是如果您在视觉上移动军队。如果你愿意的话,它可以让你在更大的县或山区(好的,可能不适用于英格兰)的移动中付出更大的代价,军队可以得到最短的路线,这样他们就不会愚蠢地走最长的路线。出于某种原因,当我将ADJCENT县作为一个类变量时,它只返回“nothing”?是的,您可能需要在某个地方初始化它,可能是在加载函数中。当然,我假设它永远不会改变。顺便说一句,有了这个答案,你可以把替罪羊变成什么也不做,并进行测试。
For Each t As Territory In TerrList
    Console.WriteLine("Here in {0}, our neighbors are:", t.Name)
    Console.WriteLine(String.Join(", ", t.AdjacentTerritories))
Next
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