Skip to content

Commit

Permalink
maze solver
Browse files Browse the repository at this point in the history
  • Loading branch information
Chuwiey committed Jul 14, 2014
1 parent 6c2400c commit d9f0fea
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 5 deletions.
38 changes: 38 additions & 0 deletions api/models/Maze.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Maze.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/#!documentation/models
*/

module.exports = {

attributes: {
hash: {
type: 'string',
required: true
},
start: {
type: 'array',
required: true
},
end: {
type: 'array',
required: true
}

},

beforeCreate: function(values, cb) {
cb();
},

generateMaze: function() {

},

checkSolvable: function(start, end) {

}

};
73 changes: 68 additions & 5 deletions views/homepage.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,9 @@
var camera = new Camera(display, MOBILE ? 160 : 320, 0.8);
var loop = new GameLoop();
var printMaze = function() {
var printMaze = function(grid) {
var maze = '';
for (var i = 0; i < map.wallGrid.length; i++) {
for (var i = 0; i < grid.length; i++) {
if (!(i % map.size)) {
maze += '\n';
}
Expand All @@ -303,19 +303,82 @@
else if (Math.floor(player.y) * map.size + Math.floor(player.x) === i) {
maze += 'X,';
}
else if (grid[i] === 6) {
maze += '+,';
}
else {
maze += map.wallGrid[i] + ',';
maze += grid[i] + ',';
}
}
console.clear();
// console.clear();
console.log(maze);
}
function mazeSolver(maze, size, start, end) {
this.maze = Array.prototype.slice.call(maze, 0);
this.mazeCopy = Array.prototype.slice.call(maze, 0);
this.size = size;
this.start = start;
this.end = end;
this.solved = false;
this.steps = 0;
this.moves = [];
}
mazeSolver.prototype.move = function(curX, curY) {
// debugger;
if (this.solved) { return false; }
if (curY < 0 || curY >= this.size) { return false; }
if (curX < 0 || curX >= this.size) { return false; }
if (this.mazeCopy[curY*this.size + curX] === 6) {
return false;
}
this.markMaze(curX, curY);
this.steps++;
if (this.maze[curY*this.size + curX] === 1) {
return false; // is wall?
}
else if (this.end[0] === curX && this.end[1] === curY) {
this.solved = true;
return true; // at end?
}
else if (this.move(curX-1, curY)) {
this.moves.push('left');
return true; // left
}
else if (this.move(curX, curY-1)) {
this.moves.push('up');
return true; // up
}
else if (this.move(curX+1, curY)) {
this.moves.push('right');
return true; // right
}
else if (this.move(curX, curY+1)) {
this.moves.push('down');
return true; // down
}
}
mazeSolver.prototype.markMaze = function(curX, curY) {
this.mazeCopy[curY*this.size + curX] = 6;
printMaze(this.mazeCopy);
}
x = new mazeSolver(map.wallGrid, map.size, map.st, map.end);
printMaze(map.wallGrid);
x.move(x.start[0],x.start[1]);
printMaze(x.maze);
console.log(x.moves.reverse(), x.steps);
//mazeSolver.prototype.walk = function()
loop.start(function frame(seconds) {
map.update(seconds);
player.update(controls.states, map, seconds);
camera.render(player, map);
printMaze();
//printMaze();
});
</script>
Expand Down

0 comments on commit d9f0fea

Please sign in to comment.