-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathalgorithm.js
More file actions
151 lines (131 loc) · 3.91 KB
/
algorithm.js
File metadata and controls
151 lines (131 loc) · 3.91 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'use strict';
//////////////////////////////////////////////////////////////////////
// Solving the board.
//
// The board is solved using a straight-forward, brute-force, backtracking
// algorithm. The algorithm tries every possible combination and stops when one
// solution is found.
//
// It is possible to adapt this code to find all solutions. However, care must
// be taken to remove duplicate solutions, because the algorithm will find the
// same solution multiple times (e.g. will find the A->B solution as well as
// the A<-B one, but both are actually the same solution).
function _solve_board_recursive(board, x, y, color) {
if (x === null && y === null) {
if (board.starting_points.length === 0) {
if (board.incomplete_nodes === 0) {
// Found a solution!
return true;
} else if (board.incomplete_nodes < 0) {
console.error('board.incomplete_nodes: ' + board.incomplete_nodes);
return false;
} else {
return false;
}
} else {
// Start a new path.
var p = board.starting_points.pop();
var found = _solve_board_recursive(board, p.x, p.y, board.nodes[p.y][p.x].color);
if (found) return found;
board.starting_points.push(p);
return false;
}
} else {
// Continue an existing path.
var src_node = board.nodes[y][x];
if (src_node.missing_edges === 0) {
// End of a path.
return _solve_board_recursive(board, null, null, null);
} else {
// Middle of a path.
// for (var dir of DIRECTIONS) {
for (var dir_index = 0; dir_index < DIRECTIONS.length; dir_index++) {
var dir = DIRECTIONS[dir_index];
var dx = x + dir.dx;
var dy = y + dir.dy;
// Out-of-bounds.
if (dx < 0 || dy < 0 || dy >= board.nodes.length || dx >= board.nodes[dy].length) {
continue;
}
// Edge blocking.
var restriction = dir.get_restriction_name(x, y);
if (board.edge_restrictions[restriction]) {
continue;
}
// Adjacent node.
var node = board.nodes[dy][dx];
// Invalid node (empty space).
if (!node) {
continue;
}
// Already completed node.
if (node.missing_edges <= 0) {
continue;
}
// Incompatible color.
if (node.color !== null && node.color !== color) {
continue;
}
// Valid node, proceed building the solution.
src_node.edges.push(new Edge(dir, color));
src_node.missing_edges--;
if (src_node.missing_edges === 0) {
board.incomplete_nodes--;
}
node.missing_edges--;
if (node.missing_edges === 0) {
board.incomplete_nodes--;
}
board.edge_count++;
board.edge_restrictions[restriction] = true;
// Recursive call.
var found = _solve_board_recursive(board, dx, dy, color);
if (found) return found;
// Undoing (back-tracking) this step.
delete board.edge_restrictions[restriction];
board.edge_count--;
if (node.missing_edges === 0) {
board.incomplete_nodes++;
}
node.missing_edges++;
if (src_node.missing_edges === 0) {
board.incomplete_nodes++;
}
src_node.missing_edges++;
src_node.edges.pop();
} // for loop for DIRECTIONS
return false;
}
}
}
function clear_solution(board) {
board.incomplete_nodes = 0;
for (var i = 0; i < board.nodes.length; i++) {
for (var j = 0; j < board.nodes[i].length; j++) {
var node = board.nodes[i][j];
if (node) {
node.edges = [];
node.missing_edges = node.max_edges;
if (node.missing_edges > 0) {
board.incomplete_nodes++;
}
}
}
}
board.edge_count = 0;
board.edge_restrictions = {};
}
function solve_board(board) {
clear_solution(board);
board.starting_points = board.terminators.slice(); // A copy of the array.
var found = _solve_board_recursive(board, null, null, null);
board.solution_found = found;
}
//////////////////////////////////////////////////////////////////////
// Web Worker initialization.
importScripts('shared.js');
onmessage = function(e) {
var board = e.data;
solve_board(board);
postMessage(board);
}