2017-08-26 10 views
0

スクリーンショットからわかるように、エラーが発生する場合がありますerrors 私のコードは、その時点で白いブロックの上の点を観察し、他の空のセクションであれば、囲まれていない領域はブロックではなく空でなければなりません。パスジェネレータが期待どおり/意図していない

namespace ConsoleApp1 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     //initiate lists (1 list for object), (2 list for occupied nearby) 
     int[] list1 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list2 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list3 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list4 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list5 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list6 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list7 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list8 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list9 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list10 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list11 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list12 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list13 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list14 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list15 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list16 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[][] lists = new int[][] { list1, list2, list3, list4, list5, list6, list7, list8, list9, list10, list11, list12, list13, list14, list15, list16 }; 

     int[] list1a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list2a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list3a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list4a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list5a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list6a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list7a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list8a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list9a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list10a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list11a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list12a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list13a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list14a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list15a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[] list16a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
     int[][] listsa = new int[][] { list1a, list2a, list3a, list4a, list5a, list6a, list7a, list8a, list9a, list10a, list11a, list12a, list13a, list14a, list15a, list16a }; 

     //assign walls 
     Random random = new Random(); 
     //full 
     // open = 2, unnasigned = 0, block = 1 
     for (int i = 0; i <= 15; i++) 
     { 
      for (int j = 0; j <= 15; j++) 
      { 
       // % chance of block spawning 
       int r = 2; 
       int p = random.Next(1, 11); 
       if (p > 5) { r = 1; } 
       else if (p < 6) { r = 2; } 
       //do we have any surrounding open* blocks? (above,sides) *(not unassigned, OPEN) 
       int[] openspaces = new int[3] { 0, 0, 0 }; //for recording which spaces are open 
       var open = "no"; //assumed to be no 
       //check space to sides and above, but not below as always unassigned 
       try { if (lists[j][i - 1] == 2) { open = "yes"; openspaces[0] = 1; } } catch (IndexOutOfRangeException) { } //above 
       try { if (lists[j + 1][i] == 2) { open = "yes"; openspaces[1] = 1; } } catch (IndexOutOfRangeException) { } //right 
       try { if (lists[j - 1][i] == 2) { open = "yes"; openspaces[2] = 1; } } catch (IndexOutOfRangeException) { } //left 
       if (open == "no") 
       { 
        lists[j][i] = r; // 50/50 chance as completely singular square 
       } 
       else if (open == "yes") 
       { // if the spaces are open, do they have any other open or undecided blocks near them (under, sides) 
        int[] otheropen = new int[3] { 0, 0, 0 }; //for recording which nearby open block have other nearby open spaces 
        if (openspaces[0] == 1) //above 
        { 
         //check space to sides, but not above as not relavent, and below would be checking itself, and none are invalid 
         try { if ((lists[j + 1][i - 1] == 0 || lists[j + 1][i - 1] == 2) && listsa[j + 1][i - 1] != 1) { otheropen[0] = 1; } } catch (IndexOutOfRangeException) { } 
         try { if ((lists[j - 1][i - 1] == 0 || lists[j - 1][i - 1] == 2) && listsa[j - 1][i - 1] != 1) { otheropen[0] = 1; } } catch (IndexOutOfRangeException) { } 
        } 
        else if (openspaces[0] == 0) { otheropen[0] = 2; } // if the block is not open it is ignored 

        if (openspaces[1] == 1) //right 
        { 
         //check space to right and below, but not above as not relavent, and left would be checking itself, and none are invalid 
         try { if ((lists[j + 1][i + 1] == 0 || lists[j][i - 1] == 2) && listsa[j + 1][i + 1] != 1) { otheropen[1] = 1; } } catch (IndexOutOfRangeException) { } 
         try { if ((lists[j + 2][i] == 0 || lists[j + 1][i] == 2) && listsa[j + 2][i] != 1) { otheropen[1] = 1; } } catch (IndexOutOfRangeException) { } 
        } 
        else if (openspaces[1] == 0) { otheropen[1] = 2; } // if the block is not open it is ignored 

        if (openspaces[2] == 1) //left 
        { 
         //check space to left and below, but not above as not relavent, and right would be checking itself, and none are invalid 
         try { if ((lists[j - 1][i + 1] == 0 || lists[j][i - 1] == 2) && listsa[j - 1][i + 1] != 1) { otheropen[2] = 1; } } catch (IndexOutOfRangeException) { } 
         try { if ((lists[j - 2][i] == 0 || lists[j - 1][i] == 2) && listsa[j - 2][i] != 1) { otheropen[2] = 1; } } catch (IndexOutOfRangeException) { } 
        } 
        else if (openspaces[2] == 0) { otheropen[2] = 2; } // if the block is not open it is ignored 

        if (otheropen[0] == 0 || otheropen[1] == 0 || otheropen[2] == 0) { lists[j][i] = 2; } //sets self as open, as a nearby open block has no other open blocks 
        else if (otheropen[0] != 0 && otheropen[1] != 0 && otheropen[2] != 0) 
          //yes all the near open block have a possible other route 
         { 
         lists[j][i] = r; // 50/50 as this block will not create a dead end 
         if (r == 1)//if original block closed then nearby open blocks will become invalid for next check, i.e. considered closed 
          { 
          try { if (otheropen[0] == 1) { listsa[j][i - 1] = 1; } } catch (IndexOutOfRangeException) { }//top 
          try { if (otheropen[1] == 1) { listsa[j + 1][i] = 1; } } catch (IndexOutOfRangeException) { }//right 
          try { if (otheropen[2] == 1) { listsa[j + 1][i] = 1; } } catch (IndexOutOfRangeException) { }//left 
         } 
         } 
       } 

      } 
     } 

     for (int j = 0; j <= 15; j++) 
     { 
      Console.WriteLine(); 
      for (int i = 0; i <= 15; i++) 
      { 
       if (lists[j][i] == 1) { Console.Write("██"); } 
       if (lists[j][i] == 2) { Console.Write("░░"); } 

      } 
     } 

    } 
} 

