Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var UP = 1, DOWN = 2, RIGHT = 4, LEFT = 8;
- var BOXCHARS =[" ", "\u2551", "\u2551", "\u2551", "\u2550", "\u255A", "\u2554", "\u2560",
- "\u2550", "\u255D", "\u2557", "\u2563", "\u2550", "\u2569", "\u2566", "\u256C"];
- function Wall(x1, y1, x2, y2) {
- this.x1 = x1;
- this.y1 = y1;
- this.x2 = x2;
- this.y2 = y2;
- }
- Wall.prototype.is = function (other) {
- return (this.x1 == other.x1 && this.y1 == other.y1 && this.x2 == other.x2 && this.y2 == other.y2)
- || (this.x1 == other.x2 && this.y1 == other.y2 && this.x2 == other.x1 && this.y2 == other.y1);
- };
- Wall.has = function (list, wall) {
- for (var i = 0; i < list.length; i++) {
- if (wall.is(list[i]))
- return true;
- }
- return false;
- };
- function maze(width, height, startx, starty, endx, endy) {
- var cells = [];
- for (var i = 0; i < width; i++) {
- cells[i] = [];
- for (var j = 0; j < height; j++) {
- cells[i][j] = 0;
- }
- }
- var walls = [];
- if (startx > 0) {
- cells[startx][starty] |= LEFT;
- walls.push(new Wall(startx, starty, startx - 1, starty));
- }
- if (starty > 0) {
- cells[startx][starty] |= UP;
- walls.push(new Wall(startx, starty, startx, starty - 1));
- }
- if (startx < width - 1) {
- cells[startx][starty] |= RIGHT;
- walls.push(new Wall(startx, starty, startx + 1, starty));
- }
- if (starty < height - 1) {
- cells[startx][starty] |= DOWN;
- walls.push(new Wall(startx, starty, startx, starty + 1));
- }
- var result = [];
- while (walls.length > 0) {
- var index = Math.floor(Math.random() * walls.length);
- var wall = walls.splice(index, 1)[0];
- if (cells[wall.x2][wall.y2] == 0) {
- if (wall.x2 - wall.x1 == 1) { // moved right
- cells[wall.x1][wall.y1] |= RIGHT;
- cells[wall.x2][wall.y2] |= LEFT;
- } else if (wall.x2 > 0)
- walls.push(new Wall(wall.x2, wall.y2, wall.x2 - 1, wall.y2));
- if (wall.x2 - wall.x1 == -1) { // moved left
- cells[wall.x1][wall.y1] |= LEFT;
- cells[wall.x2][wall.y2] |= RIGHT;
- } else if (wall.x2 < width - 1)
- walls.push(new Wall(wall.x2, wall.y2, wall.x2 + 1, wall.y2));
- if (wall.y2 - wall.y1 == 1) { // moved down
- cells[wall.x1][wall.y1] |= DOWN;
- cells[wall.x2][wall.y2] |= UP;
- } else if (wall.y2 > 0)
- walls.push(new Wall(wall.x2, wall.y2, wall.x2, wall.y2 - 1));
- if (wall.y2 - wall.y1 == -1) { // moved up
- cells[wall.x1][wall.y1] |= UP;
- cells[wall.x2][wall.y2] |= DOWN;
- } else if (wall.y2 < height - 1)
- walls.push(new Wall(wall.x2, wall.y2, wall.x2, wall.y2 + 1));
- } else
- result.push(wall);
- }
- var gwidth = 2 * width + 1, gheight = 2 * height + 1;
- var grid = [];
- for (var i = 0; i < gwidth; i++) {
- grid[i] = [];
- for (var j = 0; j < gheight; j++) {
- grid[i][j] = BOXCHARS[0];
- }
- }
- for (var x = 0; x < gwidth; x++) {
- for (var y = 0; y < gheight; y++) {
- var dirs = 0;
- if ((x == 0 || x == gwidth - 1) && y > 0)
- dirs |= UP
- if ((x == 0 || x == gwidth - 1) && y < gheight - 1)
- dirs |= DOWN
- if ((y == 0 || y == gheight - 1) && x > 0)
- dirs |= LEFT
- if ((y == 0 || y == gheight - 1) && x < gwidth - 1)
- dirs |= RIGHT
- // Magic. Don't touch.
- if (x % 2 + y % 2 == 0) {
- if (Wall.has(result, new Wall(x / 2 - 1, y / 2 - 1, x / 2, y / 2 - 1)))
- dirs |= UP
- if (Wall.has(result, new Wall(x / 2 - 1, y / 2, x / 2, y / 2)))
- dirs |= DOWN
- if (Wall.has(result, new Wall(x / 2 - 1, y / 2 - 1, x / 2 - 1, y / 2)))
- dirs |= LEFT
- if (Wall.has(result, new Wall(x / 2, y / 2 - 1, x / 2, y / 2)))
- dirs |= RIGHT
- }
- grid[x][y] = BOXCHARS[dirs];
- }
- }
- for (var x = 0; x < width; x++) {
- for (var y = 0; y < height; y++) {
- // Magic. Don't touch.
- if (Wall.has(result, new Wall(x, y, x + 1, y)))
- grid[2 * x + 2][2 * y + 1] = BOXCHARS[UP | DOWN];
- if (Wall.has(result, new Wall(x, y, x, y + 1)))
- grid[2 * x + 1][2 * y + 2] = BOXCHARS[LEFT | RIGHT];
- }
- }
- return grid;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement