Random Array Outline Patterns

I am trying to generate some random patterns (in shape of a room) in a 2x2 array. Its proving a little harder than I thought… (For 4K game so looking for few lines of code solution). Some example patterns of what I am after are commented in the Source Code.

Anyone has some clever algorithms/tricks/ideas to do this? Can’t think how to do this without looping over the array multiple times.



import java.util.Random;

public class p {

	public static void main(String[] args) {
	  int W=6;
	  int H=12;
		        
	  int test[][] = new int[H][W];    		            
	  Random rand = new Random();
	  
	  // Generate Random Room
      for (int w=0; w<W; w++) {
        for (int h=0; h<H; h++) {
		   if (h %(H-1)  ==0 || w % (W-1) ==0 ) {  // Fills the outline of the array with a '1'.  Would like is a random pattern here..
		       test[h][w] = 1;
		   }
		}
	  }            
		                
   // Show Ouput
   String out="";
   
	for (int w=0; w<W; w++) {
		for (int h=0; h<H; h++) { 
		   out+=test[h][w]; 
		}
    System.out.println(out); out="";
    } 
		
	/* 
	 * This gives (rectangular room with walls):
111111111111
100000000001
100000000001
100000000001
100000000001
111111111111
     
     but really want a random pattern for example
111111110000
100000010000
111000011111
001000000001
001000000001
001111111111     
     
     or 

111110001111
100010001001
100011111001
100000000001
100000000111
111111111100
          
	 */
	
		            
  }  // End Main	
}


maybe you are doing this but:

couldnt you just loop through each square and then do a random to decide whether or not to make it blocked?

Well this would just generate a random array of 0’s and 1s. Am looking for some AI that will generate a Random Path that resembles a room.

Taken from your desired results, you’re simply wanting to fill and outline rectangles on top of eachother.

Yeah thats pretty much what I want. Just cant think of an quick and easy way to put the results into my array.

Random walking? Start in the upper left and go either down or to the right, once you hit the bottom you can go right or up, once you hit the right, you can go up or left and at the top you can go left or down.

Something like this:


int w = 16;
int h = 16;
int rects = 13;
int[] grid = new int[w*h];

for(int i=0; i<rects; i++)
{
   int x0 = (int)(Math.random()*w)-(w>>2);
   int y0 = (int)(Math.random()*h)-(h>>2);
   int x1 = (int)(Math.random()*w)+x0;
   int y1 = (int)(Math.random()*h)+y0;

   for(int y=y0; y<=y1; y++)
       for(int x=x0; x<=x1; x++)
           if((x|y)>=0 && (x<w) && (y<h))
              grid[y*w+x] = (x==x0 || y==y0 || x==x1 || y==y1) ? 1 : 0;
}

@Wildern. I considered doing this but the problem with random walking is once you go all round the room you are unlikely to finish where you started off, having left over unwanted walls.

@Riven… Thanks for code… Its very nearly what am after… Am also trying out a solution using Geometry (areas of rectangles intersecting each other) Have nearly spent as long on this as the entire game… Doh!!