473,513 Members | 2,401 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bug in Prims Random Maze generator

8 New Member
Hi guys,

I was looking at random maze generators on the web. I found this prims algorithm but everytime I run it, it doesnt display the random maze as it should. It just displays a blank applet. Also, does any one know how to save this generated applet to a png or jpg format? Source code is attached below. Thanks.

************************************************** *******************************************

package ares;

import java.awt.*;
import java.util.*;
import java.applet.*;

public class Prim extends Applet implements Runnable
{
public static final int WTOP = 1;
public static final int WRGT = 2;
public static final int WBOT = 4;
public static final int WLFT = 8;

int cells[][];
Point current_cell;
Vector inlist;
Vector outlist;
Vector frontlist;
int gridw, gridh, cellsize;
Image offscreen;
Graphics offgr;
Thread t;

public void init()
{
int x, y;
String param;

/* Get applet params */
param = getParameter("gridw");
gridw = Integer.parseInt(param);
param = getParameter("gridh");
gridh = Integer.parseInt(param);
param = getParameter("cellsize");
cellsize = Integer.parseInt(param);

cells = new int[gridw][gridh];
/* Or together the wall bits to show that all
* the walls are up.
*/
int full = WTOP | WBOT | WLFT | WRGT;
for (x = 0; x < gridw; x++)
for (y = 0; y < gridh; y++)
cells[x][y] = full;
/* Then, mark the borders
*/
int left = WLFT << 4;
int right = WRGT << 4;
for (y = 0; y < gridh; y++)
{
cells[0][y] |= left;
cells[gridw-1][y] |= right;
}
int top = WTOP << 4;
int bottom = WBOT << 4;
for (x = 0; x < gridw; x++)
{
cells[x][0] |= top;
cells[x][gridh-1] |= bottom;
}
offscreen = createImage((gridw+2)*cellsize, (gridh+2)*cellsize);
offgr = offscreen.getGraphics();
}

public void start()
{
t = new Thread(this);
t.start();
}

public void stop()
{
t.stop();
t = null;
}

public void run()
{
int dir, x, y;

/*
* Implement Prim's algorithm to build maze
*/
outlist = new Vector(gridw*gridh);
inlist = new Vector(10,10);
frontlist = new Vector(10,10);
for (x = 0; x < gridw; x++)
for (y = 0; y < gridh; y++)
outlist.addElement(new Point(x,y));
current_cell = (Point)rndElement(outlist);
inlist.addElement(current_cell);
moveNbrs(current_cell);

while (!frontlist.isEmpty())
{
current_cell = (Point)rndElement(frontlist);
inlist.addElement(current_cell);
moveNbrs(current_cell);
dir = findInNbr(current_cell);
removeWall(current_cell, dir);
/*
* Break for the animation effect
*/
repaint();
try
{
Thread.sleep(50);
} catch (Exception e) {};
}
/*
* All done
*/
current_cell = null;
repaint();
}

public void update(Graphics g)
{
paint(offgr);
g.drawImage(offscreen,0,0,this);
}

public void paint(Graphics g)
{
int val, x ,y;

int basex = 10;
int basey = 10;
g.setColor(Color.white);
g.fillRect(basex, basey, gridw*cellsize, gridh*cellsize);
g.setColor(Color.black);
for (x = 0; x < gridw; x++)
for (y = 0; y < gridh; y++)
{
val = cells[x][y];
if ((val & WTOP) != 0)
g.drawLine(basex+x*cellsize, basey+y*cellsize,
basex+(x+1)*cellsize, basey+y*cellsize);
if ((val & WRGT) != 0)
g.drawLine(basex+(x+1)*cellsize-1, basey+y*cellsize,
basex+(x+1)*cellsize-1, basey+(y+1)*cellsize);
if ((val & WBOT) != 0)
g.drawLine(basex+x*cellsize, basey+(y+1)*cellsize-1,
basex+(x+1)*cellsize, basey+(y+1)*cellsize-1);
if ((val & WLFT) != 0)
g.drawLine(basex+x*cellsize, basey+y*cellsize,
basex+x*cellsize, basey+(y+1)*cellsize);
}
/*
* Draw the current_cell as well
*/
g.setColor(Color.red);
if (current_cell != null)
g.fillOval(basex+current_cell.x*cellsize, basey+current_cell.y*cellsize,
cellsize, cellsize);
}

/*
* The following routines provide access to the underlying
* maze data structure.
*/
int findInNbr(Point p)
{
/* Return a random direction in which the point p has
* a neighbor which is in inlist.
*/
int d = rnd(4)-1;
int k = 0;
while (k < 4)
{
switch(d)
{
case 0: /* Top nbr? */
if ((cells[p.x][p.y] & (WTOP<<4)) != 0) break;
if (inlist.indexOf(new Point(p.x,p.y-1)) >= 0)
return WTOP;
break;
case 1: /* Right nbr? */
if ((cells[p.x][p.y] & (WRGT<<4)) != 0) break;
if (inlist.indexOf(new Point(p.x+1,p.y)) >= 0)
return WRGT;
break;
case 2: /* Bottom nbr? */
if ((cells[p.x][p.y] & (WBOT<<4)) != 0) break;
if (inlist.indexOf(new Point(p.x,p.y+1)) >= 0)
return WBOT;
break;
case 3: /* Left nbr? */
if ((cells[p.x][p.y] & (WLFT<<4)) != 0) break;
if (inlist.indexOf(new Point(p.x-1,p.y)) >= 0)
return WLFT;
break;
}
d = (d+1) % 4;
k++;
}
return 0; // This shouldn't ever happen
}

void moveNbrs(Point p)
{
Point s;

/*
* Move any neighbors of p which are in outlist from
* outlist to frontlist.
*/
if ((cells[p.x][p.y] & (WTOP<<4)) == 0)
{
s = new Point(p.x, p.y-1);
movePoint(s, outlist, frontlist);
}
if ((cells[p.x][p.y] & (WRGT<<4)) == 0)
{
s = new Point(p.x+1, p.y);
movePoint(s, outlist, frontlist);
}
if ((cells[p.x][p.y] & (WBOT<<4)) == 0)
{
s = new Point(p.x, p.y+1);
movePoint(s, outlist, frontlist);
}
if ((cells[p.x][p.y] & (WLFT<<4)) == 0)
{
s = new Point(p.x-1, p.y);
movePoint(s, outlist, frontlist);
}
}

void movePoint(Point p, Vector v, Vector w)
{
/*
* If p is element of v, move it to w
*/
int i = v.indexOf(p);
if (i >= 0)
{
v.removeElementAt(i);
w.addElement(p);
}
}

void removeWall(Point p, int d)
{
/* Exclusive or bit with cell to drop wall
*/
cells[p.x][p.y] ^= d;
/*
* And drop neighboring wall as well
*/
switch(d)
{
case WTOP: cells[p.x][p.y-1] ^= WBOT;
break;
case WRGT: cells[p.x+1][p.y] ^= WLFT;
break;
case WBOT: cells[p.x][p.y+1] ^= WTOP;
break;
case WLFT: cells[p.x-1][p.y] ^= WRGT;
break;
}
}

// Utility routines
int rnd(int n)
{
return (int)(Math.random()*n+1);
}

Object rndElement(Vector v)
{
int i = rnd(v.size())-1;
Object s = v.elementAt(i);
v.removeElementAt(i);
return s;
}

}

