开发者

How to find all connected numbers in an array?

Hey all, back again. Working on a dungeon generator and I'm actually surprising myself with the progress. Yet I still have a straggling room every now and then. I was wondering if there was a way to loop through an array and see if all the '1s' (the floor tiles) are connected, and if not, how to connect them.

Thanks!

EDIT: The array is randomly filled with rooms and corridors; here's the code:

import java.util.Random;
public class Level
{
  Random random = new Random();
  int[][] A = new int[100][100];
  int minimum = 3;
  int maximum = 7;
  int xFeature = 0;
  int yFeature = 0;
private void feature()
  {
    int i = 0;
    while(i>=0)
    {
      xFeature = random.nextInt(100-1) + 1;
      yFeature = random.nextInt(100-1) + 1;
      if(A[xFeature][yFeature]==1)//||A[xFeature++][yFeature]==1||A[xFeature][yFeature--]==1||A[xFeature][yFeature++]==1)
        break;
      i++;
    }
  }


  private void room()
  {
    int safeFall = 0;
    int xCoPLUS = minimum + (int)(Math.random()*minimum);
    int yCoPLUS = minimum + (int)(Math.random()*minimum);
    if(yCoPLUS >= xCoPLUS)
    {
      for(int across = xFeature; across < xFeature+xCoPLUS+2; across++) 
      {
        for(int vert = yFeature; vert < yFeature+yCoPLUS+1; vert++)
        {
          if(A[vert][across] == 0)
            safeFall++;
          else
            break;
        }
      }
    }
    if(yCoPLUS < xCoPLUS)
    {
      for(int across = xFeature; across < xFeature+xCoPLUS+1; across++) 
      {
        for(int vert = yFeature; vert < yFeature+yCoPLUS+2; vert++)
        {
          if(A[vert][across] == 0)
            safeFall++;
          else
            break;
        }
      }
    }
    if((safeFall== (xCoPLUS+1) * (yCoPLUS+2)) || ((safeFall== (xCoPLUS+2) * (yCoPLUS+1))))
    {
      for(int across = xFeature; across < xFeature+xCoPLUS; across++) 
      {
        for(int vert = yFeature; vert < yFeature+yCoPLUS; vert++)
        {
          A[vert][across] = 1;
        }
      }
    }
  }
private void corridor()
  {

    int xCoONE = xFeature;
    int yCoONE = yFeature;
    int xCoTWO = random.nextInt(10)+10;
    int yCoTWO = random.nextInt(10)+10;
    while(xCoONE > xCoTWO)
    {
      A[xCoONE][yCoONE] = 1;
      xCoONE--;
    }
    while(xCoONE < xCoTWO)
    {
      A[xCoONE][yCoONE] = 1;
      xCoONE++;
    }
    while(yCoONE > yCoTWO)
    {
      A[xCoONE][yCoONE] = 1;
      yCoONE--;
    }
    while(yCoONE < yCoTWO)
    {
      A[xCoONE][yCoONE] = 1;
      yCoONE++;
    }
}
public Level()
  {
    first开发者_运维知识库room();
    for(int i = 0; i < 500; i++)
    {
      int x = random.nextInt(50);
      feature();
      if(x > 1)
        room();
      else
        corridor();
    }
    troubleShoot();
  }

So basically what happens when I create an object of this class is that a 100x100 array is filled with corridors and rooms determined by a random number. (well, a couple of them) But with how I have my room non-overlapping failsafe (safeFall in room()), I get stuck with a room that is one title out of reach every now and then.

How to find all connected numbers in an array?


The article Maze Generation Algorithm discusses several approaches to generating a maze. It includes links to Java examples.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