maze-orig.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. var Maze = function(doc, elemId) {
  2. this.canvas = doc.getElementById(elemId);
  3. this.width = this.canvas.width;
  4. this.height = this.canvas.height;
  5. this.ctx = this.canvas.getContext('2d');
  6. this.horizCells = 30;
  7. this.vertCells = 30;
  8. this.generator = new MazeGenerator(this.horizCells, this.vertCells);
  9. this.cellWidth = this.width / this.horizCells;
  10. this.cellHeight = this.height / this.vertCells;
  11. var self = this;
  12. self.ctx.strokeStyle = "rgb(0, 0, 0)";
  13. self.ctx.fillStyle = "rgba(255, 0, 0, 0.1)";
  14. return {
  15. width: function() {
  16. return self.width;
  17. },
  18. height: function() {
  19. return self.height;
  20. },
  21. generate: function () {
  22. self.generator.generate();
  23. },
  24. draw: function() {
  25. this.drawBorders();
  26. this.drawMaze();
  27. },
  28. solve: function() {
  29. self.generator.solve();
  30. this.drawSolution();
  31. },
  32. drawBorders: function() {
  33. this.drawLine(self.cellWidth, 0, self.width, 0);
  34. this.drawLine(self.width, 0, self.width, self.height);
  35. this.drawLine(self.width - self.cellWidth, self.height, 0, self.height);
  36. this.drawLine(0, self.height, 0, 0);
  37. },
  38. drawSolution: function() {
  39. var path = self.generator.path;
  40. for(var i = 0; i < path.length; i++) {
  41. (function () {
  42. var cell = path[i];
  43. var x = cell.x * self.cellWidth;
  44. var y = cell.y * self.cellHeight;
  45. setTimeout(function() {
  46. self.ctx.fillRect(x, y, self.cellWidth, self.cellHeight);
  47. }, 80 * i);
  48. })();
  49. }
  50. },
  51. drawMaze: function() {
  52. var graph = self.generator.graph;
  53. var drawnEdges = [];
  54. var edgeAlreadyDrawn = function(cell1, cell2) {
  55. return _.detect(drawnEdges, function(edge) {
  56. return _.include(edge, cell1) && _.include(edge, cell2);
  57. }) != undefined;
  58. };
  59. for(var i = 0; i < graph.width; i++) {
  60. for(var j = 0; j < graph.height; j++) {
  61. var cell = graph.cells[i][j];
  62. var topCell = graph.getCellAt(cell.x, cell.y - 1);
  63. var leftCell = graph.getCellAt(cell.x - 1, cell.y);
  64. var rightCell = graph.getCellAt(cell.x + 1, cell.y);
  65. var bottomCell = graph.getCellAt(cell.x, cell.y + 1);
  66. if(!edgeAlreadyDrawn(cell, topCell) && graph.areConnected(cell, topCell)) {
  67. var x1 = cell.x * self.cellWidth;
  68. var y1 = cell.y * self.cellHeight;
  69. var x2 = x1 + self.cellWidth;
  70. var y2 = y1;
  71. this.drawLine(x1, y1, x2, y2);
  72. drawnEdges.push([cell, topCell]);
  73. }
  74. if(!edgeAlreadyDrawn(cell, leftCell) && graph.areConnected(cell, leftCell)) {
  75. var x2 = x1;
  76. var y2 = y1 + self.cellHeight;
  77. this.drawLine(x1, y1, x2, y2);
  78. drawnEdges.push([cell, leftCell]);
  79. }
  80. if(!edgeAlreadyDrawn(cell, rightCell) && graph.areConnected(cell, rightCell)) {
  81. var x1 = (cell.x * self.cellWidth) + self.cellWidth;
  82. var y1 = cell.y * self.cellHeight;
  83. var x2 = x1;
  84. var y2 = y1 + self.cellHeight;
  85. this.drawLine(x1, y1, x2, y2);
  86. drawnEdges.push([cell, rightCell]);
  87. }
  88. if(!edgeAlreadyDrawn(cell, bottomCell) && graph.areConnected(cell, bottomCell)) {
  89. var x1 = cell.x * self.cellWidth;
  90. var y1 = (cell.y * self.cellHeight) + self.cellHeight;
  91. var x2 = x1 + self.cellWidth;
  92. var y2 = y1;
  93. this.drawLine(x1, y1, x2, y2);
  94. drawnEdges.push([cell, bottomCell]);
  95. }
  96. }
  97. }
  98. },
  99. drawLine: function(x1, y1, x2, y2) {
  100. self.ctx.beginPath();
  101. self.ctx.moveTo(x1, y1);
  102. self.ctx.lineTo(x2, y2);
  103. self.ctx.stroke();
  104. }
  105. };
  106. };