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
}