This site requires JavaScript, please enable it in your browser!
Greenfoot back
Razzo
Razzo wrote ...

2012/10/12

2Dimensional Arrays and Rooms

Razzo Razzo

2012/10/12

#
My plan is to generate rooms like this, Make entire int array with the value 1 in each, Which makes a "Room"
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
This will make the world all tiles that are the value 1 (walls) I then want to cutout rooms, changing the ones to 0's Randomly. I then want to make paths from a coord to a random coord inside the room. how could I even ATTEMPT this?
MatheMagician MatheMagician

2012/10/12

#
Well, first you would need to make an array and fill it with those values, and while you fill it up, you can randomly decide which ones not to fill in:
int[][] grid= new int[20][20];//create array

public Whatever() //replace "whatever" with the name of the class
{
        for(int i=0;i<20;i++)
        for(int j=0;j<20;j++)
        if(Greenfoot.getRandomNumber(30)>10) //goes through the array and randomly decides when to assign 1 to a value
               grid[i][j] = 1;
}
This code take advantage of the fact that when you create an array, its values are automatically set to 0. As for drawing a path, you could do something like this:
int x = 10;//whatever you want the starting x to be
int y = 10;//whatever you want the starting y to be
int x2 = Greenfoot.getRandomNumber(20);
int y2 = Greenfoot.getRandomNumber(20);
for(int i=0;i<Math.abs(x-x2);i++)
         grid[x+i*((x2-x)/Math.abs(x-x2))][y] = 0;
for(int i=0;i<Math.abs(y-y2);i++)
         grid[x][y+i*((y2-y)/Math.abs(y-y2))] = 0;
this will generate an L-shaped path to your destination.
Razzo Razzo

2012/10/12

#
Here is my code that I have so far. It's just the Rooms I want to look organised, right now they overlap, and also sometimes there isn't a path that connects to x = dontplace, y = 17.
    public int[][] makeData()
    {
        int amtrooms = 4;
        int[][] d2array = new int[20][20];
        for(int x = 0; x < 20; x++)
        {
            for(int y = 0; y < 20; y++)
            {
                d2array[x][y] = 1;
            }
        }
        for(int rooms = 0; rooms < amtrooms; rooms++)
        {
            int x1 = Greenfoot.getRandomNumber(15);
            int y1 = Greenfoot.getRandomNumber(15);
            int x2 = x1 + Greenfoot.getRandomNumber(2) + 4;
            int y2 = y1 + Greenfoot.getRandomNumber(2) + 4;
            for(int x = 0; x < 20; x++)
            {
                for(int y = 0; y < 20; y++)
                {
                    if(((x >= x1 && x <= x2) || (x <= x1 && x >= x2)) && ((y >= y1 && y <= y2) || (y <= y1 && y >= y2)))
                    {
                        d2array[x][y] = 0;
                    }
                    //if(y>y2 && y < 18 && x == (x1+x2)/2) d2array[x][y] = 0;
                    if(x==dontplace && y > 15) d2array[x][y] = 0; // dontplace is Where the character spawns. Always 20*32 on the Y axis
                    //if(y==17 && ((x > (x1+x2)/2) && (x <= dontplace) || (x < (x1+x2)/2) && (x >= dontplace))) d2array[x][y] = 0;
                }
            }
            int x5 = (x1+x2)/2;//whatever you want the starting x to be
            int y5 = y2;//whatever you want the starting y to be
            int x6 = dontplace;
            int y6 = 17;
            for(int i=0;i<Math.abs(x5-x6);i++)
                d2array[x5+i*((x6-x5)/Math.abs(x5-x6))][y5] = 0;
            for(int i=0;i<Math.abs(y5-y6);i++)
                d2array[x5][y5+i*((y6-y5)/Math.abs(y5-y6))] = 0;
        }
        return d2array;
    }
MatheMagician MatheMagician

2012/10/13

#
Well, I need to go to bed now. I will return to this conversation tomorrow morning. To solve your path code, I think the solution will be something like this:
int x = 10;//whatever you want the starting x to be  
int y = 10;//whatever you want the starting y to be  
int x2 = Greenfoot.getRandomNumber(20);  
int y2 = Greenfoot.getRandomNumber(20);  
for(int i=0;i<Math.abs(x-x2);i++)  
         grid[x+i*((x2-x)/Math.abs(x-x2))][y] = 0;  
for(int i=0;i<Math.abs(y-y2);i++)  
         grid[x][y+i*((y2-y)/Math.abs(y-y2))] = 0;  
MatheMagician MatheMagician

2012/10/13

#
I'm back! If you are having anymore trouble, let me know. Disregard last post, I did not see you already implemented my methods.
MatheMagician MatheMagician

2012/10/13

#
It might be, though I haven't tried it, that you need to change the path creating codes from:
for(int i=0;i<Math.abs(x5-x6);i++)  
            d2array[x5+i*((x6-x5)/Math.abs(x5-x6))][y5] = 0;  
        for(int i=0;i<Math.abs(y5-y6);i++)  
            d2array[x5][y5+i*((y6-y5)/Math.abs(y5-y6))] = 0;  
to:
for(int i=0;i<Math.abs(x5-x6);i++)  
            d2array[x5+i*((x6-x5)/Math.abs(x5-x6))][y6] = 0;  
        for(int i=0;i<Math.abs(y5-y6);i++)  
            d2array[x5][y5+i*((y6-y5)/Math.abs(y5-y6))]=0;    
MatheMagician MatheMagician

2012/10/13

#
I assigned the unchanged part of the array reference a different variable. It works now!
You need to login to post a reply.