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

2014/11/26

Creating paths in Arrays. - Maze

connorxclose connorxclose

2014/11/26

#
I am currently, creating a maze game and am using an Array to assign my wall blocks. However, these blocks are always random except for the border. I need to create a path to ensure my characters can always fit through and there will always be an entrance and exit. Help? Attached below is the current array for randomly assigning them. public class world extends World { private static final int SIZEY = 24; private static final int SIZEX = 44; private int mapArray = new int; private int map = { {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0}, {1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0}, {1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1}, {1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, {1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0}, {1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1}, {1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0} }; /** * Constructor for objects of class world. * */ public world() { super(1100, 600, 1); fillArray(); popMap(); } public void fillArray() { for (int j=0; j<SIZEY; j++) { for (int i=0; i<SIZEX; i++) { if(i>0 && j>0 && i<SIZEX-1 && j<SIZEY-1) { mapArray=Greenfoot.getRandomNumber(2); } else { mapArray = 1; } } } } public void popMap() { for (int j=0; j<SIZEY; j++) { for (int i=0; i<SIZEX; i++) { if(mapArray == 1) { Wall wall = new Wall(); addObject(wall, ((i * 25) + 13), ((j * 25) + 13)); } } } } }
danpost danpost

2014/11/26

#
Please use the 'code' link below the reply box when inserting code into your posts. If you look above, your arrays do not appear to be posted properly (your square brackets and what you had inside them are not shown -- I believe they are taken as invalid tags and ignored when the content is parsed).
connorxclose connorxclose

2014/11/26

#
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
public class world extends World
{
    private static final int SIZEY = 24;
    private static final int SIZEX = 44;
    private int[][] mapArray = new int[SIZEX][SIZEY];
    private int[][] map = {
            {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
            {1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0},
            {1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0},
            {1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1},
            {1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1},
            {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0},
            {1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0},
            {1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1},
            {1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1},
            {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}
            };
    /**
     * Constructor for objects of class world.
     *
     */
    public world()
    {  
        super(1100, 600, 1);
        fillArray();
        popMap();
    }
     
    public void fillArray()
    {
        for (int j=0; j<SIZEY; j++)
        {
        for (int i=0; i<SIZEX; i++)
        {
            if(i>0 && j>0 && i<SIZEX-1 && j<SIZEY-1)
            {
            mapArray[i][j]=Greenfoot.getRandomNumber(2);
        }
        else {
            mapArray[i][j] = 1;
        }
        }
    }
}
     
    public void popMap()
    {
        for (int j=0; j<SIZEY; j++)
        {
        for (int i=0; i<SIZEX; i++)
        {
            if(mapArray[i][j] == 1)
            {
               Wall wall = new Wall();
               addObject(wall, ((i * 25) + 13), ((j * 25) + 13));
            }
         
    }
}
}
}
danpost danpost

2014/11/26

#
From what I can tell, the 'map' array that you have assigned values to is not being used and can be removed. It is the 'mapArray' array that you are programmatically assigning values to. Good thing, as the 'map' array does not match the world as far as length and height (12x9 instead of 44x24). Anyway, it is not an easy task to trace the map to ensure continuity from one location to another; however, there are several ways to do it (again, none easy). I cannot currently think of an example that you could look at for this; however, I would guess there would be something around that could help.
danpost danpost

2014/11/27

#
Things that might help in creating code for a random maze using walls: (1) use odd dimensions for the size of your maze; doing so will make it an even count from one wall to the opposite wall (and from inside the one wall to inside the opposite wall); (2) pick any starting point along one edge that has an odd valued coordinate (all edges will have at least one coordinate value that is even and half of them will have two evens); do the same on the opposite edge for an ending point; (3) image the body of the maze is completely filled with walls -- you can start at any location with both odd value coordinates as an open location and systematically eliminate wall locations (opening paths), always beginning with an open location and going two squares at a time in any direction to an untouched location provided that location is within the grid; when you can no longer eliminate any, you will have a maze where all open locations will be reachable and it will have one path from the starting point to the end point -- guaranteed.
danpost danpost

2014/12/4

#
I used the technique described above in my Crazy Cheatin' Maze scenario.
You need to login to post a reply.