🚀 improve algorithm for enclaves
This commit is contained in:
@@ -3,26 +3,23 @@ import { around4 } from "@snk/types/point";
|
||||
import { sortPush } from "./utils/sortPush";
|
||||
import type { Color, Grid } from "@snk/types/grid";
|
||||
import type { Point } from "@snk/types/point";
|
||||
import {
|
||||
createSnakeFromCells,
|
||||
getHeadX,
|
||||
getHeadY,
|
||||
nextSnake,
|
||||
Snake,
|
||||
snakeEquals,
|
||||
snakeWillSelfCollide,
|
||||
} from "@snk/types/snake";
|
||||
|
||||
type M = Point & { parent: M | null; h: number; w: number };
|
||||
type M = Point & { parent: M | null; h: number };
|
||||
|
||||
const unwrap = (grid: Grid, m: M | null): Point[] =>
|
||||
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 = (
|
||||
grid: Grid,
|
||||
x: number,
|
||||
y: number,
|
||||
color: Color,
|
||||
previousSnake: Point[] = []
|
||||
) => {
|
||||
const openList: M[] = [{ x, y, h: 0, w: 0, parent: null }];
|
||||
const getEscapePath = (grid: Grid, x: number, y: number, color: Color) => {
|
||||
const openList: M[] = [{ x, y, h: 0, parent: null }];
|
||||
const closeList: Point[] = [];
|
||||
|
||||
while (openList.length) {
|
||||
@@ -34,23 +31,20 @@ const getEscapePath = (
|
||||
const x = c.x + a.x;
|
||||
const y = c.y + a.y;
|
||||
|
||||
if (!contains(previousSnake, x, y, c.w)) {
|
||||
if (!isInside(grid, x, y))
|
||||
return unwrap(grid, { x, y, parent: c } as any);
|
||||
if (!isInside(grid, x, y))
|
||||
return unwrap(grid, { x, y, parent: c } as any);
|
||||
|
||||
const u = getColor(grid, x, y);
|
||||
const u = getColor(grid, x, y);
|
||||
|
||||
if (
|
||||
(isEmpty(u) || u <= color) &&
|
||||
!closeList.some((cl) => cl.x === x && cl.y === y)
|
||||
) {
|
||||
const h = Math.abs(grid.height / 2 - y);
|
||||
const o = { x, y, parent: c, h, w: c.w + 1 };
|
||||
if (
|
||||
(isEmpty(u) || u <= color) &&
|
||||
!closeList.some((cl) => cl.x === x && cl.y === y)
|
||||
) {
|
||||
const h = Math.abs(grid.height / 2 - y);
|
||||
const o = { x, y, parent: c, h };
|
||||
|
||||
sortPush(openList, o, (a, b) => a.h - b.h);
|
||||
closeList.push(o);
|
||||
openList.push(o);
|
||||
}
|
||||
sortPush(openList, o, (a, b) => a.h - b.h);
|
||||
closeList.push(o);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -58,6 +52,38 @@ const getEscapePath = (
|
||||
return null;
|
||||
};
|
||||
|
||||
const snakeCanEscape = (grid: Grid, snake: Snake, color: Color) => {
|
||||
const openList: Snake[] = [snake];
|
||||
const closeList: Snake[] = [];
|
||||
|
||||
while (openList.length) {
|
||||
const s = openList.shift()!;
|
||||
|
||||
for (const a of around4) {
|
||||
const x = getHeadX(s) + a.x;
|
||||
const y = getHeadY(s) + a.y;
|
||||
|
||||
if (!isInside(grid, x, y)) return true;
|
||||
|
||||
const u = getColor(grid, x, y);
|
||||
|
||||
if ((isEmpty(u) || u <= color) && !snakeWillSelfCollide(s, a.x, a.y)) {
|
||||
const sn = nextSnake(s, a.x, a.y);
|
||||
|
||||
if (!closeList.some((s0) => snakeEquals(s0, sn))) {
|
||||
openList.push(sn);
|
||||
closeList.push(sn);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* returns true if the cell can be reached by the snake from anywhere, and the snake can go back to anywhere
|
||||
*/
|
||||
const isFree = (
|
||||
grid: Grid,
|
||||
x: number,
|
||||
@@ -65,15 +91,25 @@ const isFree = (
|
||||
color: Color,
|
||||
snakeN: number
|
||||
) => {
|
||||
const one = getEscapePath(grid, x, y, color);
|
||||
// get the first path to escape
|
||||
const firstPath = getEscapePath(grid, x, y, color);
|
||||
|
||||
if (!one) return false;
|
||||
if (!firstPath) return false;
|
||||
|
||||
const two = getEscapePath(grid, x, y, color, one.slice(0, snakeN));
|
||||
// build a snake from the path
|
||||
// /!\ it might be not a valid snake as we stack up the queue if the path is too short
|
||||
const s = firstPath.slice(0, snakeN);
|
||||
while (s.length < snakeN) s.push(s[s.length - 1]);
|
||||
const snake1 = createSnakeFromCells(s);
|
||||
|
||||
return !!two;
|
||||
// check for a second route, considering snake collision
|
||||
return snakeCanEscape(grid, snake1, color);
|
||||
};
|
||||
|
||||
/**
|
||||
* returns free cells
|
||||
* and removes them from the grid
|
||||
*/
|
||||
export const pruneLayer = (grid: Grid, color: Color, snakeN: number) => {
|
||||
const chunk: Point[] = [];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user