私はこのエラーが発生している理由を理解できません。試しにチェックしている可能性があります。私はこれが大規模なコードダンプであり、私が投稿したものから解釈するのはかなり難しいことを知っています。私はC#を初めて使っているので、もっと効率的に作業できると確信しています。

私は本当にすべての広場がアクセス可能であるか壁であることを望んでいますが、あなたは壁の壁を見ることができるように、ポイントを全領域に渡ります。

EDIT:プロセスexplination:

だから、大きなアレイを反復。まず、2d配列内の現在の特定の値の辺と値の上に(値2の)「オープンブロック」があるかどうかを調べます。存在しない場合、ブロックが「オープン」または「充填」(それぞれ2または1の値)かどうかをランダムに決定します。

オープンブロックがある場合は、ブロックを囲むブロック(サイドとその下)をチェックして、パスとして機能する他のオープン(2)ブロックがあるかどうかを調べます。それがあれば、これを認め、ランダムに再び選ぶ。これらのブロックの1つに周囲のオープンブロックがない場合、現在の値はオープンブロックになります。

ランダムな選択肢が後者のプロセスの後で 'filled'ブロックである場合、それはnearブロックを同じプロセスに対して再び開かれているとしてもう有効ではないとして割り当てるので、別のブロックがそれができないことを知っている道としてそれを使うことができます。

+0

ようこそ。アルゴリズムの仕組みをもう少し説明できますか?あなたをよく願って – MickyD

+0

問題ないとありがとう。あなたの説明をあなたの質問に加えました。うーん、なぜ悲しいことにエラーが出るのかわからない。 – MickyD

+3

