-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathfind.js
60 lines (49 loc) · 1.44 KB
/
pathfind.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
angular.module('PathfinderApp', [])
.controller('MazeCtrl', function($scope, $http) {
$scope.painting = false;
$scope.original = {};
$http.get('/maze').success(function(maze) {
$scope.maze = maze;
});
$scope.submit = function() {
$http.post('/maze/solve', $scope.maze)
.success(function(maze) {
$scope.original = _(maze).clone();
_(maze.solutions).each(function(s) {
s.steps = $scope.steps_in_solution(s).length
});
// sort in place
$scope.maze.solutions = _(maze.solutions).sortBy(function(s) {
return s.steps
});
$scope.maze.solution = maze.solutions[0];
});
};
$scope.steps_in_solution = function(solution) {
// lame
var result = _(solution).flatten()
return _(result).where({ 'type': 'path' });
};
$scope.paint = function(cell, e) {
if ($scope.painting) {
cell.type = $scope.brush;
e.preventDefault();
}
};
$scope.startPainting = function(cell, e) {
$scope.painting = true;
$scope.brush = cell.type;
e.preventDefault();
};
$scope.stopPainting = function() {
$scope.painting = false;
$scope.submit($scope.maze);
};
$scope.toggle_wall = function(cell) {
if (cell.type == 'space') {
cell.type = 'wall';
} else if (cell.type == 'wall') {
cell.type = 'space';
}
};
});