🚀 refactor
This commit is contained in:
@@ -56,3 +56,28 @@ export const getAvailableRoutes = (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const getInterestingAvailableRoutes = (
|
||||||
|
grid: Grid,
|
||||||
|
snake0: Snake,
|
||||||
|
onSolution: (snakes: Snake[], color: Color) => boolean,
|
||||||
|
n = snake0.length
|
||||||
|
) => {
|
||||||
|
const solutions: Snake[] = [];
|
||||||
|
|
||||||
|
getAvailableRoutes(grid, snake0, (snakes, color) => {
|
||||||
|
const [snake] = snakes;
|
||||||
|
|
||||||
|
for (let j = solutions.length; j--; ) {
|
||||||
|
let same = true;
|
||||||
|
for (let i = 0; i < n * 2; i++)
|
||||||
|
same = same && solutions[j][i] === snake[i];
|
||||||
|
|
||||||
|
if (same) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
solutions.push(snake);
|
||||||
|
|
||||||
|
return onSolution(snakes, color);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { createCanvas } from "./canvas";
|
import { createCanvas } from "./canvas";
|
||||||
import { samples } from "./samples";
|
import { samples } from "./samples";
|
||||||
import { getAvailableRoutes } from "@snk/compute/getAvailableRoutes";
|
import { getInterestingAvailableRoutes } from "@snk/compute/getAvailableRoutes";
|
||||||
import { createSnake, Snake, snakeToCells } from "@snk/compute/snake";
|
import { createSnake, Snake, snakeToCells } from "@snk/compute/snake";
|
||||||
import { GUI } from "dat.gui";
|
import { GUI } from "dat.gui";
|
||||||
import { Point } from "@snk/compute/point";
|
import { Point } from "@snk/compute/point";
|
||||||
@@ -23,10 +23,15 @@ const snake0 = createSnake([
|
|||||||
{ x: -1, y: 3 },
|
{ x: -1, y: 3 },
|
||||||
]);
|
]);
|
||||||
const routes: Snake[][] = [];
|
const routes: Snake[][] = [];
|
||||||
getAvailableRoutes(grid0, snake0, (snakes) => {
|
getInterestingAvailableRoutes(
|
||||||
routes.push(snakes);
|
grid0,
|
||||||
return routes.length > 10;
|
snake0,
|
||||||
});
|
(snakes) => {
|
||||||
|
routes.push(snakes);
|
||||||
|
return routes.length > 10;
|
||||||
|
},
|
||||||
|
2
|
||||||
|
);
|
||||||
|
|
||||||
const config = { routeN: 0, routeK: 0 };
|
const config = { routeN: 0, routeK: 0 };
|
||||||
|
|
||||||
|
|||||||
32
packages/draw/drawSnake.ts
Normal file
32
packages/draw/drawSnake.ts
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import { pathRoundedRect } from "./pathRoundedRect";
|
||||||
|
import { Snake, snakeToCells } from "@snk/compute/snake";
|
||||||
|
|
||||||
|
type Options = {
|
||||||
|
colorSnake: string;
|
||||||
|
sizeCell: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const drawSnake = (
|
||||||
|
ctx: CanvasRenderingContext2D,
|
||||||
|
snake: Snake,
|
||||||
|
o: Options
|
||||||
|
) => {
|
||||||
|
const cells = snakeToCells(snake);
|
||||||
|
|
||||||
|
for (let i = 0; i < cells.length; i++) {
|
||||||
|
const u = (i + 1) * 0.6;
|
||||||
|
|
||||||
|
ctx.save();
|
||||||
|
ctx.fillStyle = o.colorSnake;
|
||||||
|
ctx.translate(cells[i].x * o.sizeCell + u, cells[i].y * o.sizeCell + u);
|
||||||
|
ctx.beginPath();
|
||||||
|
pathRoundedRect(
|
||||||
|
ctx,
|
||||||
|
o.sizeCell - u * 2,
|
||||||
|
o.sizeCell - u * 2,
|
||||||
|
(o.sizeCell - u * 2) * 0.25
|
||||||
|
);
|
||||||
|
ctx.fill();
|
||||||
|
ctx.restore();
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { Grid, Color } from "@snk/compute/grid";
|
import { Grid, Color } from "@snk/compute/grid";
|
||||||
import { pathRoundedRect } from "./pathRoundedRect";
|
|
||||||
import { drawGrid } from "./drawGrid";
|
import { drawGrid } from "./drawGrid";
|
||||||
import { Snake, snakeToCells } from "@snk/compute/snake";
|
import { Snake } from "@snk/compute/snake";
|
||||||
|
import { drawSnake } from "./drawSnake";
|
||||||
|
|
||||||
type Options = {
|
type Options = {
|
||||||
colorDots: Record<Color, string>;
|
colorDots: Record<Color, string>;
|
||||||
@@ -13,31 +13,6 @@ type Options = {
|
|||||||
sizeBorderRadius: number;
|
sizeBorderRadius: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const drawSnake = (
|
|
||||||
ctx: CanvasRenderingContext2D,
|
|
||||||
snake: Snake,
|
|
||||||
o: Options
|
|
||||||
) => {
|
|
||||||
const cells = snakeToCells(snake);
|
|
||||||
|
|
||||||
for (let i = 0; i < cells.length; i++) {
|
|
||||||
const u = (i + 1) * 0.6;
|
|
||||||
|
|
||||||
ctx.save();
|
|
||||||
ctx.fillStyle = o.colorSnake;
|
|
||||||
ctx.translate(cells[i].x * o.sizeCell + u, cells[i].y * o.sizeCell + u);
|
|
||||||
ctx.beginPath();
|
|
||||||
pathRoundedRect(
|
|
||||||
ctx,
|
|
||||||
o.sizeCell - u * 2,
|
|
||||||
o.sizeCell - u * 2,
|
|
||||||
(o.sizeCell - u * 2) * 0.25
|
|
||||||
);
|
|
||||||
ctx.fill();
|
|
||||||
ctx.restore();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const drawWorld = (
|
export const drawWorld = (
|
||||||
ctx: CanvasRenderingContext2D,
|
ctx: CanvasRenderingContext2D,
|
||||||
grid: Grid,
|
grid: Grid,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"lib": ["dom", "ES2020"],
|
"lib": ["dom", "ES2020"],
|
||||||
|
"target": "ES2020",
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"noUnusedLocals": true,
|
"noUnusedLocals": true,
|
||||||
|
|||||||
Reference in New Issue
Block a user