2016-05-15 17 views
0

私はトップダウンシューティングゲーム用のJavaでマップジェネレータを開発しています。核兵器のようなものを実装しています。地図を作成して床タイルを作成する「ウォーカー」 を作成します。 は1タイルを移動した後に90度、-90度、180度になる可能性があります。奇妙な結果を返すマップジェネレータ

希望の数のフロアタイルが配置されるまで、そのウォーカーは走ります。

この機能は時には時間がかかり、動作する時に、床タイルがそれよりも少ないマップを返し、常に同じエスケープパターンを持ちます。これは出力例です:

00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000 
00000000000000000000000000010000000000000000000000 
00000000000000000000000000011000000000000000000000 
00000000000000000000000000001111000000000000000000 
00000000000000000000000000000001100000000000000000 
00000000000000000000000000000000100000000000000000 
00000000000000000000000000000000110000000000000000 
00000000000000000000000000000000011000000000000000 
00000000000000000000000000000000001100000000000000 
00000000000000000000000000000000000111000000000000 
00000000000000000000000000000000000001110000000000 
00000000000000000000000000000000000000010000000000 
00000000000000000000000000000000000000011000000000 
00000000000000000000000000000000000000001100000000 
00000000000000000000000000000000000000000111000000 
00000000000000000000000000000000000000000001000000 
00000000000000000000000000000000000000000001100000 
00000000000000000000000000000000000000000000100000 
00000000000000000000000000000000000000000000110000 
00000000000000000000000000000000000000000000011000 
00000000000000000000000000000000000000000000001000 
00000000000000000000000000000000000000000000001110 
00000000000000000000000000000000000000000000000011 

1はフロアタイルです。 0は何も開始しません。

この地図には44個の床タイルしかなく、generateMap機能が50個の希望の床タイルで呼び出されました。

ここではその私のコード:

public static int[][]generateMap(int floorTiles){ 
    int map[][] = new int[floorTiles][floorTiles]; 
    int currentX, currentY; 
    for (int x = 0; x<floorTiles;x++){ 
     for (int y = 0; y<floorTiles;y++){ 
      map[x][y] = 0; 
     } 
    } 
    boolean movingThroughX = true; 
    boolean Forward = true; 
    Random rand = new Random(); 
    int counter = 0; 
    int decide = 0; 
    currentX = (floorTiles/8)*2 + (int)(Math.random() * ((floorTiles/8)*6 - (floorTiles/8)*2) + 1); 
    currentY = (floorTiles/8)*2 + (int)(Math.random() * ((floorTiles/8)*6 - (floorTiles/8)*2) + 1); 
    while(counter < floorTiles){ 
     if(map[currentX][currentY] ==0){ 
      map[currentX][currentY] = 1; 
      counter++; 
     } 

     //aply movement 
     if(movingThroughX){ 
      if(Forward){ 
       if(currentX<floorTiles-1){ 
        currentX++; 
       } 
      }else{ 
       if(currentX>floorTiles+1){ 
        currentX--; 
       } 
      } 
     }else{ 
      if(Forward){ 
       if(currentY<floorTiles-1){ 
        currentY++; 
       } 
      }else{ 
       if(currentY>floorTiles+1){ 
        currentY--; 
       } 
      } 
     } 

     decide = rand.nextInt(100); 
     if(decide<20){ 
      //keep walking forward 
     } 
     else if(decide<45){ 
      //turn 90degres 
      if(movingThroughX){ 
       if(Forward){ 
        movingThroughX = false; 
        Forward = false; 
       }else{ 
        movingThroughX = false; 
        Forward = true; 
       } 
      }else{ 
       if(Forward){ 
        movingThroughX = true; 
        Forward = true; 
       }else{ 
        movingThroughX = false; 
        Forward = true; 
       } 
      } 
     }else if(decide<70){ 
      //turn -90degres 
      if(movingThroughX){ 
       if(Forward){ 
        movingThroughX=false; 
        Forward = true; 
       }else{ 
        movingThroughX = false; 
        Forward = false; 
       } 
      }else{ 
       if(Forward){ 
        movingThroughX = true; 
        Forward = false; 
       }else{ 
        movingThroughX = true; 
        Forward = true; 
       } 
      } 
     }else{ 
      //turn 180 degres 
      Forward = !Forward; 
     } 


    } 
    return map; 

} 

私はそれと間違っていただきました!の見当がつかない。

+0

を取り組みました。 4つの異なるケースはありませんか? – usr2564301

+0

あなたは正しいですが、私はまだ昇天の客を得て、予想以上の余裕がないので、問題はありませんでした。 –

+0

movingThroughXとmovingThroughYはありませんか?私はXだけを見るだけです。また、ブール値で方向を表現する代わりに、次のステップで位置の変化を表すために "dx"と "dy"の整数を使うことを強くお勧めします。それらを印刷すれば、移動する方向を簡単に見ることができます。これにより、コードをデバッグするのがずっと簡単になります。 – Luke

答えて

0

私はそれはあなたが二度 `movingThroughX` /` Forward`に同じ値を使用

public static int[][]generateMap(int floorTiles){ 
    int map[][] = new int[floorTiles][floorTiles]; 
    int currentX, currentY; 
    for (int x = 0; x<floorTiles;x++){ 
     for (int y = 0; y<floorTiles;y++){ 
      map[x][y] = 0; 
     } 
    } 
    int dx, dy; 
    dx = 0; 
    dy= 1; 
    Random rand = new Random(); 
    int counter = 0; 
    int decide = 0; 
    currentX = (floorTiles/8)*2 + (int)(Math.random() * ((floorTiles/8)*6 - (floorTiles/8)*2) + 1); 
    currentY = (floorTiles/8)*2 + (int)(Math.random() * ((floorTiles/8)*6 - (floorTiles/8)*2) + 1); 
    while(counter < floorTiles){ 

     //rotate 
     decide = rand.nextInt(100); 
     if(decide<25){ 

     }else if(decide<50){ 
      if(dx == 1){ 
       dy = -1; 
       dx = 0; 
      }else if(dx == -1){ 
       dy = 1; 
       dx = 0; 
      }else if(dy == 1){ 
       dx = 1; 
       dy = 0; 
      }else{ 
       dx = -1; 
       dy = 0; 
      } 
     }else if(decide<75){ 
      if(dx == 1){ 
       dy = 1; 
       dx = 0; 
      }else if(dx == -1){ 
       dy = -1; 
       dx = 0; 
      }else if(dy == 1){ 
       dx = -1; 
       dy = 0; 
      }else{ 
       dx = 1; 
       dy = 0; 
      } 
     }else{ 
      dx = -dx; 
      dy = -dy; 
     } 


     //aply movement 
     currentX += dx; 
     currentY += dy; 
     currentX = Math.min(Math.max(0,currentX),floorTiles-1); 
     currentY = Math.min(Math.max(0,currentY),floorTiles-1); 


     //place tiles 

     if(map[currentX][currentY] == 0){ 
      map[currentX][currentY] = 1; 
      counter++; 
     } 


    } 
    return map; 

}