あなたのコードは読みにくいので、焦点を絞るのは難しいです。私は侮辱であるとは言いませんが、少し修正すれば幸運にも助けを求めるかもしれないと思います。たぶんあなたはそうすることで自分自身で問題を見つけるでしょう。例:1 - "yes"/"no"(bool isOpen = false;)の文字列ではなく、ブール値を使用します。2 - 配列の配列の代わりにintの2次元配列を使用する(int [、] lists = new int [height、width];)3バグを調べるには、ランダムでシードを使用してランダム性を削除します。 (ランダムランダム=新しいランダム(3);)4-あなたはパスで "生の"マップを生成し、2番目にそれを固定することを検討する必要があります。 – Tipx

答えて

0

コードを再フォーマットしてより簡単にすると、私はエラーと思われるものを見つけました(少なくとも、これらの変更後に再現するためにエラーが発生することはありません(以下のコードは、あなたはそれがtry { if ...を言うどこを見れば、あなたや文の第2の条件は第一の条件そして異なっていることがわかります

if (openspaces[1] == 1) //right  
    { 
    //check space to right and below, but not above as not relavent, and left would be checking itself, and none are invalid 
    try { if ((lists[j + 1][i + 1] == 0 || lists[j][i - 1] == 2) && listsa[j + 1][i + 1] != 1) { otheropen[1] = 1; } } catch (IndexOutOfRangeException) { } 
    try { if ((lists[j + 2][i] == 0 || lists[j + 1][i] == 2) && listsa[j + 2][i] != 1) { otheropen[1] = 1; } } catch (IndexOutOfRangeException) { } 
} 
else if (openspaces[1] == 0) { otheropen[1] = 2; } // if the block is not open it is ignored 

if (openspaces[2] == 1) //left 
{ 
    //check space to left and below, but not above as not relavent, and right would be checking itself, and none are invalid 
    try { if ((lists[j - 1][i + 1] == 0 || lists[j][i - 1] == 2) && listsa[j - 1][i + 1] != 1) { otheropen[2] = 1; } } catch (IndexOutOfRangeException) { } 
    try { if ((lists[j - 2][i] == 0 || lists[j - 1][i] == 2) && listsa[j - 2][i] != 1) { otheropen[2] = 1; } } catch (IndexOutOfRangeException) { } 
} 

あなたは、このセクションでは、あなたのコード)

に見つけることができます。彼らはする必要がありますコピーペーストエラーの犠牲になったようです。

@Tipxは、あなたのコードを再フォーマットすることを推奨しているので、これをもっと簡単に見つけることができました。 C#を初めてお使いの方は、コードを読みやすくするために最初から学んでください。あなたは後で自分自身に感謝するでしょう。

あなたのコードでは、パスの塊をメインパスから分離することができます(ランダムシードを0に設定して右上隅を見てください)が、あなたが持っていたような個々の四角。

+0

これは@Tipxのヒントを使って非常に参考になりましたが、私はいくつかの適切な変更を行いましたが、読みやすくするためにコードのフォーマットを増やす予定です。今は、分離されたパスがどのように行われるかを把握するだけです。 –

+0

@ GregoryHallam分離されたパスは、2-ishスペースを離れている可能性があるかどうかを確認するためだけにチェックされているために発生します。それは十分ではない時があります(例えばランダムなシード0)。パスが常に存在することを保証するには、可能であれば、最初に各四角形をトレースしなければならないでしょう。注 - 本当に必要な場合は、A *アルゴリズムのようなものでかなり簡単に行うことができます。 –

+0

しかし、@マイケルシャープは、排除のプロセスを経て、私は何が起こるかと何が起こるかは正しいとは思わない、私の理論的な機能はまだ動作するはずですか?シード0の例では、エラーは4行目、15列目で発生します。 この時点で、プロセスは次のようになります。 左上に空きスペースがありますか?はい、上記のものがあります。 上記にはスペースがありますか?右 - いいえ(除外の特別な条件の下では、2番目の配列によって通知されます)、左 - ブロックされないので、オリジナルはオープンな値になります。 –

関連する問題