:roclet: improve algorithm, add an intermediate phase to clean up residual cell from previous layer, before grabing the free ones

This commit is contained in:
platane
2020-10-26 00:18:50 +01:00
parent 69c3551cc5
commit a3f590a7d2
13 changed files with 565 additions and 21 deletions

View File

@@ -0,0 +1,54 @@
import "./menu";
import { createCanvas } from "./canvas";
import { getSnakeLength } from "@snk/types/snake";
import { grid, snake } from "./sample";
import { getColor } from "@snk/types/grid";
import { getBestTunnel } from "@snk/compute/getBestTunnel";
import type { Color } from "@snk/types/grid";
import type { Point } from "@snk/types/point";
const { canvas, ctx, draw, highlightCell } = createCanvas(grid);
document.body.appendChild(canvas);
const ones: Point[] = [];
for (let x = 0; x < grid.width; x++)
for (let y = 0; y < grid.height; y++)
if (getColor(grid, x, y) === 1) ones.push({ x, y });
const points = getBestTunnel(
grid,
ones[0].x,
ones[0].y,
3 as Color,
getSnakeLength(snake)
);
let k = 0;
const onChange = () => {
ctx.clearRect(0, 0, 9999, 9999);
draw(grid, [] as any, []);
if (points) {
points.forEach(({ x, y }) => highlightCell(x, y));
highlightCell(points[k].x, points[k].y, "blue");
}
};
onChange();
const inputK = document.createElement("input") as any;
inputK.type = "range";
inputK.value = 0;
inputK.step = 1;
inputK.min = 0;
inputK.max = points ? points.length - 1 : 0;
inputK.style.width = "90%";
inputK.style.padding = "20px 0";
inputK.addEventListener("input", () => {
k = +inputK.value;
onChange();
});
document.body.append(inputK);

View File

@@ -0,0 +1,75 @@
import "./menu";
import { createCanvas } from "./canvas";
import { snakeToCells } from "@snk/types/snake";
import { grid, snake } from "./sample";
import { getColor } from "@snk/types/grid";
import { getPathTo } from "@snk/compute/getPathTo";
import type { Point } from "@snk/types/point";
const { canvas, ctx, draw, highlightCell } = createCanvas(grid);
document.body.appendChild(canvas);
const ones: Point[] = [];
for (let x = 0; x < grid.width; x++)
for (let y = 0; y < grid.height; y++)
if (getColor(grid, x, y) === 1) ones.push({ x, y });
const chains = ones.map((p) => {
const chain = getPathTo(grid, snake, p.x, p.y);
return chain && [...chain, snake];
});
let k = 0;
let i = 0;
const onChange = () => {
ctx.clearRect(0, 0, 9999, 9999);
const chain = chains[k];
if (chain) {
draw(grid, chain[i], []);
chain
.map(snakeToCells)
.flat()
.forEach(({ x, y }) => highlightCell(x, y));
} else draw(grid, snake, []);
};
onChange();
const inputK = document.createElement("input") as any;
inputK.type = "range";
inputK.value = k;
inputK.step = 1;
inputK.min = 0;
inputK.max = chains.length - 1;
inputK.style.width = "90%";
inputK.style.padding = "20px 0";
inputK.addEventListener("input", () => {
k = +inputK.value;
i = inputI.value = 0;
inputI.max = chains[k] ? chains[k]!.length - 1 : 0;
onChange();
});
document.body.append(inputK);
const inputI = document.createElement("input") as any;
inputI.type = "range";
inputI.value = 0;
inputI.max = chains[k] ? chains[k]!.length - 1 : 0;
inputI.step = 1;
inputI.min = 0;
inputI.style.width = "90%";
inputI.style.padding = "20px 0";
inputI.addEventListener("input", () => {
i = +inputI.value;
onChange();
});
document.body.append(inputI);
window.addEventListener("click", (e) => {
if (e.target === document.body || e.target === document.body.parentElement)
inputK.focus();
});

View File

@@ -1 +1,8 @@
["getAvailableRoutes", "pruneLayer", "getBestRoute", "interactive"]
[
"getAvailableRoutes",
"pruneLayer",
"getBestRoute",
"getBestRoundTrip",
"getPathTo",
"interactive"
]

View File

@@ -3,10 +3,11 @@ import { createCanvas } from "./canvas";
import { Color, copyGrid } from "@snk/types/grid";
import { grid, snake } from "./sample";
import { pruneLayer } from "@snk/compute/pruneLayer";
import { getSnakeLength } from "@snk/types/snake";
const colors = [1, 2, 3] as Color[];
const snakeN = snake.length / 2;
const snakeN = getSnakeLength(snake);
const layers = [{ grid, chunk: [] as { x: number; y: number }[] }];
let grid0 = copyGrid(grid);