Advertisement
ramytamer

maze with jsface class

Jan 25th, 2015
186
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 arr = new Array(h);
  38.             for (i = 0; i < h; i++) {
  39.                 arr[i] = new Array(w);
  40.             }
  41.  
  42.             return arr;
  43.         }
  44.     },
  45.  
  46.     constructor: function(width, height) {
  47.         this.width = width;
  48.         this.height = height;
  49.  
  50.         this.grid = this.create2DArray(this.height, this.width);
  51.         this.maze = this.create2DArray(2 * this.height + 1, 2 * this.width + 1);
  52.  
  53.  
  54.         this.carve_pass();
  55.         this.toBlocks();
  56.  
  57.         this.toArray();
  58.  
  59.     },
  60.  
  61.     carve_pass: function(cx, cy) {
  62.         cx = cx ? cx : 0;
  63.         cy = cy ? cy : 0;
  64.  
  65.         this.directions = this.shuffle(this.directions);
  66.         for (var i = 0; i < 4; i++) {
  67.             var direction = this.directions[i];
  68.             var nx = cx + this.DX[direction],
  69.                 ny = cy + this.DY[direction];
  70.             if (this.grid && ny >= 0 && ny <= ((this.grid).length - 1) && nx >= 0 && nx <= ((this.grid).length - 1) && this.grid[ny][nx] === 0) {
  71.                 this.grid[cy][cx] |= this.dirVal[direction];
  72.                 this.grid[ny][nx] |= this.OPPOSITE[direction];
  73.                 this.carve_pass(nx, ny);
  74.             }
  75.         }
  76.     },
  77.  
  78.     toBlocks: function() {
  79.         if (this.maze) {
  80.             for (var i = 0; i < 2 * this.width + 1; i++) {
  81.                 this.maze[0][i] = 1;
  82.                 this.maze[2 * this.height][i] = 1;
  83.                 this.maze[i][0] = 1;
  84.                 this.maze[i][this.width * 2] = 1;
  85.             }
  86.             var x, y;
  87.             for (i = 0; i < this.height; i++) {
  88.                 for (var j = 0; j < this.width; j++) {
  89.                     x = i * 2 + 1;
  90.                     y = j * 2 + 1;
  91.                     this.maze[x + 1][y] = (this.grid[i][j] & this.S) === 0;
  92.                     if (this.grid[i][j] & this.E)
  93.                         this.maze[x + 1][y + 1] = ((this.grid[i][j] | this.grid[i][j + 1]) & this.S) === 0;
  94.                     else
  95.                         this.maze[x][y + 1] = this.maze[x + 1][y + 1] = 1;
  96.                 }
  97.             }
  98.         }
  99.     },
  100.  
  101.     toArray: function() {
  102.         for (var i = 0; i < 2 * this.height + 1; i++) {
  103.             var line = '';
  104.             for (var j = 0; j < 2 * this.width + 1; j++) {
  105.                 line += ((this.maze[i][j]) ? '#' : '.');
  106.             }
  107.             document.write(line + "<br>");
  108.         }
  109.     },
  110.  
  111.     getMazeArray: function() {
  112.         // console.log(this.width);
  113.         return this.maze;
  114.     }
  115.  
  116.  
  117. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement