🚀 refactor

This commit is contained in:
platane
2020-09-29 11:00:29 +02:00
committed by Platane
parent 8d8956229c
commit 9b92697ef9
5 changed files with 70 additions and 32 deletions

View File

@@ -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);
});
};

View File

@@ -1,6 +1,6 @@
import { createCanvas } from "./canvas";
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 { GUI } from "dat.gui";
import { Point } from "@snk/compute/point";
@@ -23,10 +23,15 @@ const snake0 = createSnake([
{ x: -1, y: 3 },
]);
const routes: Snake[][] = [];
getAvailableRoutes(grid0, snake0, (snakes) => {
routes.push(snakes);
return routes.length > 10;
});
getInterestingAvailableRoutes(
grid0,
snake0,
(snakes) => {
routes.push(snakes);
return routes.length > 10;
},
2
);
const config = { routeN: 0, routeK: 0 };

View 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();
}
};

View File

@@ -1,7 +1,7 @@
import { Grid, Color } from "@snk/compute/grid";
import { pathRoundedRect } from "./pathRoundedRect";
import { drawGrid } from "./drawGrid";
import { Snake, snakeToCells } from "@snk/compute/snake";
import { Snake } from "@snk/compute/snake";
import { drawSnake } from "./drawSnake";
type Options = {
colorDots: Record<Color, string>;
@@ -13,31 +13,6 @@ type Options = {
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 = (
ctx: CanvasRenderingContext2D,
grid: Grid,

View File

@@ -1,6 +1,7 @@
{
"compilerOptions": {
"lib": ["dom", "ES2020"],
"target": "ES2020",
"strict": true,
"skipLibCheck": true,
"noUnusedLocals": true,