🚀 draw world
This commit is contained in:
26
packages/compute/__tests__/grid.spec.ts
Normal file
26
packages/compute/__tests__/grid.spec.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { generateEmptyGrid } from "../generateGrid";
|
||||
import { setColor, getColor, isInside } from "../grid";
|
||||
|
||||
it("should set / get cell", () => {
|
||||
const grid = generateEmptyGrid(2, 3);
|
||||
|
||||
expect(getColor(grid, 0, 1)).toBe(null);
|
||||
|
||||
setColor(grid, 0, 1, 1);
|
||||
|
||||
expect(getColor(grid, 0, 1)).toBe(1);
|
||||
});
|
||||
|
||||
test.each([
|
||||
[0, 1, true],
|
||||
[1, 2, true],
|
||||
|
||||
[-1, 1, false],
|
||||
[0, -1, false],
|
||||
[2, 1, false],
|
||||
[0, 3, false],
|
||||
])("isInside", (x, y, output) => {
|
||||
const grid = generateEmptyGrid(2, 3);
|
||||
|
||||
expect(isInside(grid, x, y)).toBe(output);
|
||||
});
|
||||
24
packages/compute/__tests__/snake.spec.ts
Normal file
24
packages/compute/__tests__/snake.spec.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { snakeSelfCollide } from "../snake";
|
||||
|
||||
test.each([
|
||||
[[{ x: 0, y: 0 }], false],
|
||||
[
|
||||
[
|
||||
{ x: 0, y: 0 },
|
||||
{ x: 0, y: 0 },
|
||||
],
|
||||
true,
|
||||
],
|
||||
[
|
||||
[
|
||||
{ x: 1, y: 7 },
|
||||
{ x: 0, y: 6 },
|
||||
{ x: 2, y: 8 },
|
||||
{ x: 1, y: 7 },
|
||||
{ x: 3, y: 9 },
|
||||
],
|
||||
true,
|
||||
],
|
||||
])("should report snake collision", (snake, collide) => {
|
||||
expect(snakeSelfCollide(snake)).toBe(collide);
|
||||
});
|
||||
94
packages/compute/__tests__/step.spec.ts
Normal file
94
packages/compute/__tests__/step.spec.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import { step } from "../step";
|
||||
import { generateEmptyGrid } from "../generateGrid";
|
||||
import { around4 } from "../point";
|
||||
import { setColor, getColor } from "../grid";
|
||||
|
||||
it("should move snake", () => {
|
||||
const grid = generateEmptyGrid(4, 3);
|
||||
const snake = [{ x: 1, y: 1 }];
|
||||
const direction = around4[0];
|
||||
const stack: number[] = [];
|
||||
const options = { maxSnakeLength: 5 };
|
||||
|
||||
step(grid, snake, stack, direction, options);
|
||||
|
||||
expect(snake).toEqual([
|
||||
{ x: 2, y: 1 },
|
||||
{ x: 1, y: 1 },
|
||||
]);
|
||||
|
||||
step(grid, snake, stack, direction, options);
|
||||
|
||||
expect(snake).toEqual([
|
||||
{ x: 3, y: 1 },
|
||||
{ x: 2, y: 1 },
|
||||
{ x: 1, y: 1 },
|
||||
]);
|
||||
|
||||
step(grid, snake, stack, direction, options);
|
||||
|
||||
expect(snake).toEqual([
|
||||
{ x: 4, y: 1 },
|
||||
{ x: 3, y: 1 },
|
||||
{ x: 2, y: 1 },
|
||||
{ x: 1, y: 1 },
|
||||
]);
|
||||
});
|
||||
|
||||
it("should move short snake", () => {
|
||||
const grid = generateEmptyGrid(8, 3);
|
||||
const snake = [{ x: 1, y: 1 }];
|
||||
const direction = around4[0];
|
||||
const stack: number[] = [];
|
||||
const options = { maxSnakeLength: 3 };
|
||||
|
||||
step(grid, snake, stack, direction, options);
|
||||
|
||||
expect(snake).toEqual([
|
||||
{ x: 2, y: 1 },
|
||||
{ x: 1, y: 1 },
|
||||
]);
|
||||
|
||||
step(grid, snake, stack, direction, options);
|
||||
|
||||
expect(snake).toEqual([
|
||||
{ x: 3, y: 1 },
|
||||
{ x: 2, y: 1 },
|
||||
{ x: 1, y: 1 },
|
||||
]);
|
||||
|
||||
step(grid, snake, stack, direction, options);
|
||||
|
||||
expect(snake).toEqual([
|
||||
{ x: 4, y: 1 },
|
||||
{ x: 3, y: 1 },
|
||||
{ x: 2, y: 1 },
|
||||
]);
|
||||
|
||||
step(grid, snake, stack, direction, options);
|
||||
|
||||
expect(snake).toEqual([
|
||||
{ x: 5, y: 1 },
|
||||
{ x: 4, y: 1 },
|
||||
{ x: 3, y: 1 },
|
||||
]);
|
||||
});
|
||||
|
||||
it("should pick up fruit", () => {
|
||||
const grid = generateEmptyGrid(4, 3);
|
||||
const snake = [{ x: 1, y: 1 }];
|
||||
const direction = around4[0];
|
||||
const stack: number[] = [];
|
||||
const options = { maxSnakeLength: 2 };
|
||||
setColor(grid, 3, 1, 9);
|
||||
|
||||
step(grid, snake, stack, direction, options);
|
||||
|
||||
expect(getColor(grid, 3, 1)).toBe(9);
|
||||
expect(stack).toEqual([]);
|
||||
|
||||
step(grid, snake, stack, direction, options);
|
||||
|
||||
expect(getColor(grid, 3, 1)).toBe(null);
|
||||
expect(stack).toEqual([9]);
|
||||
});
|
||||
@@ -2,6 +2,9 @@ import { Grid, Color } from "./grid";
|
||||
|
||||
const rand = (a: number, b: number) => Math.floor(Math.random() * (b - a)) + a;
|
||||
|
||||
export const generateEmptyGrid = (width: number, height: number) =>
|
||||
generateGrid(width, height, { colors: [], emptyP: 1 });
|
||||
|
||||
export const generateGrid = (
|
||||
width: number,
|
||||
height: number,
|
||||
|
||||
@@ -12,9 +12,14 @@ export const getIndex = (grid: Grid, x: number, y: number) =>
|
||||
export const isInside = (grid: Grid, x: number, y: number) =>
|
||||
x >= 0 && y >= 0 && x < grid.width && y < grid.height;
|
||||
|
||||
export const isInsideLarge = (grid: Grid, m: number, x: number, y: number) =>
|
||||
x >= -m && y >= -m && x < grid.width + m && y < grid.height + m;
|
||||
|
||||
export const getColor = (grid: Grid, x: number, y: number) =>
|
||||
grid.data[getIndex(grid, x, y)];
|
||||
|
||||
export const copyGrid = (grid: Grid) => ({ ...grid, data: grid.data.slice() });
|
||||
|
||||
export const setColor = (
|
||||
grid: Grid,
|
||||
x: number,
|
||||
|
||||
@@ -1,13 +1,44 @@
|
||||
import { Grid, Color } from "./grid";
|
||||
import { Grid, Color, copyGrid, isInsideLarge } from "./grid";
|
||||
import { Point, around4 } from "./point";
|
||||
import { stepSnake, step } from "./step";
|
||||
import { copySnake, snakeSelfCollide } from "./snake";
|
||||
|
||||
type Point = { x: number; y: number };
|
||||
const isGridEmpty = (grid: Grid) => grid.data.every((x) => x === null);
|
||||
|
||||
export const computeBestRun = (
|
||||
pattern: Color[],
|
||||
grid: Grid,
|
||||
originalPosition: Point
|
||||
snake: Point[],
|
||||
options: { maxSnakeLength: number }
|
||||
) => {
|
||||
console.log(pattern, grid, originalPosition);
|
||||
const g = copyGrid(grid);
|
||||
const s = copySnake(snake);
|
||||
const q: Color[] = [];
|
||||
|
||||
return [];
|
||||
const commands: Point[] = [];
|
||||
|
||||
let u = 100;
|
||||
|
||||
while (!isGridEmpty(g) && u-- > 0) {
|
||||
let direction;
|
||||
|
||||
for (let k = 10; k--; ) {
|
||||
direction = around4[Math.floor(Math.random() * around4.length)];
|
||||
|
||||
const sn = copySnake(s);
|
||||
stepSnake(sn, direction, options);
|
||||
|
||||
if (isInsideLarge(g, 1, sn[0].x, sn[0].y) && !snakeSelfCollide(sn)) {
|
||||
break;
|
||||
} else {
|
||||
direction = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
if (direction !== undefined) {
|
||||
step(g, s, q, direction, options);
|
||||
commands.push(direction);
|
||||
}
|
||||
}
|
||||
|
||||
return commands;
|
||||
};
|
||||
|
||||
8
packages/compute/point.ts
Normal file
8
packages/compute/point.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
export type Point = { x: number; y: number };
|
||||
|
||||
export const around4 = [
|
||||
{ x: 1, y: 0 },
|
||||
{ x: 0, y: -1 },
|
||||
{ x: -1, y: 0 },
|
||||
{ x: 0, y: 1 },
|
||||
];
|
||||
24
packages/compute/snake.ts
Normal file
24
packages/compute/snake.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { Point } from "./point";
|
||||
|
||||
export const snakeSelfCollideNext = (
|
||||
snake: Point[],
|
||||
direction: Point,
|
||||
options: { maxSnakeLength: number }
|
||||
) => {
|
||||
const hx = snake[0].x + direction.x;
|
||||
const hy = snake[0].y + direction.y;
|
||||
|
||||
for (let i = 0; i < Math.min(options.maxSnakeLength, snake.length); i++)
|
||||
if (snake[i].x === hx && snake[i].y === hy) return true;
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
export const snakeSelfCollide = (snake: Point[]) => {
|
||||
for (let i = 1; i < snake.length; i++)
|
||||
if (snake[i].x === snake[0].x && snake[i].y === snake[0].y) return true;
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
export const copySnake = (x: Point[]) => x.map((p) => ({ ...p }));
|
||||
48
packages/compute/step.ts
Normal file
48
packages/compute/step.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { Grid, Color, getColor, isInside, setColor } from "./grid";
|
||||
import { Point } from "./point";
|
||||
|
||||
const moveSnake = (snake: Point[], headx: number, heady: number) => {
|
||||
for (let k = snake.length - 1; k > 0; k--) {
|
||||
snake[k].x = snake[k - 1].x;
|
||||
snake[k].y = snake[k - 1].y;
|
||||
}
|
||||
snake[0].x = headx;
|
||||
snake[0].y = heady;
|
||||
};
|
||||
|
||||
export const stepSnake = (
|
||||
snake: Point[],
|
||||
direction: Point,
|
||||
options: { maxSnakeLength: number }
|
||||
) => {
|
||||
const headx = snake[0].x + direction.x;
|
||||
const heady = snake[0].y + direction.y;
|
||||
|
||||
if (snake.length === options.maxSnakeLength) {
|
||||
moveSnake(snake, headx, heady);
|
||||
} else {
|
||||
snake.unshift({ x: headx, y: heady });
|
||||
}
|
||||
};
|
||||
|
||||
export const stepPicking = (grid: Grid, snake: Point[], stack: Color[]) => {
|
||||
if (isInside(grid, snake[0].x, snake[0].y)) {
|
||||
const c = getColor(grid, snake[0].x, snake[0].y);
|
||||
|
||||
if (c) {
|
||||
setColor(grid, snake[0].x, snake[0].y, null);
|
||||
stack.push(c);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const step = (
|
||||
grid: Grid,
|
||||
snake: Point[],
|
||||
stack: Color[],
|
||||
direction: Point,
|
||||
options: { maxSnakeLength: number }
|
||||
) => {
|
||||
stepSnake(snake, direction, options);
|
||||
stepPicking(grid, snake, stack);
|
||||
};
|
||||
Reference in New Issue
Block a user