************************************************** ******************************************
Nov 3 '07 #1
2 3426
r035198x
13,262 MVP
1.) Use codetags when posting code
2.) Go back to where-ever you got that code and ask the person who wrote that code.
Nov 3 '07 #2
dev24
8 New Member
Hi,

Apologies for not using the code tags. And this is a prims algorithm for generating maze. This was an exam paper for a university so cant ask anyone. The link is as below:

[HTML]http://www.mscs.mu.edu/~mikes/198.1999/exam331/[/HTML]

Besides the problem is not in actual code. The problem is with something trivial.

Anyway,

no worries,

dev
Nov 6 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
3690
by: Brandon Michael Moore | last post by:
I'm trying to test a web application using a tool written in python. I would like to be able to generate random values to put in fields. I would like to be able to generate random dates (in a...
28
3653
by: Paul Rubin | last post by:
http://www.nightsong.com/phr/python/sharandom.c This is intended to be less predicable/have fewer correlations than the default Mersenne Twister or Wichmann-Hill generators. Comments are...
3
7357
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I...
5
3326
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
104
5036
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
13
3163
by: porterboy76 | last post by:
If you only use a 32 bit seed for a random number generator, does that mean you can only ever produce a maximum of 2^32 (approx 4 billion) different sequences? What about the Mersenne Twister,...
2
3351
by: Matthew Wilson | last post by:
The random.jumpahead documentation says this: Changed in version 2.3: Instead of jumping to a specific state, n steps ahead, jumpahead(n) jumps to another state likely to be separated by many...
4
6541
by: wohast | last post by:
Hi, I'm stuck at the very beginning of my current assignment. I just need a little help getting started, and then I know how to do everything else. ask user: "Please enter your name followed by...
0
7386
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7106
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7534
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5094
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3236
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3226
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1601
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
805
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.