Код
(function ()
{
// generate labyrinth
var WIDTH = 61, HEIGHT = 61;
var labyrinth = new Array(HEIGHT);
var i, j;
for(i = 0; i < HEIGHT; i++)
{
labyrinth[i] = new Array(WIDTH);
for(j = 0; j < WIDTH; j++)
labyrinth[i][j] = '#';
}
function excavate(x, y)
{
labyrinth[y][x] = ' ';
var rnd = Math.floor(Math.random() * 4);
for(var i = 0; i < 4; i++)
{
switch((rnd + i) % 4)
{
case 0: // top
if(y > 2 && labyrinth[y-2][x] === '#')
{
labyrinth[y-1][x] = ' ';
excavate(x, y-2);
}
break;
case 1: // right
if(x < WIDTH-2 && labyrinth[y][x+2] === '#')
{
labyrinth[y][x+1] = ' ';
excavate(x+2, y);
}
break;
case 2: // down
if(y < HEIGHT-2 && labyrinth[y+2][x] === '#')
{
labyrinth[y+1][x] = ' ';
excavate(x, y+2);
}
break;
case 3: // left
if(x > 2 && labyrinth[y][x-2] === '#')
{
labyrinth[y][x-1] = ' ';
excavate(x-2, y);
}
break;
}
}
}
excavate(1, 1);
for(i = 0; i < HEIGHT; i++)
{
var str = '';
for(j = 0; j < WIDTH; j++)
str = str + labyrinth[i][j];
print(str);
}
// make visual
var CELL_SIZE = 2.0;
var vd = { // visual desc
ceil: {
textures : ["laby\\laby_ceil"],
blender : "default",
primitive_type : Visual.PT_QUAD_LIST,
vertex_format : Visual.VF_POSITION|Visual.VF_NORMAL|Visual.VF_TEXCOORD,
vertices : []
},
floor: {
textures : ["laby\\laby_sand"],
blender : "default",
primitive_type : Visual.PT_QUAD_LIST,
vertex_format : Visual.VF_POSITION|Visual.VF_NORMAL|Visual.VF_TEXCOORD,
vertices : []
},
walls: {
textures : ["laby\\laby_brick"],
blender : "default",
primitive_type : Visual.PT_QUAD_LIST,
vertex_format : Visual.VF_POSITION|Visual.VF_NORMAL|Visual.VF_TEXCOORD,
vertices : []
}
};
// refs
var v_ceil = vd.ceil.vertices, v_floor = vd.floor.vertices, v_walls = vd.walls.vertices;
for(i = 1; i < HEIGHT-1; i++)
{
for(j = 1; j < WIDTH-1; j++)
{
if(labyrinth[i][j] === '#')
continue;
v_ceil.push({ point: [j*CELL_SIZE, CELL_SIZE, i*CELL_SIZE], normal: [0, -1, 0], tc: [0, 0] });
v_ceil.push({ point: [(j+1)*CELL_SIZE, CELL_SIZE, i*CELL_SIZE], normal: [0, -1, 0], tc: [1, 0] });
v_ceil.push({ point: [(j+1)*CELL_SIZE, CELL_SIZE, (i+1)*CELL_SIZE], normal: [0, -1, 0], tc: [1, 1] });
v_ceil.push({ point: [j*CELL_SIZE, CELL_SIZE, (i+1)*CELL_SIZE], normal: [0, -1, 0], tc: [0, 1] });
v_floor.push({ point: [j*CELL_SIZE, 0, (i+1)*CELL_SIZE], normal: [0, 1, 0], tc: [0, 1] });
v_floor.push({ point: [(j+1)*CELL_SIZE, 0, (i+1)*CELL_SIZE], normal: [0, 1, 0], tc: [1, 1] });
v_floor.push({ point: [(j+1)*CELL_SIZE, 0, i*CELL_SIZE], normal: [0, 1, 0], tc: [1, 0] });
v_floor.push({ point: [j*CELL_SIZE, 0, i*CELL_SIZE], normal: [0, 1, 0], tc: [0, 0] });
if(labyrinth[i][j-1] === '#')
{
v_walls.push({ point: [j*CELL_SIZE, CELL_SIZE, i*CELL_SIZE], normal: [1, 0, 0], tc: [0, 0] });
v_walls.push({ point: [j*CELL_SIZE, CELL_SIZE, (i+1)*CELL_SIZE], normal: [1, 0, 0], tc: [1, 0] });
v_walls.push({ point: [j*CELL_SIZE, 0, (i+1)*CELL_SIZE], normal: [1, 0, 0], tc: [1, 1] });
v_walls.push({ point: [j*CELL_SIZE, 0, i*CELL_SIZE], normal: [1, 0, 0], tc: [0, 1] });
}
if(labyrinth[i][j+1] === '#')
{
v_walls.push({ point: [(j+1)*CELL_SIZE, CELL_SIZE, (i+1)*CELL_SIZE], normal: [-1, 0, 0], tc: [1, 0] });
v_walls.push({ point: [(j+1)*CELL_SIZE, CELL_SIZE, i*CELL_SIZE], normal: [-1, 0, 0], tc: [0, 0] });
v_walls.push({ point: [(j+1)*CELL_SIZE, 0, i*CELL_SIZE], normal: [-1, 0, 0], tc: [0, 1] });
v_walls.push({ point: [(j+1)*CELL_SIZE, 0, (i+1)*CELL_SIZE], normal: [-1, 0, 0], tc: [1, 1] });
}
if(labyrinth[i-1][j] === '#')
{
v_walls.push({ point: [(j+1)*CELL_SIZE, CELL_SIZE, i*CELL_SIZE], normal: [0, 0, 1], tc: [1, 0] });
v_walls.push({ point: [j*CELL_SIZE, CELL_SIZE, i*CELL_SIZE], normal: [0, 0, 1], tc: [0, 0] });
v_walls.push({ point: [j*CELL_SIZE, 0, i*CELL_SIZE], normal: [0, 0, 1], tc: [0, 1] });
v_walls.push({ point: [(j+1)*CELL_SIZE, 0, i*CELL_SIZE], normal: [0, 0, 1], tc: [1, 1] });
}
if(labyrinth[i+1][j] === '#')
{
v_walls.push({ point: [j*CELL_SIZE, CELL_SIZE, (i+1)*CELL_SIZE], normal: [0, 0, -1], tc: [1, 0] });
v_walls.push({ point: [(j+1)*CELL_SIZE, CELL_SIZE, (i+1)*CELL_SIZE], normal: [0, 0, -1], tc: [0, 0] });
v_walls.push({ point: [(j+1)*CELL_SIZE, 0, (i+1)*CELL_SIZE], normal: [0, 0, -1], tc: [0, 1] });
v_walls.push({ point: [j*CELL_SIZE, 0, (i+1)*CELL_SIZE], normal: [0, 0, -1], tc: [1, 1] });
}
}
}
new Visual('?maze', vd);
})();