🚀 improve algorithm for enclaved cell
This commit is contained in:
@@ -36,7 +36,7 @@ export const getAvailableRoutes = (
|
|||||||
const ny = cy + dy;
|
const ny = cy + dy;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
isInsideLarge(grid, 1, nx, ny) &&
|
isInsideLarge(grid, 2, nx, ny) &&
|
||||||
!snakeWillSelfCollide(snake, dx, dy)
|
!snakeWillSelfCollide(snake, dx, dy)
|
||||||
) {
|
) {
|
||||||
const nsnake = nextSnake(snake, dx, dy);
|
const nsnake = nextSnake(snake, dx, dy);
|
||||||
|
|||||||
@@ -4,19 +4,25 @@ import { sortPush } from "./utils/sortPush";
|
|||||||
import type { Color, Grid } from "@snk/types/grid";
|
import type { Color, Grid } from "@snk/types/grid";
|
||||||
import type { Point } from "@snk/types/point";
|
import type { Point } from "@snk/types/point";
|
||||||
|
|
||||||
type M = Point & { parent: M | null; h: number };
|
type M = Point & { parent: M | null; h: number; w: number };
|
||||||
|
|
||||||
const unwrap = (grid: Grid, m: M | null): Point[] =>
|
const unwrap = (grid: Grid, m: M | null): Point[] =>
|
||||||
m ? [...unwrap(grid, m.parent), m] : [];
|
m ? [...unwrap(grid, m.parent), m] : [];
|
||||||
|
|
||||||
|
const contains = (arr: Point[], x: number, y: number, limit: number) => {
|
||||||
|
for (let i = Math.max(0, arr.length - limit); i--; )
|
||||||
|
if (arr[i].x === x && arr[i].y === y) return true;
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
const getEscapePath = (
|
const getEscapePath = (
|
||||||
grid: Grid,
|
grid: Grid,
|
||||||
x: number,
|
x: number,
|
||||||
y: number,
|
y: number,
|
||||||
color: Color,
|
color: Color,
|
||||||
forbidden: Point[] = []
|
previousSnake: Point[] = []
|
||||||
) => {
|
) => {
|
||||||
const openList: M[] = [{ x, y, h: 0, parent: null }];
|
const openList: M[] = [{ x, y, h: 0, w: 0, parent: null }];
|
||||||
const closeList: Point[] = [];
|
const closeList: Point[] = [];
|
||||||
|
|
||||||
while (openList.length) {
|
while (openList.length) {
|
||||||
@@ -28,7 +34,7 @@ const getEscapePath = (
|
|||||||
const x = c.x + a.x;
|
const x = c.x + a.x;
|
||||||
const y = c.y + a.y;
|
const y = c.y + a.y;
|
||||||
|
|
||||||
if (!forbidden.some((cl) => cl.x === x && cl.y === y)) {
|
if (!contains(previousSnake, x, y, c.w)) {
|
||||||
if (!isInside(grid, x, y))
|
if (!isInside(grid, x, y))
|
||||||
return unwrap(grid, { x, y, parent: c } as any);
|
return unwrap(grid, { x, y, parent: c } as any);
|
||||||
|
|
||||||
@@ -39,7 +45,7 @@ const getEscapePath = (
|
|||||||
!closeList.some((cl) => cl.x === x && cl.y === y)
|
!closeList.some((cl) => cl.x === x && cl.y === y)
|
||||||
) {
|
) {
|
||||||
const h = Math.abs(grid.height / 2 - y);
|
const h = Math.abs(grid.height / 2 - y);
|
||||||
const o = { x, y, parent: c, h };
|
const o = { x, y, parent: c, h, w: c.w + 1 };
|
||||||
|
|
||||||
sortPush(openList, o, (a, b) => a.h - b.h);
|
sortPush(openList, o, (a, b) => a.h - b.h);
|
||||||
closeList.push(o);
|
closeList.push(o);
|
||||||
|
|||||||
@@ -1,62 +1,85 @@
|
|||||||
import { createCanvas } from "./canvas";
|
import { createCanvas } from "./canvas";
|
||||||
import { snakeToCells } from "@snk/types/snake";
|
import { getHeadX, getHeadY, snakeToCells } from "@snk/types/snake";
|
||||||
import { GUI } from "dat.gui";
|
|
||||||
import { grid, snake } from "./sample";
|
import { grid, snake } from "./sample";
|
||||||
|
import { getColor } from "@snk/types/grid";
|
||||||
import { getAvailableRoutes } from "@snk/compute/getAvailableRoutes";
|
import { getAvailableRoutes } from "@snk/compute/getAvailableRoutes";
|
||||||
import type { Point } from "@snk/types/point";
|
|
||||||
import type { Snake } from "@snk/types/snake";
|
import type { Snake } from "@snk/types/snake";
|
||||||
|
import type { Color, Empty } from "@snk/types/grid";
|
||||||
|
|
||||||
//
|
//
|
||||||
// compute
|
// compute
|
||||||
|
|
||||||
const routes: Snake[][] = [];
|
const solutions: {
|
||||||
|
x: number;
|
||||||
|
y: number;
|
||||||
|
chain: Snake[];
|
||||||
|
color: Color | Empty;
|
||||||
|
}[] = [];
|
||||||
getAvailableRoutes(grid, snake, (chain) => {
|
getAvailableRoutes(grid, snake, (chain) => {
|
||||||
routes.push(chain);
|
const x = getHeadX(chain[0]);
|
||||||
return routes.length > 10;
|
const y = getHeadY(chain[0]);
|
||||||
|
|
||||||
|
if (!solutions.some((s) => x === s.x && y === s.y))
|
||||||
|
solutions.push({
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
chain: [snake, ...chain.slice().reverse()],
|
||||||
|
color: getColor(grid, x, y),
|
||||||
|
});
|
||||||
|
|
||||||
|
return false;
|
||||||
});
|
});
|
||||||
|
solutions.sort((a, b) => a.color - b.color);
|
||||||
const config = { routeN: 0, routeK: 0 };
|
|
||||||
|
|
||||||
//
|
|
||||||
// draw
|
|
||||||
|
|
||||||
const { canvas, ctx, draw } = createCanvas(grid);
|
const { canvas, ctx, draw } = createCanvas(grid);
|
||||||
document.body.appendChild(canvas);
|
document.body.appendChild(canvas);
|
||||||
|
|
||||||
draw(grid, snake, []);
|
let k = 0;
|
||||||
|
let i = solutions[k].chain.length - 1;
|
||||||
let cancel: number;
|
|
||||||
|
|
||||||
const mod = (x: number, m: number) => ((x % m) + m) % m;
|
|
||||||
|
|
||||||
const onChange = () => {
|
const onChange = () => {
|
||||||
const t = Math.floor(Date.now() / 300);
|
const { chain } = solutions[k];
|
||||||
|
|
||||||
cancelAnimationFrame(cancel);
|
ctx.clearRect(0, 0, 9999, 9999);
|
||||||
cancel = requestAnimationFrame(onChange);
|
|
||||||
|
|
||||||
const chain = routes[config.routeN] || [snake];
|
draw(grid, chain[i], []);
|
||||||
|
|
||||||
draw(grid, chain[mod(-t, chain.length)], []);
|
|
||||||
|
|
||||||
const cells: Point[] = [];
|
|
||||||
chain.forEach((s) => cells.push(...snakeToCells(s)));
|
|
||||||
|
|
||||||
ctx.fillStyle = "orange";
|
ctx.fillStyle = "orange";
|
||||||
ctx.fillRect(0, 0, 1, 1);
|
chain
|
||||||
|
.map(snakeToCells)
|
||||||
cells
|
.flat()
|
||||||
.filter((x, i, arr) => i === arr.indexOf(x))
|
.forEach(({ x, y }) => {
|
||||||
.forEach((c) => {
|
|
||||||
ctx.beginPath();
|
ctx.beginPath();
|
||||||
ctx.fillRect((1 + c.x + 0.5) * 16 - 2, (2 + c.y + 0.5) * 16 - 2, 4, 4);
|
ctx.fillRect((1 + x + 0.5) * 16 - 2, (2 + y + 0.5) * 16 - 2, 4, 4);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
|
||||||
// ui
|
|
||||||
|
|
||||||
const gui = new GUI();
|
|
||||||
gui.add(config, "routeN", 0, routes.length - 1, 1).onChange(onChange);
|
|
||||||
|
|
||||||
onChange();
|
onChange();
|
||||||
|
|
||||||
|
const inputK = document.createElement("input") as any;
|
||||||
|
inputK.type = "range";
|
||||||
|
inputK.value = 0;
|
||||||
|
inputK.step = 1;
|
||||||
|
inputK.min = 0;
|
||||||
|
inputK.max = solutions.length - 1;
|
||||||
|
inputK.style.width = "90%";
|
||||||
|
inputK.style.padding = "20px 0";
|
||||||
|
inputK.addEventListener("input", () => {
|
||||||
|
k = +inputK.value;
|
||||||
|
i = inputI.value = inputI.max = solutions[k].chain.length - 1;
|
||||||
|
onChange();
|
||||||
|
});
|
||||||
|
document.body.append(inputK);
|
||||||
|
|
||||||
|
const inputI = document.createElement("input") as any;
|
||||||
|
inputI.type = "range";
|
||||||
|
inputI.value = inputI.max = solutions[k].chain.length - 1;
|
||||||
|
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);
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ setColor(corner, 4, 0, 1 as Color);
|
|||||||
setColor(corner, 4, 4, 1 as Color);
|
setColor(corner, 4, 4, 1 as Color);
|
||||||
setColor(corner, 0, 0, 1 as Color);
|
setColor(corner, 0, 0, 1 as Color);
|
||||||
|
|
||||||
// enclaved color
|
|
||||||
export const enclave = createEmptyGrid(7, 7);
|
export const enclave = createEmptyGrid(7, 7);
|
||||||
setColor(enclave, 3, 4, 2 as Color);
|
setColor(enclave, 3, 4, 2 as Color);
|
||||||
setColor(enclave, 2, 3, 2 as Color);
|
setColor(enclave, 2, 3, 2 as Color);
|
||||||
@@ -29,13 +28,42 @@ setColor(enclave, 4, 3, 2 as Color);
|
|||||||
setColor(enclave, 3, 3, 1 as Color);
|
setColor(enclave, 3, 3, 1 as Color);
|
||||||
setColor(enclave, 5, 5, 1 as Color);
|
setColor(enclave, 5, 5, 1 as Color);
|
||||||
|
|
||||||
// enclaved color
|
|
||||||
export const enclaveBorder = createEmptyGrid(7, 7);
|
export const enclaveBorder = createEmptyGrid(7, 7);
|
||||||
setColor(enclaveBorder, 1, 0, 3 as Color);
|
setColor(enclaveBorder, 1, 0, 3 as Color);
|
||||||
setColor(enclaveBorder, 2, 1, 3 as Color);
|
setColor(enclaveBorder, 2, 1, 3 as Color);
|
||||||
setColor(enclaveBorder, 3, 0, 3 as Color);
|
setColor(enclaveBorder, 3, 0, 3 as Color);
|
||||||
setColor(enclaveBorder, 2, 0, 1 as Color);
|
setColor(enclaveBorder, 2, 0, 1 as Color);
|
||||||
|
|
||||||
|
export const enclaveM = createEmptyGrid(7, 7);
|
||||||
|
setColor(enclaveM, 1, 0, 3 as Color);
|
||||||
|
setColor(enclaveM, 2, 0, 3 as Color);
|
||||||
|
setColor(enclaveM, 3, 0, 3 as Color);
|
||||||
|
setColor(enclaveM, 1, 4, 3 as Color);
|
||||||
|
setColor(enclaveM, 3, 4, 3 as Color);
|
||||||
|
setColor(enclaveM, 4, 1, 3 as Color);
|
||||||
|
setColor(enclaveM, 4, 2, 3 as Color);
|
||||||
|
setColor(enclaveM, 4, 3, 3 as Color);
|
||||||
|
setColor(enclaveM, 0, 1, 3 as Color);
|
||||||
|
setColor(enclaveM, 0, 2, 3 as Color);
|
||||||
|
setColor(enclaveM, 0, 3, 3 as Color);
|
||||||
|
setColor(enclaveM, 2, 2, 1 as Color);
|
||||||
|
|
||||||
|
export const enclaveK = createEmptyGrid(7, 7);
|
||||||
|
setColor(enclaveK, 1, 1, 3 as Color);
|
||||||
|
setColor(enclaveK, 2, 1, 3 as Color);
|
||||||
|
setColor(enclaveK, 3, 1, 3 as Color);
|
||||||
|
setColor(enclaveK, 0, 1, 3 as Color);
|
||||||
|
setColor(enclaveK, 0, 2, 3 as Color);
|
||||||
|
setColor(enclaveK, 0, 3, 3 as Color);
|
||||||
|
setColor(enclaveK, 3, 1, 3 as Color);
|
||||||
|
setColor(enclaveK, 3, 2, 3 as Color);
|
||||||
|
setColor(enclaveK, 3, 3, 3 as Color);
|
||||||
|
setColor(enclaveK, 1, 4, 3 as Color);
|
||||||
|
setColor(enclaveK, 3, 4, 3 as Color);
|
||||||
|
setColor(enclaveK, 3, 5, 3 as Color);
|
||||||
|
setColor(enclaveK, 1, 5, 3 as Color);
|
||||||
|
setColor(enclaveK, 2, 2, 1 as Color);
|
||||||
|
|
||||||
const create = (width: number, height: number, emptyP: number) => {
|
const create = (width: number, height: number, emptyP: number) => {
|
||||||
const grid = createEmptyGrid(width, height);
|
const grid = createEmptyGrid(width, height);
|
||||||
const random = new ParkMiller(10);
|
const random = new ParkMiller(10);
|
||||||
|
|||||||
@@ -5,5 +5,6 @@ const create = (length: number) =>
|
|||||||
|
|
||||||
export const snake1 = create(1);
|
export const snake1 = create(1);
|
||||||
export const snake3 = create(3);
|
export const snake3 = create(3);
|
||||||
|
export const snake4 = create(4);
|
||||||
export const snake5 = create(5);
|
export const snake5 = create(5);
|
||||||
export const snake9 = create(9);
|
export const snake9 = create(9);
|
||||||
|
|||||||
Reference in New Issue
Block a user