Advertisement
Pietu1998

Prim's Algorithm

Dec 25th, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var UP = 1, DOWN = 2, RIGHT = 4, LEFT = 8;
  2. var BOXCHARS =[" ", "\u2551", "\u2551", "\u2551", "\u2550", "\u255A", "\u2554", "\u2560",
  3.                "\u2550", "\u255D", "\u2557", "\u2563", "\u2550", "\u2569", "\u2566", "\u256C"];
  4.  
  5. function Wall(x1, y1, x2, y2) {
  6.     this.x1 = x1;
  7.     this.y1 = y1;
  8.     this.x2 = x2;
  9.     this.y2 = y2;
  10. }
  11. Wall.prototype.is = function (other) {
  12.     return (this.x1 == other.x1 && this.y1 == other.y1 && this.x2 == other.x2 && this.y2 == other.y2)
  13.         || (this.x1 == other.x2 && this.y1 == other.y2 && this.x2 == other.x1 && this.y2 == other.y1);
  14. };
  15. Wall.has = function (list, wall) {
  16.     for (var i = 0; i < list.length; i++) {
  17.         if (wall.is(list[i]))
  18.             return true;
  19.     }
  20.     return false;
  21. };
  22.  
  23. function maze(width, height, startx, starty, endx, endy) {
  24.     var cells = [];
  25.     for (var i = 0; i < width; i++) {
  26.         cells[i] = [];
  27.         for (var j = 0; j < height; j++) {
  28.             cells[i][j] = 0;
  29.         }
  30.     }
  31.     var walls = [];
  32.     if (startx > 0) {
  33.         cells[startx][starty] |= LEFT;
  34.         walls.push(new Wall(startx, starty, startx - 1, starty));
  35.     }
  36.     if (starty > 0) {
  37.         cells[startx][starty] |= UP;
  38.         walls.push(new Wall(startx, starty, startx, starty - 1));
  39.     }
  40.     if (startx < width - 1) {
  41.         cells[startx][starty] |= RIGHT;
  42.         walls.push(new Wall(startx, starty, startx + 1, starty));
  43.     }
  44.     if (starty < height - 1) {
  45.         cells[startx][starty] |= DOWN;
  46.         walls.push(new Wall(startx, starty, startx, starty + 1));
  47.     }
  48.     var result = [];
  49.     while (walls.length > 0) {
  50.         var index = Math.floor(Math.random() * walls.length);
  51.         var wall = walls.splice(index, 1)[0];
  52.         if (cells[wall.x2][wall.y2] == 0) {
  53.             if (wall.x2 - wall.x1 == 1) { // moved right
  54.                 cells[wall.x1][wall.y1] |= RIGHT;
  55.                 cells[wall.x2][wall.y2] |= LEFT;
  56.             } else if (wall.x2 > 0)
  57.                 walls.push(new Wall(wall.x2, wall.y2, wall.x2 - 1, wall.y2));
  58.             if (wall.x2 - wall.x1 == -1) { // moved left
  59.                 cells[wall.x1][wall.y1] |= LEFT;
  60.                 cells[wall.x2][wall.y2] |= RIGHT;
  61.             } else if (wall.x2 < width - 1)
  62.                 walls.push(new Wall(wall.x2, wall.y2, wall.x2 + 1, wall.y2));
  63.             if (wall.y2 - wall.y1 == 1) { // moved down
  64.                 cells[wall.x1][wall.y1] |= DOWN;
  65.                 cells[wall.x2][wall.y2] |= UP;
  66.             } else if (wall.y2 > 0)
  67.                 walls.push(new Wall(wall.x2, wall.y2, wall.x2, wall.y2 - 1));
  68.             if (wall.y2 - wall.y1 == -1) { // moved up
  69.                 cells[wall.x1][wall.y1] |= UP;
  70.                 cells[wall.x2][wall.y2] |= DOWN;
  71.             } else if (wall.y2 < height - 1)
  72.                 walls.push(new Wall(wall.x2, wall.y2, wall.x2, wall.y2 + 1));
  73.         } else
  74.             result.push(wall);
  75.     }
  76.     var gwidth = 2 * width + 1, gheight = 2 * height + 1;
  77.     var grid = [];
  78.     for (var i = 0; i < gwidth; i++) {
  79.         grid[i] = [];
  80.         for (var j = 0; j < gheight; j++) {
  81.             grid[i][j] = BOXCHARS[0];
  82.         }
  83.     }
  84.     for (var x = 0; x < gwidth; x++) {
  85.         for (var y = 0; y < gheight; y++) {
  86.             var dirs = 0;
  87.             if ((x == 0 || x == gwidth - 1) && y > 0)
  88.                 dirs |= UP
  89.             if ((x == 0 || x == gwidth - 1) && y < gheight - 1)
  90.                 dirs |= DOWN
  91.             if ((y == 0 || y == gheight - 1) && x > 0)
  92.                 dirs |= LEFT
  93.             if ((y == 0 || y == gheight - 1) && x < gwidth - 1)
  94.                 dirs |= RIGHT
  95.             // Magic. Don't touch.
  96.             if (x % 2 + y % 2 == 0) {
  97.                 if (Wall.has(result, new Wall(x / 2 - 1, y / 2 - 1, x / 2, y / 2 - 1)))
  98.                     dirs |= UP
  99.                 if (Wall.has(result, new Wall(x / 2 - 1, y / 2, x / 2, y / 2)))
  100.                     dirs |= DOWN
  101.                 if (Wall.has(result, new Wall(x / 2 - 1, y / 2 - 1, x / 2 - 1, y / 2)))
  102.                     dirs |= LEFT
  103.                 if (Wall.has(result, new Wall(x / 2, y / 2 - 1, x / 2, y / 2)))
  104.                     dirs |= RIGHT
  105.             }
  106.             grid[x][y] = BOXCHARS[dirs];
  107.         }
  108.     }
  109.     for (var x = 0; x < width; x++) {
  110.         for (var y = 0; y < height; y++) {
  111.             // Magic. Don't touch.
  112.             if (Wall.has(result, new Wall(x, y, x + 1, y)))
  113.                 grid[2 * x + 2][2 * y + 1] = BOXCHARS[UP | DOWN];
  114.             if (Wall.has(result, new Wall(x, y, x, y + 1)))
  115.                 grid[2 * x + 1][2 * y + 2] = BOXCHARS[LEFT | RIGHT];
  116.         }
  117.     }
  118.     return grid;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement