123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- var Maze = function(doc, elemId) {
- this.canvas = doc.getElementById(elemId);
- this.width = this.canvas.width;
- this.height = this.canvas.height;
- this.ctx = this.canvas.getContext('2d');
- this.horizCells = 30;
- this.vertCells = 30;
- this.generator = new MazeGenerator(this.horizCells, this.vertCells);
- this.cellWidth = this.width / this.horizCells;
- this.cellHeight = this.height / this.vertCells;
- var self = this;
- self.ctx.strokeStyle = "rgb(0, 0, 0)";
- self.ctx.fillStyle = "rgba(255, 0, 0, 0.1)";
- return {
- width: function() {
- return self.width;
- },
- height: function() {
- return self.height;
- },
- generate: function () {
- self.generator.generate();
- },
- draw: function() {
- this.drawBorders();
- this.drawMaze();
- },
- solve: function() {
- self.generator.solve();
- this.drawSolution();
- },
- drawBorders: function() {
- this.drawLine(self.cellWidth, 0, self.width, 0);
- this.drawLine(self.width, 0, self.width, self.height);
- this.drawLine(self.width - self.cellWidth, self.height, 0, self.height);
- this.drawLine(0, self.height, 0, 0);
- },
- drawSolution: function() {
- var path = self.generator.path;
- for(var i = 0; i < path.length; i++) {
- (function () {
- var cell = path[i];
- var x = cell.x * self.cellWidth;
- var y = cell.y * self.cellHeight;
- setTimeout(function() {
- self.ctx.fillRect(x, y, self.cellWidth, self.cellHeight);
- }, 80 * i);
- })();
- }
- },
- drawMaze: function() {
- var graph = self.generator.graph;
- var drawnEdges = [];
- var edgeAlreadyDrawn = function(cell1, cell2) {
- return _.detect(drawnEdges, function(edge) {
- return _.include(edge, cell1) && _.include(edge, cell2);
- }) != undefined;
- };
- for(var i = 0; i < graph.width; i++) {
- for(var j = 0; j < graph.height; j++) {
- var cell = graph.cells[i][j];
- var topCell = graph.getCellAt(cell.x, cell.y - 1);
- var leftCell = graph.getCellAt(cell.x - 1, cell.y);
- var rightCell = graph.getCellAt(cell.x + 1, cell.y);
- var bottomCell = graph.getCellAt(cell.x, cell.y + 1);
- if(!edgeAlreadyDrawn(cell, topCell) && graph.areConnected(cell, topCell)) {
- var x1 = cell.x * self.cellWidth;
- var y1 = cell.y * self.cellHeight;
- var x2 = x1 + self.cellWidth;
- var y2 = y1;
- this.drawLine(x1, y1, x2, y2);
- drawnEdges.push([cell, topCell]);
- }
- if(!edgeAlreadyDrawn(cell, leftCell) && graph.areConnected(cell, leftCell)) {
- var x2 = x1;
- var y2 = y1 + self.cellHeight;
- this.drawLine(x1, y1, x2, y2);
- drawnEdges.push([cell, leftCell]);
- }
- if(!edgeAlreadyDrawn(cell, rightCell) && graph.areConnected(cell, rightCell)) {
- var x1 = (cell.x * self.cellWidth) + self.cellWidth;
- var y1 = cell.y * self.cellHeight;
- var x2 = x1;
- var y2 = y1 + self.cellHeight;
- this.drawLine(x1, y1, x2, y2);
- drawnEdges.push([cell, rightCell]);
- }
- if(!edgeAlreadyDrawn(cell, bottomCell) && graph.areConnected(cell, bottomCell)) {
- var x1 = cell.x * self.cellWidth;
- var y1 = (cell.y * self.cellHeight) + self.cellHeight;
- var x2 = x1 + self.cellWidth;
- var y2 = y1;
- this.drawLine(x1, y1, x2, y2);
- drawnEdges.push([cell, bottomCell]);
- }
- }
- }
- },
- drawLine: function(x1, y1, x2, y2) {
- self.ctx.beginPath();
- self.ctx.moveTo(x1, y1);
- self.ctx.lineTo(x2, y2);
- self.ctx.stroke();
- }
- };
- };
|