CODE FOR THE MAZE

;D HELLO, MY GAME IS PAC-MAN GAME. I WANT THE CODE TO BUILT THE MAZE FOR THE GAME.

I don’t think you’re going to get much help, seeing as:

  1. You’re shouting
  2. Your name is “game”
  3. You’re asking a very unspecific question

Are you asking what the best way to represent a map in memory is, or how to render it to screen, or are you asking about writing a level editor for building the maze?

yup yup

COULDNT HAVE SAID IT ANY BETTER MYSELF lol

no offense, but all caps just makes it look offensive.

http://www.java-gaming.org/index.php?topic=20052.msg161192;topicseen#msg161192

http://www.otcsw.com/maze.php

All the source is there on that page and is free to use. It’s an infinitely scalable maze generating/solving algorithm, although it won’t work exactly for Pac-Man. You’ll have to tweak it so that there’s more than one solvable path to a single location.

In case you’re curious what the maze generation algorithm is, it’s called the “Drunkard’s Walk” algorithm. It is tweaked to generate better mazes, however.

;)than you, the maze which i want it called block grid maze which is use for pac-man
I have the code but it not work and there are many errors and I can not solve it.
I hope anyone have idea or the code can I used for my game. thanks

I already gave you a link to the source code of an advanced maze generator. That’s more than most will do, as your question is ambiguous and simply cannot be answered. Post a specific question with attached source and maybe you’ll get a response.

:)thank you for the link, but I dont understand it, you have code is easy than this.
you know it is the first time I used java to create game
I am sorry because I ask so much :persecutioncomplex:

Hi

Demonpants gave you some excellent suggestions. If you don’t understand the source code, maybe you should rather improve your skills in Java and algorithmic before attempting to create a game. On my view, yes, you ask too much. You can look at sourceforge.net, there are some other examples of PacMan in Java and in other languages.

The maze generator class is not very long at all. See, I can post the whole thing within a single JGO post. It’s also well commented. And for you experienced JGOers, I know I do some lazy things in this code. This also tends to generates mazes where the solution goes from top-left to bottom-left to bottom-right because it generates it through for loops. I have a much improved version but it’s significantly more complicated, so I figured now is not the time to show it. :slight_smile:


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.


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

It’s also incredibly complicated. :stuck_out_tongue:

If you can’t get your head around those, you’ve got some more learning to do. Why not start with pre-made levels so you can practice Java then come back to more advanced techniques later? Or why not just download my source, run it on your machine, then edit it so you have Pac-Man running around? The best way to learn it all is to mess around with it, rather than simply staring at it and saying “I don’t understand this.”

:smiley: thank you very much
I will try the bast to do it
thannnnnk you for your help

By the way, that was copy/pasted from the link I already gave you. ::slight_smile:

The only difference here is that you didn’t need to open a zip file.

people are very lazy (me included ;D)

:smiley: hellow how are you master Demonpants
I read the code which you gave it to me and I run on my machine, but the are errors

public static Room[][] generateMaze(int width, int height, [color=orange]Main frame)[/color]

in Main frame
what you mean (main frame) and it not delacer (what it’s type)

I thought you might ask that. This is simply a reference to the main window in the app that the maze generator is included in so that you can visually see what is happening as it is created (it updates the draw to reflect the maze). You can delete all the references to Main as well as that parameter.

:’( I know I asked too much but I don’t understand what you main
I will tell some thing I am just student and I don’t have iead how to do game by java
but what can I do, because the pac-man game is my project and I must submit after two weeks
can you help me
I am ready to learn more to do it

[quote]but what can I do, because the pac-man game is my project and I must submit after two weeks
[/quote]
Oh my, you’re in trouble :stuck_out_tongue:

If I were in your position, I’d (given the little time you have left):

  1. Learn the very basics of java. The java tutorial is a good starting point: http://java.sun.com/docs/books/tutorial/
  2. Go to http://javaboutique.internet.com/PacMan/. There’s a pacman applet there with source code included.
  3. Study the source and try to get a raw understanding
  4. Implement something similar yourself.

If all fails, just hack your own name in there and submit it to your teacher :persecutioncomplex: ;D
(just kidding, of course! I’d tell on you if you did ;))

By deleting all the references to Main, you would for example do this:

//Was this:
Main main;
foo(bar);

//Becomes this:
foo(bar);

If you know how to use a word processor you should be able to delete any references (should I say “mentions” to be more understandable) to Main.

That’s with all the references deleted. If you can’t solve such a simple type of syntax error and didn’t understand what I meant about deleting the references, you should probably not be working on Pac-Man. Instead, start with something like a bunch of balls bouncing around. Note that the program I linked was one of the very first “games” I made in Java. You’ve got to start a lot smaller than Bomberman or you’ll just be pulling your hair out. Also, I slowly over time added on to this bounce ball game, I didn’t do it all at once. As I learned different things I would implement them.



/**
 * 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.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)
	{
		//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>());
				
				//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.
	 */
	private static void constructRoom(Room[][] maze, int i, int j, ArrayList<Integer> checkedDirs)
	{
		//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);
		
		//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);
			//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>());
				}
				//If there is already a room there, try again.
				else
					constructRoom(maze,i,j,checkedDirs);
			}
		}
		else
			constructRoom(maze,i,j,checkedDirs);
	}
	
	/**
	 * 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)
	{
		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;
				}
			}
		}
	}
}

Note I did not test this at all. I just deleted. Took me 2 minutes.

if he didn’t know the basics of java, his teacher would have to be insane to ask him to program a game.