2017-02-18 111 views
-2
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 

對於我的計算項目,我製作了一個版本風險,這需要玩家攻擊後允許他們將部隊從一個縣移到另一個縣,如果他們由擁有的縣由同一個玩家(在我的遊戲中用按鈕的背景表示)。我一直在玩一輪,此代碼上面關於最後4小時十歲上下,並沒有取得任何進展 你的幫助,將不勝感激感謝通過二維陣列搜索多次

+0

如果你更好地解釋了代碼,那就好了.... /它是或不在做什麼。 –

+0

我的代碼適用於隔壁的縣,如果我想將部隊從肯特搬到倫敦,它的工作原理是,但如果我想將部隊從肯特搬到白金漢郡(與擁有倫敦的玩家一起),它並不會將從肯特到倫敦然後是白金漢郡,並返回假。 – user6878266

+0

雅我得到那部分的要點,但不是你如何實現它。解釋如何適用於您的數據表AdjcentCounties和什麼替罪羊是... –

回答

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出常規的,並使其成爲一個類(形式)變量,如圖所示。

+0

由於某些原因,當我使AdjcentCounties成爲類變量時,它只返回「Nothing」? – user6878266

+0

雅你可能需要初始化它的某個地方,可能在加載函數。我假設它永遠不會改變。 –

+0

順便說一句,您可以將ScapeGoat更改爲Nothing,然後進行測試。 –

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,我們的鄰居是:
赫特福德郡,林斯頓上,伯克郡,牛津郡,北安普敦郡,貝德福德
在這裏,在赫特福德郡,我們的鄰居是:
劍橋郡,埃塞克斯,倫敦,Buckinhamshire,貝德福德
在這裏,在貝德福德,我們的鄰居是:
劍橋郡,赫特福德郡,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)