Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Random Array Outline Patterns  (Read 6822 times)
0 Members and 1 Guest are viewing this topic.
Offline steveyO
« Posted 2009-12-06 16:24:27 »


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();
     
     // 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  
}

https://play.google.com/store/apps/details?id=com.bullsquared.alggame Annoying Little Gits (Android)
www.bullsquared.com   Play java (applet) games! www.chessclockpro.com Free Online Chess Clock
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #1 - Posted 2009-12-06 16:51:22 »

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?
Offline steveyO
« Reply #2 - Posted 2009-12-06 21:55:52 »

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.

https://play.google.com/store/apps/details?id=com.bullsquared.alggame Annoying Little Gits (Android)
www.bullsquared.com   Play java (applet) games! www.chessclockpro.com Free Online Chess Clock
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2009-12-06 23:07:06 »

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

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline steveyO
« Reply #4 - Posted 2009-12-06 23:17:55 »

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

https://play.google.com/store/apps/details?id=com.bullsquared.alggame Annoying Little Gits (Android)
www.bullsquared.com   Play java (applet) games! www.chessclockpro.com Free Online Chess Clock
Offline Wildern

Junior Member





« Reply #5 - Posted 2009-12-08 15:50:15 »

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.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2009-12-08 17:20:46 »

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

Something like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
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;
}

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline steveyO
« Reply #7 - Posted 2009-12-08 22:39:22 »

@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!!

https://play.google.com/store/apps/details?id=com.bullsquared.alggame Annoying Little Gits (Android)
www.bullsquared.com   Play java (applet) games! www.chessclockpro.com Free Online Chess Clock
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (23 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (32 views)
2014-08-08 02:01:56

Norakomi (42 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!