WE.mount('pathfinding', function (S) {
var C = S.C, ctx = S.ctx, cols = 0, rws = 0, cs = 20, wall, st, gl, order = [], path = [], shown = 0, drag = null, paint = 1, ox = 0, oy = 0;
var algo = S.select('Method', [['bfs', 'Search everywhere'], ['astar', 'Aim for the goal']], 'bfs', solve);
S.button('Random walls', function () { for (var i = 0; i < wall.length; i++) wall[i] = Math.random() < .28 && i !== st && i !== gl ? 1 : 0; solve(); });
S.button('Clear walls', function () { wall.fill(0); solve(); }); S.button('Search again', function () { shown = 0; });
function init() { var w = S.W, h = S.H; cs = S.small() ? Math.floor(w / 18) : Math.floor(w / 44); var c = Math.floor(w / cs), r = Math.floor(h / cs); if (c === cols && r === rws) return; cols = c; rws = r; ox = (w - c * cs) / 2; oy = (h - r * cs) / 2; wall = new Uint8Array(c * r); var my = Math.floor(r / 2); st = my * c + Math.floor(c * .15); gl = my * c + Math.floor(c * .85); var wx = Math.floor(c / 2); for (var y = 2; y < r - 2; y++) wall[y * c + wx] = 1; solve(); }
function solve() {
order = []; path = []; shown = 0; var n = cols * rws, prev = new Int32Array(n).fill(-1), g = new Float32Array(n).fill(1e9), seen = new Uint8Array(n), open = [st], gx = gl % cols, gy = Math.floor(gl / cols), A = algo.v === 'astar';
function hh(i) { return Math.abs(i % cols - gx) + Math.abs(Math.floor(i / cols) - gy); }
g[st] = 0; seen[st] = 1; var head = 0, found = false;
while (A ? open.length : head < open.length) {
var cur; if (A) { var bi = 0, bf = 1e9, bh = 1e9; for (var k = 0; k < open.length; k++) { var f = g[open[k]] + hh(open[k]), q = hh(open[k]); if (f < bf || (f === bf && q < bh)) { bf = f; bh = q; bi = k; } } cur = open.splice(bi, 1)[0]; if (seen[cur] === 2) continue; seen[cur] = 2; } else cur = open[head++];
order.push(cur); if (cur === gl) { found = true; break; }
var x = cur % cols, y = Math.floor(cur / cols), nb = []; if (x > 0) nb.push(cur - 1); if (x < cols - 1) nb.push(cur + 1); if (y > 0) nb.push(cur - cols); if (y < rws - 1) nb.push(cur + cols);
nb.forEach(function (m) { if (wall[m]) return; if (A) { if (g[cur] + 1 < g[m]) { g[m] = g[cur] + 1; prev[m] = cur; open.push(m); } } else if (!seen[m]) { seen[m] = 1; prev[m] = cur; open.push(m); } });
}
if (found) for (var p = gl; p !== -1; p = prev[p]) path.unshift(p);
}
function at(x, y) { var c = Math.floor((x - ox) / cs), r = Math.floor((y - oy) / cs); return c >= 0 && c < cols && r >= 0 && r < rws ? r * cols + c : -1; }
S.pointer({ cursor: 'crosshair', down: function (x, y) { var i = at(x, y); if (i < 0) return; if (i === st) drag = 's'; else if (i === gl) drag = 'g'; else { drag = 'w'; paint = wall[i] ? 0 : 1; wall[i] = paint; solve(); } },
move: function (x, y, d) { if (!d || !drag) return; var i = at(x, y); if (i < 0) return; if (drag === 'w') { if (i !== st && i !== gl && wall[i] !== paint) { wall[i] = paint; solve(); } } else if (!wall[i] && i !== st && i !== gl) { if (drag === 's') st = i; else gl = i; solve(); } }, up: function () { drag = null; } });
S.onresize = function () { cols = 0; init(); }; init();
S.loop(function (dt) {
if (!cols) { init(); return; } if (!S.ptr.down) shown = Math.min(order.length + path.length, shown + dt * Math.max(60, order.length / 2.2)); else shown = order.length + path.length;
S.clear(); var i, n = Math.min(order.length, Math.floor(shown));
for (i = 0; i < wall.length; i++) { var x = ox + (i % cols) * cs, y = oy + Math.floor(i / cols) * cs; ctx.fillStyle = wall[i] ? '#59606d' : '#141820'; ctx.fillRect(x + .5, y + .5, cs - 1, cs - 1); }
for (i = 0; i < n; i++) { var c = order[i]; ctx.fillStyle = i > n - 14 && n < order.length ? C.blue : 'rgba(111,177,230,.28)'; ctx.fillRect(ox + (c % cols) * cs + .5, oy + Math.floor(c / cols) * cs + .5, cs - 1, cs - 1); }
var pn = Math.max(0, Math.floor(shown) - order.length); if (pn > 0 && path.length) { ctx.beginPath(); for (i = 0; i < Math.min(pn + 1, path.length); i++) ctx.lineTo(ox + (path[i] % cols + .5) * cs, oy + (Math.floor(path[i] / cols) + .5) * cs); ctx.strokeStyle = C.acc; ctx.lineWidth = Math.max(3, cs * .3); ctx.lineJoin = 'round'; ctx.lineCap = 'round'; ctx.stroke(); }
[[st, C.green, 'A'], [gl, C.red, 'B']].forEach(function (q) { var x = ox + (q[0] % cols + .5) * cs, y = oy + (Math.floor(q[0] / cols) + .5) * cs; S.dot(x, y, cs * .46, q[1]); S.txt(q[2], x, y + 1, '#111', Math.max(10, cs * .5), 'center', 700); });
S.rr(8, 8, 250, 28, 14, 'rgba(13,15,19,.85)'); S.txt(n + ' squares checked' + (shown >= order.length ? (path.length ? ' · route: ' + (path.length - 1) + ' steps' : ' · no way through') : ''), 20, 22, C.ink, 12.5, 'left', 600);
S.say((algo.v === 'bfs' ? 'Search everywhere spreads out evenly from A, one step at a time, like water. It always finds the shortest route, but it checks a lot of squares.' : 'Aim for the goal checks the squares that look closest to B first. It finds a route just as short, usually after checking far fewer squares. Map apps work this way.') + ' Draw walls on the grid, or drag A and B.');
});
});