Guest User

Untitled

a guest
Apr 19th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1.  
  2. Array.prototype.shuffle = function () {
  3. var i, j, tempi, tempj;
  4. i = this.length;
  5. if ( i === 0 ) {
  6. return false;
  7. }
  8. while ( --i ) {
  9. j = Math.floor( Math.random() * ( i + 1 ) );
  10. tempi = this[i];
  11. tempj = this[j];
  12. this[i] = tempj;
  13. this[j] = tempi;
  14. }
  15. };
  16.  
  17. Number.prototype.times = function (fn) {
  18. for (var i = 0; i < this; i += 1) {
  19. fn(i);
  20. }
  21. };
  22.  
  23. var Maze;
  24.  
  25. (function () {
  26.  
  27. function get_root(node) {
  28. if (node.parent) {
  29. return get_root(node.parent);
  30. }
  31. return node;
  32. }
  33.  
  34. function check_wall(first, second) {
  35. var first_root, second_root;
  36. first_root = get_root(first);
  37. second_root = get_root(second);
  38. if (first_root === second_root) {
  39. return true;
  40. }
  41. first_root.parent = second_root;
  42. return false;
  43. }
  44.  
  45. Maze = function (width, height) {
  46. var sets = [];
  47. var order = [];
  48. for (var i = 0; i < width * height; i += 1) {
  49. sets.push({});
  50. order.push(i * 2);
  51. order.push(i * 2 + 1);
  52. }
  53. order.shuffle();
  54.  
  55. order.forEach(function (i) {
  56. var first, second;
  57. if (i >= sets.length) {
  58. // Right Wall
  59. first = sets[i - sets.length];
  60. if (i % width === width - 1) {
  61. first.right = true;
  62. // Skip right wall for last column of cells
  63. return;
  64. }
  65. second = sets[i - sets.length + 1];
  66. first.right = check_wall(first, second);
  67. } else {
  68. // Bottom Wall
  69. first = sets[i];
  70. if (Math.floor(i / width) === height - 1) {
  71. first.bottom = true;
  72. // Skip bottom wall for last row of cells
  73. return;
  74. }
  75. second = sets[i + width];
  76. first.bottom = check_wall(first, second);
  77. }
  78. });
  79.  
  80. this.width = width;
  81. this.height = height;
  82. this.sets = sets;
  83. }
  84.  
  85. Maze.prototype.display = function () {
  86. var x, y, map, item, grid, line;
  87. grid = [];
  88. map = "██";
  89. line = [1];
  90. for (x = 0; x < this.width; x += 1) {
  91. map += "████";
  92. line.push(1);
  93. line.push(1);
  94. }
  95. grid.push(line);
  96. for (y = 0; y < this.height; y += 1) {
  97. map += "\n██";
  98. line = [1];
  99. for (x = 0; x < this.width; x += 1) {
  100. item = this.sets[y * this.width + x];
  101. map += item.right ? " ██" : " ";
  102. line.push(0);
  103. line.push(item.right ? 1 : 0);
  104. }
  105. grid.push(line);
  106. map += "\n██";
  107. line = [1];
  108. for (x = 0; x < this.width; x += 1) {
  109. item = this.sets[y * this.width + x];
  110. var itemr = this.sets[y * this.width + x + 1];
  111. var itemb = this.sets[y * this.width + x + this.width];
  112. map += item.bottom ? "██" : " ";
  113. line.push(item.bottom ? 1 : 0);
  114. map += (item.right || item.bottom || itemr.bottom || itemb.right) ? "██" : " ";
  115. line.push((item.right || item.bottom || itemr.bottom || itemb.right) ? 1 : 0);
  116. }
  117. grid.push(line);
  118. }
  119. map += "\n";
  120. // sys.p(grid);
  121. return map;
  122. };
  123.  
  124. }());
  125. if (__filename === process.ARGV[1]) {
  126. var sys = require('sys');
  127. var width, height;
  128. // Check that there are two extra arguments and that they are both non-zero numbers
  129. if (process.ARGV.length == 4 && (width = parseInt(process.ARGV[2])) && (height = parseInt(process.ARGV[3]))) {
  130. sys.print((new Maze(width, height)).display());
  131. } else {
  132. sys.puts("USAGE:\n\t" + process.ARGV[1] + " width height")
  133. }
  134. }
  135.  
  136.  
  137. // Sample output for a 5x5 maze
  138. // ██████████████████████
  139. // ██ ██ ██
  140. // ██ ██████ ██ ██████
  141. // ██ ██ ██
  142. // ██ ██████ ██ ██████
  143. // ██ ██ ██ ██
  144. // ██ ██████████████ ██
  145. // ██ ██ ██
  146. // ██ ██████ ██████ ██
  147. // ██ ██ ██ ██
  148. // ██████████████████████
Add Comment
Please, Sign In to add comment