Results 1 to 16 of 16
  1. #1
    air
    air is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default 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) {}
    		}
    	}
    }
    Here is the Room class it relies on
    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;
    	}
    }
    I run it, the error in the Main frame

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Well, seeing as how we have no idea what error came, we can't help.

  3. #3
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    An error would be nice, yes.
    I die a little on the inside...
    Every time I get shot.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    air
    air is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default

    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

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    So the fist thing I suggest you is that go through the each line of the code and try to understand.

  7. #7
    air
    air is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default

    Ok, but I don't understand the function of main frame

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  9. #9
    air
    air is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default

    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;
    }

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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)
    then it means that you haven't copied all of the code, and don't understand it enough to use/modify it. Those methods expect to get an instance of a Class called "Main". From a couple of (just quickly) spotted comments/uses, it appears to at least contain a JFrame if it is not a Class that's extends JFrame. The rest is up to you to figure out. Go back to where you copied that from.

  11. #11
    air
    air is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default

    you mean I must declare JFrame in the class

  12. #12
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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 11:57 AM.

  13. #13
    air
    air is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default

    thank you.
    If you have code for create maze by the java language which it help me in my project I want necessary please

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  15. #15
    demonpants is offline Member
    Join Date
    Apr 2009
    Posts
    1
    Rep Power
    0

    Default

    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 07:52 AM.

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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

  1. Maze
    By soriano in forum New To Java
    Replies: 1
    Last Post: 12-16-2008, 05:40 PM
  2. Maze reader
    By bix in forum New To Java
    Replies: 1
    Last Post: 11-20-2008, 06:06 PM
  3. 3D Maze
    By EternalSolitude in forum New To Java
    Replies: 5
    Last Post: 11-14-2008, 12:51 AM
  4. Java Maze Problem
    By mousey182 in forum New To Java
    Replies: 2
    Last Post: 03-28-2008, 06:29 PM
  5. Maze Help
    By Soda in forum Advanced Java
    Replies: 1
    Last Post: 12-22-2007, 03:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •