Results 1 to 16 of 16
Thread: help in maze code
- 03-25-2009, 09:06 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 6
- Rep Power
- 0
help in maze code
hi I am new in the forums
I want help in maze code
I do game by java language
it is pac-man game I want to do the maze, so anyone have the of maze
I had one one ,but there are some errors
This is the code
Java Code:package mm; /** * This class generates a random solvable maze by first filling in a maze with walls, * then charting a path from the start to the finish (top left to bottom right), then * giving every other piece of the path a random number of exits, until every * space has been evaluated. * @author Eli Delventhal * */ //import java.util.Stack; import java.util.ArrayList; public class MazeGenerator { //Can be changed for mazes that are more than two dimensions. public static final int NUM_DIRECTIONS = 4; //The indeces for the different possible directions. Add more for //mazes that are more than two dimensions. public static final int UP = 0; public static final int LEFT = 1; public static final int RIGHT = 2; public static final int DOWN = 3; /** * Generates a random solvable maze with the top left corner as the start, * with no goal yet specified. The maze has the specified width and height * in terms of number of rooms, and is awlays rectangular. * @param width The width of the maze. * @param height The height of the maze. * @return A solvable maze with the matched width and height. */ public static Room[][] generateMaze(int width, int height) { return generateMaze(width,height,null); } /** * Generates a random solvable maze with the top left corner as the start, * with no goal yet specified. The maze has the specified width and height * in terms of number of rooms, and is awlays rectangular. * @param width The width of the maze. * @param height The height of the maze. * @param frame The JFrame that should be repainting. If null, no pausing or painting. * @return A solvable maze with the matched width and height. */ public static Room[][] generateMaze(int width, int height, Main frame) { //Initialize the maze with no rooms at all (all set to null). Room[][] maze = new Room[width][height]; //Visit all spaces in the maze at least once. for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { //As we get to a point in the maze, we need to create a room there //if there isn't one. Then we need to construct a path from that room. if (maze[i][j] == null) constructRoom(maze,i,j,new ArrayList<Integer>(),frame); //If there is already a room, we simply skip onwards. } } return maze; } /** * Constructs a room of the maze at the specified i and j positions. * It will then continue to call itself until it reaches a point * where it can no longer create more rooms. * @param maze The maze to work with. * @param i The i position to start with. * @param j The j position to start with. * @param checkedDirs The directions that have already been checked. * @param frame The frame to tell to repaint. */ private static void constructRoom(Room[][] maze, int i, int j, ArrayList<Integer> checkedDirs, Main frame) { //If all four directions have been tried, return from the function. if (checkedDirs.size() >= NUM_DIRECTIONS) return; //If we don't already have a room here, create one. if (maze[i][j] == null) addNewRoom(maze,i,j,frame); //Open up a random direction for this room. If that //direction already has a room, //try a different direction. If there is no room there, //call this function at the new position. int direction = (int) (Math.random() * NUM_DIRECTIONS); if (!checkedDirs.contains(direction)) { //Add the current direction to the ones checked. checkedDirs.add(direction); //Create delta i and delta j variables to map change. int di = 0; int dj = 0; if (direction == UP) di = -1; else if (direction == DOWN) di = 1; else if (direction == LEFT) dj = -1; else if (direction == RIGHT) dj = 1; //If the change will take us out of bounds, try again. if (i + di < 0 || i + di >= maze.length || j + dj < 0 || j + dj >= maze[0].length) constructRoom(maze,i,j,checkedDirs,frame); //Otherwise, it's okay to go in our direction. else { //If there isn't a room there, call the function with no checked dirs. if (maze[i+di][j+dj] == null) { maze[i][j].open[direction] = true; constructRoom(maze,i+di,j+dj,new ArrayList<Integer>(),frame); } //If there is already a room there, try again. else constructRoom(maze,i,j,checkedDirs,frame); } } else constructRoom(maze,i,j,checkedDirs,frame); } /** * Adds a new room to the maze, linking it to adjacent rooms correctly. * @param maze The maze. * @param i The i position to add in. * @param j The j position to add in. */ private static void addNewRoom(Room[][] maze, int i, int j, Main frame) { maze[i][j] = new Room(i,j); boolean linked = false; //Search for adjacent rooms already linked to this one and link back to them. if (i > 0 && maze[i-1][j] != null && maze[i-1][j].open[DOWN]) { maze[i][j].open[UP] = true; linked = true; } if (i < maze.length-1 && maze[i+1][j] != null && maze[i+1][j].open[UP]) { maze[i][j].open[DOWN] = true; linked = true; } if (j > 0 && maze[i][j-1] != null && maze[i][j-1].open[RIGHT]) { maze[i][j].open[LEFT] = true; linked = true; } if (j < maze[0].length-1 && maze[i][j+1] != null && maze[i][j+1].open[LEFT]) { maze[i][j].open[RIGHT] = true; linked = true; } //If it wasn't linked to anything, link it to a random adjacent room. //Make sure an adjacent room exists first. if ((i > 0 && maze[i-1][j] != null) || (i < maze.length-1 && maze[i+1][j] != null) || (j > 0 && maze[i][j-1] != null) || (j < maze[0].length-1 && maze[i][j+1] != null)) { while (!linked) { double random = Math.random() * 4; if (random <= 1 && i > 0 && maze[i-1][j] != null) { maze[i][j].open[UP] = true; maze[i-1][j].open[DOWN] = true; linked = true; } else if (random <= 2 && i < maze.length-1 && maze[i+1][j] != null) { maze[i][j].open[DOWN] = true; maze[i+1][j].open[UP] = true; linked = true; } else if (random <= 3 && j > 0 && maze[i][j-1] != null) { maze[i][j].open[LEFT] = true; maze[i][j-1].open[RIGHT] = true; linked = true; } else if (random <= 4 && j < maze[0].length-1 && maze[i][j+1] != null) { maze[i][j].open[RIGHT] = true; maze[i][j+1].open[LEFT] = true; linked = true; } } } //Tell the main class to paint if it has been specified. if (frame != null) { frame.setMaze(maze); frame.repaint(); try {Thread.sleep(frame.getDrawDelay());} catch(Exception e) {} } } }
Java Code:package mm; /** * A Room is a very simple object that contains 4 booleans determining where it has * walls. These walls can be at the north, south, east, or west. These are public * instance variables for simplicity's sake. By default, a room is entirely closed. * @author Eli Delventhal * */ public class Room { public boolean[] open; public int i; public int j; public Room(int ni, int nj) { open = new boolean[MazeGenerator.NUM_DIRECTIONS]; i = ni; j = nj; } }
- 03-25-2009, 09:46 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 15
Well, seeing as how we have no idea what error came, we can't help.
- 03-25-2009, 10:16 AM #3
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 13
An error would be nice, yes.
I die a little on the inside...
Every time I get shot.
- 03-25-2009, 10:29 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
And for me, seems that you have no idea about what you have done in the code.
- 03-25-2009, 12:02 PM #5
Member
- Join Date
- Mar 2009
- Posts
- 6
- Rep Power
- 0
I get the code from the internet and I try to run it but the main frame not found in the class.
I sorry, but I am beginner
- 03-25-2009, 12:20 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
So the fist thing I suggest you is that go through the each line of the code and try to understand.
- 03-25-2009, 12:26 PM #7
Member
- Join Date
- Mar 2009
- Posts
- 6
- Rep Power
- 0
Ok, but I don't understand the function of main frame
- 03-25-2009, 12:32 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Ok, where the main frame is. You've post a long code here and not easy to go through it. Can you specifically show the code segment where you get that issue.
- 03-25-2009, 12:45 PM #9
Member
- Join Date
- Mar 2009
- Posts
- 6
- Rep Power
- 0
this the code
public static Room[][] generateMaze(int width, int height, Main frame) {
//Initialize the maze with no rooms at all (all set to null).
Room[][] maze = new Room[width][height];
//Visit all spaces in the maze at least once.
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
//As we get to a point in the maze, we need to create a room there
//if there isn't one. Then we need to construct a path from that room.
if (maze[i][j] == null)
constructRoom(maze,i,j,new ArrayList<Integer>(),frame);
//If there is already a room, we simply skip onwards.
}
}
return maze;
}
- 03-25-2009, 12:47 PM #10
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 15
If you mean the literal words "Main frame" in
Java Code:public static Room[][] generateMaze(int width, int height, Main frame) private static void constructRoom(Room[][] maze, int i, int j, ArrayList<Integer> checkedDirs, Main frame) private static void addNewRoom(Room[][] maze, int i, int j, Main frame)
- 03-25-2009, 12:53 PM #11
Member
- Join Date
- Mar 2009
- Posts
- 6
- Rep Power
- 0
you mean I must declare JFrame in the class
- 03-25-2009, 12:55 PM #12
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 15
I mean ask the people you got the code from.
Edit: Or at least get the rest of the code so that you can see how it is suppossed to be used, then, maybe you'll be able to modify it (although that shouldn't be necessary to modify it to your use, but we have no idea how "your use" is suppossed to look, so, once again, we can't help you).Last edited by masijade; 03-25-2009 at 12:57 PM.
- 03-25-2009, 01:05 PM #13
Member
- Join Date
- Mar 2009
- Posts
- 6
- Rep Power
- 0
thank you.
If you have code for create maze by the java language which it help me in my project I want necessary please
- 03-25-2009, 02:38 PM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Read what masijade carefully. He said that, because he want to help you in much better way. If you try to do something first, you can learn a lot.
On the other hand, no one wants to do your stuff here, as we explain to you several times. Please keep in mind too.
- 04-01-2009, 08:45 AM #15
Member
- Join Date
- Apr 2009
- Posts
- 1
- Rep Power
- 0
I don't recommend anyone else reply to him.
I'm the creator of that code, I gave it to him over at javagaming.org in response to: "HELLO, MY GAME IS PAC-MAN GAME. I WANT THE CODE TO BUILT THE MAZE FOR THE GAME."
Yeah. He doesn't actually want to understand anything, he just wants code he can copy / paste in a school project. The syntax errors he's getting are simply because of missing references - if he knew anything about Java syntax he would know (and also he would know because I told him so) that he could simply delete the references to Main and then it would run just fine.
If you're curious - the reference to Main is so that there is somewhere to draw to. The point of the code was to illustrate in real-time how a simple maze generation algorithm works, as well as solver algorithms. Hence the Thread.sleep() in there.
You can view the running app here: atche tee tee pee:// dub dub dub .otcsw.com/maze.php
Also, I happened to see this on a Google result when I narcissistically looked up my own name. :)Last edited by demonpants; 04-01-2009 at 08:52 AM.
- 04-01-2009, 09:20 AM #16
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
This is really bad thing going on. No need to worry lol. Some people behave like that, is shameless way. And also I don't think that anyone wants to replay him as well.
Similar Threads
-
Maze
By soriano in forum New To JavaReplies: 1Last Post: 12-16-2008, 06:40 PM -
Maze reader
By bix in forum New To JavaReplies: 1Last Post: 11-20-2008, 07:06 PM -
3D Maze
By EternalSolitude in forum New To JavaReplies: 5Last Post: 11-14-2008, 01:51 AM -
Java Maze Problem
By mousey182 in forum New To JavaReplies: 2Last Post: 03-28-2008, 07:29 PM -
Maze Help
By Soda in forum Advanced JavaReplies: 1Last Post: 12-22-2007, 04:26 AM
Bookmarks