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

2013/5/15

Randomly generated dungeon

1
2
3
4
henrYoda henrYoda

2013/5/15

#
Hi all, I recently had an idea of making a randomly generated dungeon game, like the game "The binding of isaac" However, I have absolutely no idea how to do anything of the sort. I was thinking that I had to represent the world like this: 00000 00x00 y000y Where 0 = nothing and x = player and y = enemy. Am I on the right track? Do you guys know how I could do this? Thank you for reading, -henrYoda
Really, it's your decision how you lay out the world, you can use a 2D array like you suggest, or you can do it all manually through the constructor. If you want to do it with a 2D array (which is a great idea if all your worlds will be the same size, and you have the same basic formula for each world) you will have to make a method that would read the array and place it on the screen. Here's an example of how I'd do it:
public void setItemsFromArray(String[] world)
{
    for (int i = 0; i < world.length; i++)
        for (int j = 0; j < world[i].length; j++)
        {
            if (world[i][j].equals("x"))
                addObject(new Player(), i * /*Specific Constant*/, j * /*Specific Constant*/);
            if (world[i][j].equals("y"))
                addObject(new Enemy(), i * /*Specific Constant*/, j * /*Specific Constant*/);
            ...
        }
}
Use something like this to make your world, or you can find a new way to do it.
I'm not familiar with the use of traversing through 2D arrays, but I'm pretty sure that's how you would do it.
henrYoda henrYoda

2013/5/16

#
Hi, but how would I create the worlds, I'm not familiar with 2d arrays myself
each world would be the same size (if you're planning on using same sized 2d arrays) and you create the worlds on the size you want. What I would recommend is a superclass for your Worlds that has the method from above, the constant of your world (which should be world width or height / width or height of the array), and would keep all the worlds the same size. Then all you'd have to do is make subclasses that give the 2D array to the superclass.
guigowls guigowls

2013/5/16

#
henry its wernerpeed lol.
henrYoda henrYoda

2013/5/16

#
Right, got it, but is the code above all I need to get it working? Is that what a 2d array is, you see, I just can't really understand it all that well....
so you have almost all the code in the superclass, but then your sublcasses will give the superclass the array. Something like this:
public class MainWorldClass /*Or whatever you want to call it*/
{
    private String[][] world;

    public MainWorldClass(String[][] array)
    {
        world = array;
    }

    //other methods
    ...
}
Then any subclasses would be like this:
public ASubClass extends MainWorldClass
{
    public ASubClass()
    {
        super(/*Insert 2D array here*/);
    }
}
but if you want each world to be random, you wouldn't have to make as many subclasses, just make a method that makes a random 2D array.
henrYoda henrYoda

2013/5/16

#
Right, now its beginning to make sense. But what does a 2d array look like, I don't know what to type into that area where you told me to insert the 2d array. Could you give me an example that would work with the rest of the code you showed me please?
Ah, now this I know:
int[][] myTwoDArray = {
                                      {1, 2, 3, 4, 5},
                                      {6, 7, 8, 9, 10},
                                      {11, 12, 13, 14, 15}
                                    };

//gets height of a 2D array (it equals 3)
int height = myTwoDArray.length;

//gets width of a 2D array (it equals 5)
int width = myTwoDArray[0].length;
If you're confused about how the width works (like I was when I first learned a bit about them) think of it like this: A 2D array is basically an array of arrays, so element 0 of the 2D array is the first array of the array of arrays (a 2D array), therefore you can get the width of the 2D array by getting the length of one of the arrays in the 2D array. Hope I didn't confuse you there. :)
and yes, you can make 3D arrays, 4D arrays, and so forth, but you will never need to use more than 2D arrays in almost all cases.
danpost danpost

2013/5/16

#
You can also check out the Java tutorial on Arrays.
henrYoda henrYoda

2013/5/16

#
@FlyingRabidUnicorn thank you so much for your help, I will try your code as soon as I can(Fixing my computer, so i'm on my Raspberry pi). @danpost Thank you for sharing that link with me, it definitely helped me a lot
henrYoda henrYoda

2013/5/16

#
Oh and to make it random, would I make it get a random number? like Greenfoot.getRandomNumber(Some number)?
Entity1037 Entity1037

2013/5/16

#
Yes. "Greenfoot.getRandomNumber(x);" is the exact method to use. However it can be very... well not so random. If you tell it to get a random number using 360 to 0, then all of the numbers will be multiples of 45. Try using the random number method from actual java; you'll have to type the import for it and I can't remember what the method is, but it shouldn't be too hard to look it up. I hope this helps!
There are more replies on the next page.
1
2
3
4