2017-02-18 9 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時間っぽいについてのために上記のコードでラウンド果たしていると、あなたも、私はあなたの要件は、しかしRecursion2次元配列で何度も検索する

を使用して理解していればあなたの助けをありがとう

+0

コードを少し上手く説明すればいいと思っています.... /やっているかしていないか。 –

+0

私のコードは隣の郡のために働いています、もし私がケントからロンドンに軍隊を移したければ、それは機能しますが、私はケントからバッキンガムシャーに(ロンドンを所有するプレイヤーと共に)軍隊を移動したいと思っていますケントからロンドン、そしてバッキンガムシャーに行き、偽を返します。 – user6878266

+0

私はその部分の要点を得るが、それをどのように実装しようとしているのか分からない。あなたのデータテーブルAdjcentCountiesにどのように適用されるのかを説明してください... –

答えて

0

をいただければ幸いです 任意の前進を行っていません(それはしかし私だった場合

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をクラス変数にすると、「何も返さない」だけですか? – user6878266

+0

おそらく、おそらくロード関数のどこかで初期化する必要があります。私はそれが決して変化しないと仮定しています。 –

+0

BTWこの答えでScapeGoatをNothingに変更して代わりにそれをテストできます。 –

1

ビジネス/ゲーム/プログラムのロジックがUIから分離されている場合、オブジェクト指向のアプローチを検討することをお勧めします。それぞれのさまざまな特性を表現するためにクラスを使用すると、それらのうちの1つは、フレンドリーであろうとなかろうと、隣接する領土であり、ちょっとした仕事である可能性があります。すべての「スケープゴート」プレースホルダが存在しない必要があるように、

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 

このラフアウト」はdoesnの誰が各地域を所有しているかを含む。それには、マスター地域リストまたはAdjacentTerritoriesにはList(Of Territory)としてアクセスする必要があります。