Random Maze Generator Function

Previous topic - Next topic

Ian Price

Quote from: Kitty Hello on 2010-Dec-08
nah, don't add bumps and make it realistic and whatnot (I mean - ok, maybe it's worth) but I really like it as it is.

I totally agree. I can see real-life all around me - I don't want it in my games as well!
I came. I saw. I played.

Slydog

Quote from: Ian Price on 2010-Dec-08
Quote from: Kitty Hello on 2010-Dec-08
nah, don't add bumps and make it realistic and whatnot (I mean - ok, maybe it's worth) but I really like it as it is.

I totally agree. I can see real-life all around me - I don't want it in my games as well!

Well, since I'm no artist (it took me an hour just to draw the d-pad controls!), anything I did add would probably just end up looking less polished!
I was thinking I *HAD* to, just to compete with the other iPhone games.
How do I get rid of (or reduce) the aliasing you see at the path edges?
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Ian Price

Have you tried SMOOTHSHADING TRUE/FALSE?

Don't know if that works with 3D or not, but it won't hurt to try.
I came. I saw. I played.

Kitty Hello

try a texture, that is solid, but has a black border pixel? Then use smoothshading set to false - just a thouht. The image is quite small.

matchy

I'm trying to convert this code to c++ but I not able to debug. Could someone please test/correct this code so it works please?! The algo crashes in VC and GLB.  :'( :help:

Flat version
Code (glbasic) Select


int maze[9][9];
int maze_width = 8;
int maze_height = 8;

void make_maze() {
int mx = maze_width - 1;
int my = maze_height - 1;
int ypos;
int xpos;
int allx;
int ally;
int alldir;
int x;
int y;
int dir;
float result;
int x2;
int y2;
int temp2;
int pcos[] = {1, 0,-1, 0};
int psin[] = {0, 1, 0,-1};
int lp;
int exit_flag = 0;
int temp;
int farx;
int fary;
float go;
float exact;

for (int fx = 0; fx <= mx; fx++) {
maze[fx][0] = 1;
maze[fx][my] = 1;
}
for (int fy = 0; fy <= my; fy++) {
maze[0][fy] = 1;
maze[mx][fy] = 1;
}
for (int loup = 1; loup <= my / 6; loup++) {
ypos = random(((my - 6) / 2) + 1) * 2;
xpos = random(3) * 2;
for (int fx = 1; fx <= xpos; fx++) {
maze[fx][ypos] = 1;
}
ypos = random(((my - 6) / 2) + 1) * 2;
xpos = mx - (random(3) * 2);
for (int fx = mx; fx <= xpos; fx--) {
maze[fx][ypos] = 1;
}
}

for (int loup = 1; loup <= mx / 6; loup++) {
xpos = random(((mx - 4) / 2) + 1) * 2;
maze[xpos][my - 1] = 1;
maze[xpos][my - 2] = 1;
}

farx = mx;
fary = my;
allx = 0;
ally = 0;
exact++;

while (exit_flag == 0) {
while (exit_flag == 0) {

allx = allx + (pcos[alldir] * exact);
ally = ally + (psin[alldir] * exact);
go = go + exact;

if (go >= farx - exact && (alldir == 0 || alldir == 2)) {
go = 0;
farx = farx - exact;
temp = alldir + 1;
alldir = temp - (4 * (temp > 3)) + (4 * (temp < 0));
}
if (go >= fary - exact && (alldir == 1 || alldir == 3)) {
go = 0;
fary = fary - exact;
temp = alldir + 1;
alldir = temp - (4 * (temp > 3)) + (4 * (temp < 0));
}

if (farx <= 0 || fary <= 0) return; // exit_flag = 1; return} // break;

if (maze[allx][ally] == 1 && (allx < mx - 1 || ally < my - 1)) break;
} // wend


if (allx >= mx || ally >= my) break;

x = allx;
y = ally;
dir = random(4);

while (exit_flag == 0) {
lp = 0;
for (int loup = 0; loup <= 3; loup++) {
result = 1;
for (int lop = 1; lop <= exact + 1; lop++) {
x2 = x + (pcos[loup] * lop);
y2 = y + (psin[loup] * lop);
if (x2 <= 0 || y2 <= 0 || x2 > mx || y2 > my) result = 0;
if (result == 1) {
if (maze[x2][y2] == 1) result = 0;
}
if (result == 1) {
temp = loup + 1;
temp2 = temp - (4 * (temp > 3)) + (4 * (temp < 0));
if (maze[x2 + pcos[temp2]][y2 + psin[temp2]] == 1) result = 0;
}
if (result == 1) {
temp = loup - 1;
temp2 = temp - (4 * (temp > 3)) + (4 * (temp < 0));
if (maze[x2 + pcos[temp2]][y2 + psin[temp2]] == 1) result = 0;
}
} // fend

if (result == 1) {
lp = 0;
break;
}
lp++;
} // fend

if (lp == 4) break;

temp = dir + 1 - random(3);
dir = temp - (4 * (temp > 3)) + (4 * (temp < 0));

result = 1;
for (int lop = 1; lop <= exact + 1; lop++) {
x2 = x + (pcos[dir] * lop);
y2 = y + (psin[dir] * lop);
if (x2 <= 0 || y2 <= 0 || x2 > mx || y2 > my) result = 0;
if (result == 1) {
if (maze[x2][y2] == 1) result = 0;
}
if (result == 1) {
temp = dir + 1;
temp2 = temp - (4 * (temp > 3)) + (4 * (temp < 0));
if (maze[x2 + pcos[temp2]][y2 + psin[temp2]] == 1) result = 0;
}
if (result == 1) {
temp = dir - 1;
temp2 = temp - (4 * (temp > 3)) + (4 * (temp < 0));
if (maze[x2 + pcos[temp2]][y2 + psin[temp2]] == 1) result = 0;
}
} // fend

if (result == 1) {
for (int loup = 1; loup <= exact; loup) {
x = x + pcos[dir];
y = y + psin[dir];
maze[x][y] = 1;
} // fend
}
} // wend
} // wend
} // function





GLB Inline:
Code (glbasic) Select


main()

FUNCTION dummy:
ENDFUNCTION


INLINE
int maze[9][9];
int maze_width = 8;
int maze_height = 8;

ENDINLINE


FUNCTION main:
// DIM maze[9][9]
DEBUG "start\n"
start()
LOCAL ok = form_maze()
draw_maze()
ENDFUNCTION

FUNCTION start:
INLINE
printf("Hello World!\n");
ENDINLINE
ENDFUNCTION

FUNCTION printf: in$
DEBUG in$
ENDFUNCTION

FUNCTION random: n
RETURN RND(n)
ENDFUNCTION

FUNCTION draw_maze:
INLINE
printf("begin\n");
for (int y = 0; y < maze_height; y++) {
for (int x = 0; x < maze_width; x++) {
if (maze[x][y] == 0) {
printf(".");
} else {
printf("X");
}
}
printf("\n");
}
printf("end\n");
ENDINLINE
ENDFUNCTION

FUNCTION form_maze:
INLINE
int mx = maze_width - 1;
int my = maze_height - 1;
int ypos = 0;
int xpos = 0;
int allx = 0;
int ally = 0;
int alldir = 0;
int x = 0;
int y = 0;
int dir = 0;
int result = 0;
int x2 = 0;
int y2 = 0;
int pcos[] = {1, 0,-1, 0};
int psin[] = {0, 1, 0,-1};
int lp = 0;
int exit_flag = 0;
int temp = 0;
int temp2 = 0;
int farx = 0;
int fary = 0;
int fx = 0;
int fy = 0;
int loup = 0;
int lop = 0;
int go = 0;
int exact = 0;

printf("form maze...");

for (fx = 0; fx <= mx; fx++) {
maze[fx][0] = 1;
maze[fx][my] = 1;
}
for (fy = 0; fy <= my; fy++) {
maze[0][fy] = 1;
maze[mx][fy] = 1;
}
for (loup = 1; loup <= my / 6; loup++) {
ypos = random(((my - 6) / 2) + 1) * 2;
xpos = random(3) * 2;
for (fx = 1; fx <= xpos; fx++) {
maze[fx][ypos] = 1;
}
ypos = random(((my - 6) / 2) + 1) * 2;
xpos = mx - (random(3) * 2);
for (fx = mx; fx <= xpos; fx--) {
maze[fx][ypos] = 1;
}
}

for (loup = 1; loup <= mx / 6; loup++) {
xpos = random(((mx - 4) / 2) + 1) * 2;
maze[xpos][my - 1] = 1;
maze[xpos][my - 2] = 1;
}

farx = mx;
fary = my;
allx = 0;
ally = 0;
exact++;


while (exit_flag == 0) { // wa

while (exit_flag == 0) { //wb

allx = allx + (pcos[alldir] * exact);
ally = ally + (psin[alldir] * exact);
go = go + exact;

if (go >= farx - exact && (alldir == 0 || alldir == 2)) {
go = 0;
farx = farx - exact;
temp = alldir + 1;
alldir = temp - (4 * (temp > 3)) + (4 * (temp < 0));
}
if (go >= fary - exact && (alldir == 1 || alldir == 3)) {
go = 0;
fary = fary - exact;
temp = alldir + 1;
alldir = temp - (4 * (temp > 3)) + (4 * (temp < 0));
}

if (farx <= 0 || fary <= 0) return 0; // exit_flag = 1; return} // break;

if (maze[allx][ally] == 1 && (allx < mx - 1 || ally < my - 1)) break;
} // wend b


// if (allx >= mx || ally >= my) break;

x = allx;
y = ally;
dir = random(4);

while (exit_flag == 0) { // wc
lp = 0;
for (loup = 0; loup <= 3; loup++) {
result = 1;
for (lop = 1; lop <= exact + 1; lop++) {
x2 = x + (pcos[loup] * lop);
y2 = y + (psin[loup] * lop);
if (x2 <= 0 || y2 <= 0 || x2 > mx || y2 > my) result = 0;
if (result == 1) {
if (maze[x2][y2] == 1) result = 0;
}
if (result == 1) {
temp = loup + 1;
temp2 = temp - (4 * (temp > 3)) + (4 * (temp < 0));
if (maze[x2 + pcos[temp2]][y2 + psin[temp2]] == 1) result = 0;
}
if (result == 1) {
temp = loup - 1;
temp2 = temp - (4 * (temp > 3)) + (4 * (temp < 0));
if (maze[x2 + pcos[temp2]][y2 + psin[temp2]] == 1) result = 0;
}
} // fend

if (result == 1) {
lp = 0;
break;
}
lp++;
} // fend

if (lp == 4) break;

temp = dir + 1 - random(3);
dir = temp - (4 * (temp > 3)) + (4 * (temp < 0));

result = 1;
for (lop = 1; lop <= exact + 1; lop++) {
x2 = x + (pcos[dir] * lop);
y2 = y + (psin[dir] * lop);
if (x2 <= 0 || y2 <= 0 || x2 > mx || y2 > my) result = 0;
if (result == 1) {
if (maze[x2][y2] == 1) result = 0;
}
if (result == 1) {
temp = dir + 1;
temp2 = temp - (4 * (temp > 3)) + (4 * (temp < 0));
if (maze[x2 + pcos[temp2]][y2 + psin[temp2]] == 1) result = 0;
}
if (result == 1) {
temp = dir - 1;
temp2 = temp - (4 * (temp > 3)) + (4 * (temp < 0));
if (maze[x2 + pcos[temp2]][y2 + psin[temp2]] == 1) result = 0;
}
} // fend

if (result == 1) {
for (loup = 1; loup <= exact; loup) {
x = x + pcos[dir];
y = y + psin[dir];
maze[x][y] = 1;
} // fend
}
} // wend c
} // wend a
ENDINLINE

ENDFUNCTION