Advertisement
ramytamer

Untitled

Jan 25th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Maze = Class({
  2.     $statics: {
  3.         N: 1,
  4.         S: 2,
  5.         E: 4,
  6.         W: 8,
  7.         DX: {
  8.             E: 1,
  9.             W: -1,
  10.             N: 0,
  11.             S: 0
  12.         },
  13.         DY: {
  14.             E: 0,
  15.             W: 0,
  16.             N: -1,
  17.             S: 1
  18.         },
  19.         OPPOSITE: {
  20.             E: this.W,
  21.             W: this.E,
  22.             N: this.S,
  23.             S: this.N
  24.         },
  25.         directions: ['N', 'S', 'E', 'W'],
  26.         dirVal: {
  27.             E: this.E,
  28.             W: this.W,
  29.             N: this.N,
  30.             S: this.S
  31.         },
  32.         shuffle: function(arr) {
  33.             for (var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
  34.             return arr;
  35.         },
  36.         create2DArray: function(h, w) {
  37.             var array = [];
  38.             for (var i = 0; i < h; i++) {
  39.                 array[i] = [];
  40.             }
  41.             for (i = 0; i < h; i++) {
  42.                 for (var j = 0; j < w; j++) {
  43.                     array[i][j] = 0;
  44.                 }
  45.             }
  46.             return array;
  47.         }
  48.     },
  49.  
  50.     constructor: function(width, height) {
  51.         this.width = width;
  52.         this.height = height;
  53.  
  54.         this.grid = this.create2DArray(this.height, this.width);
  55.         this.maze = this.create2DArray(2 * this.height + 1, 2 * this.width + 1);
  56.  
  57.  
  58.         this.carve_pass();
  59.         this.toBlocks();
  60.  
  61.         this.toArray();
  62.  
  63.     },
  64.  
  65.     carve_pass: function(cx, cy) {
  66.         cx = cx ? cx : 0;
  67.         cy = cy ? cy : 0;
  68.  
  69.         this.directions = this.shuffle(this.directions);
  70.         for (var i = 0; i < 4; i++) {
  71.             var direction = this.directions[i];
  72.             var nx = cx + this.DX[direction],
  73.                 ny = cy + this.DY[direction];
  74.             if (this.grid && ny >= 0 && ny <= ((this.grid).length - 1) && nx >= 0 && nx <= ((this.grid).length - 1) && this.grid[ny][nx] === 0) {
  75.                 this.grid[cy][cx] |= this.dirVal[direction];
  76.                 this.grid[ny][nx] |= this.OPPOSITE[direction];
  77.                 this.carve_pass(nx, ny);
  78.             }
  79.         }
  80.     },
  81.  
  82.     toBlocks: function() {
  83.         if (this.maze) {
  84.             for (var i = 0; i < 2 * this.width + 1; i++) {
  85.                 this.maze[0][i] = 1;
  86.                 this.maze[2 * this.height][i] = 1;
  87.                 this.maze[i][0] = 1;
  88.                 this.maze[i][this.width * 2] = 1;
  89.             }
  90.             var x, y;
  91.             for (i = 0; i < this.height; i++) {
  92.                 for (var j = 0; j < this.width; j++) {
  93.                     x = i * 2 + 1;
  94.                     y = j * 2 + 1;
  95.                     this.maze[x + 1][y] = (this.grid[i][j] & this.S) === 0;
  96.                     if (this.grid[i][j] & this.E)
  97.                         this.maze[x + 1][y + 1] = ((this.grid[i][j] | this.grid[i][j + 1]) & this.S) === 0;
  98.                     else
  99.                         this.maze[x][y + 1] = this.maze[x + 1][y + 1] = 1;
  100.                 }
  101.             }
  102.         }
  103.     },
  104.  
  105.     toArray: function() {
  106.         if (this.maze) {
  107.             for (var i = 0; i < 2 * this.height + 1; i++) {
  108.                 var line = '';
  109.                 for (var j = 0; j < 2 * this.width + 1; j++) {
  110.                     line += ((this.maze[i][j]) ? '#' : '.');
  111.                 }
  112.                 document.write(line + "<br>");
  113.             }
  114.         }
  115.     },
  116.  
  117.     getMazeArray: function() {
  118.         // console.log(this.width);
  119.         return this.maze;
  120.     }
  121.  
  122.  
  123. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement