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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| 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(); for (int w=0; w<W; w++) { for (int h=0; h<H; h++) { if (h %(H-1) ==0 || w % (W-1) ==0 ) { test[h][w] = 1; } } } 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=""; } } } |