🚀 imrpove algorithm

This commit is contained in:
platane
2020-10-31 17:23:19 +01:00
parent d81ecec836
commit b595e7de53
22 changed files with 707 additions and 451 deletions

View File

@@ -1,10 +1,4 @@
import {
copyGrid,
getColor,
isEmpty,
isInside,
setColorEmpty,
} from "@snk/types/grid";
import { copyGrid, getColor, isInside, setColorEmpty } from "@snk/types/grid";
import { around4 } from "@snk/types/point";
import { sortPush } from "./utils/sortPush";
import {
@@ -15,75 +9,57 @@ import {
snakeEquals,
snakeWillSelfCollide,
} from "@snk/types/snake";
import { isOutside } from "./outside";
import { trimTunnelEnd, trimTunnelStart } from "./tunnel";
import type { Outside } from "./outside";
import type { Snake } from "@snk/types/snake";
import type { Color, Grid } from "@snk/types/grid";
import type { Empty, Color, Grid } from "@snk/types/grid";
import type { Point } from "@snk/types/point";
type M = {
snake: Snake;
grid: Grid;
parent: M | null;
w: number;
h: number;
f: number;
const getColorSafe = (grid: Grid, x: number, y: number) =>
isInside(grid, x, y) ? getColor(grid, x, y) : (0 as Empty);
const setEmptySafe = (grid: Grid, x: number, y: number) => {
if (isInside(grid, x, y)) setColorEmpty(grid, x, y);
};
type M = { snake: Snake; parent: M | null; w: number };
const unwrap = (m: M | null): Point[] =>
!m
? []
: [...unwrap(m.parent), { x: getHeadX(m.snake), y: getHeadY(m.snake) }];
/**
* returns the path to reach the outside which contains the least color cell
*/
const getSnakeEscapePath = (grid0: Grid, snake0: Snake, color: Color) => {
const openList: M[] = [
{ snake: snake0, grid: grid0, w: 0, h: 0, f: 0, parent: null },
];
const getSnakeEscapePath = (
grid: Grid,
outside: Outside,
snake0: Snake,
color: Color
) => {
const openList: M[] = [{ snake: snake0, w: 0 } as any];
const closeList: Snake[] = [];
while (openList.length) {
while (openList[0]) {
const o = openList.shift()!;
const x = getHeadX(o.snake);
const y = getHeadY(o.snake);
if (isOutside(outside, x, y)) return unwrap(o);
for (const a of around4) {
if (!snakeWillSelfCollide(o.snake, a.x, a.y)) {
const y = getHeadY(o.snake) + a.y;
const x = getHeadX(o.snake) + a.x;
const c = getColorSafe(grid, x + a.x, y + a.y);
if (!isInside(grid0, x, y)) {
// unwrap and return
const points: Point[] = [];
if (c <= color && !snakeWillSelfCollide(o.snake, a.x, a.y)) {
const snake = nextSnake(o.snake, a.x, a.y);
points.push({ x, y });
let e: M["parent"] = o;
while (e) {
points.unshift({
x: getHeadX(e.snake),
y: getHeadY(e.snake),
});
e = e.parent;
}
return points;
}
const u = getColor(grid0, x, y);
if (isEmpty(u) || u <= color) {
const snake = nextSnake(o.snake, a.x, a.y);
if (!closeList.some((s0) => snakeEquals(s0, snake))) {
let grid = o.grid;
if (!isEmpty(u)) {
grid = copyGrid(grid);
setColorEmpty(grid, x, y);
}
const h = Math.abs(grid.height / 2 - y);
const w = o.w + (u === color ? 1 : 0);
const f = w * 1000 - h;
sortPush(
openList,
{ snake, grid, parent: o, h, w, f },
(a, b) => a.f - b.f
);
closeList.push(snake);
}
if (!closeList.some((s0) => snakeEquals(s0, snake))) {
const w = o.w + 1 + +(c === color) * 1000;
sortPush(openList, { snake, w, parent: o }, (a, b) => a.w - b.w);
closeList.push(snake);
}
}
}
@@ -99,15 +75,16 @@ const getSnakeEscapePath = (grid0: Grid, snake0: Snake, color: Color) => {
*/
export const getBestTunnel = (
grid: Grid,
outside: Outside,
x: number,
y: number,
color: Color,
snakeN: number
) => {
const c = { x, y };
const snake = createSnakeFromCells(Array.from({ length: snakeN }, () => c));
const snake0 = createSnakeFromCells(Array.from({ length: snakeN }, () => c));
const one = getSnakeEscapePath(grid, snake, color);
const one = getSnakeEscapePath(grid, outside, snake0, color);
if (!one) return null;
@@ -119,46 +96,18 @@ export const getBestTunnel = (
// remove from the grid the colors that one eat
const gridI = copyGrid(grid);
for (const { x, y } of one)
if (isInside(grid, x, y)) setColorEmpty(gridI, x, y);
for (const { x, y } of one) setEmptySafe(gridI, x, y);
const two = getSnakeEscapePath(gridI, snakeI, color);
const two = getSnakeEscapePath(gridI, outside, snakeI, color);
if (!two) return null;
one.shift();
one.reverse();
one.push(...two);
trimTunnelStart(grid, one);
trimTunnelEnd(grid, one);
return one;
};
/**
* remove empty cell from start
*/
export const trimTunnelStart = (grid: Grid, tunnel: Point[]) => {
while (tunnel.length) {
const { x, y } = tunnel[0];
if (!isInside(grid, x, y) || isEmpty(getColor(grid, x, y))) tunnel.shift();
else break;
}
};
/**
* remove empty cell from end
*/
export const trimTunnelEnd = (grid: Grid, tunnel: Point[]) => {
while (tunnel.length) {
const i = tunnel.length - 1;
const { x, y } = tunnel[i];
if (
!isInside(grid, x, y) ||
isEmpty(getColor(grid, x, y)) ||
tunnel.findIndex((p) => p.x === x && p.y === y) < i
)
tunnel.pop();
else break;
}
};