Skip to content

Commit

Permalink
🚀 improve command computation
Browse files Browse the repository at this point in the history
  • Loading branch information
Platane committed Jul 20, 2020
1 parent fd9d7da commit 1898ec1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/action/generateContributionSnake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const generateContributionSnake = async (userName: string) => {
colors: Array.from({ length: colorScheme.length - 1 }, (_, i) => i + 1),
};

const gifOptions = { delay: 10 };
const gifOptions = { delay: 3 };

const commands = computeBestRun(grid0, snake0, gameOptions);

Expand Down
35 changes: 21 additions & 14 deletions packages/compute/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,25 @@ const createComputeHeuristic = (
const values = colors
.map((k) => Array.from({ length: colorCount[k] }, () => k))
.flat();
const weights = colors
.map((k) =>
Array.from({ length: colorCount[k] }).map(
(_, i, arr) => i / (arr.length - 1)
)
)
.flat();

return (_grid: Grid, _snake: Snake, stack: Color[]) => {
let score = 0;

for (let i = 0; i < stack.length; i++) {
const k = Math.abs(stack[i] - values[i]);
score += k === 0 ? 100 : -100 * k;
const u = stack[i] - values[i];

if (u !== 0) debugger;

if (u > 0) score -= 100 * u * (1 + 1 - weights[i]);
else if (u < 0) score -= 100 * -u * (1 + weights[i]);
else score += 100;
}

return score;
Expand All @@ -45,13 +57,11 @@ const createCell = (
grid: Grid,
snake: Snake,
stack: Color[],
direction: Point | null,
parent: any | null,
heuristic: number
) => ({
key,
parent,
direction,
grid,
snake,
stack,
Expand All @@ -60,13 +70,12 @@ const createCell = (
});

const unwrap = (c: ReturnType<typeof createCell> | null): Point[] =>
c && c.direction ? [...unwrap(c.parent), c.direction] : [];
// c && c.parent
// ? [
// ...unwrap(c.parent),
// { x: c.snake[1].x - c.snake[0].x, y: c.snake[1].y - c.snake[0].y },
// ]
// : [];
c && c.parent
? [
...unwrap(c.parent),
{ x: c.snake[0].x - c.snake[1].x, y: c.snake[0].y - c.snake[1].y },
]
: [];

export const computeBestRun = (
grid0: Grid,
Expand All @@ -91,12 +100,11 @@ export const computeBestRun = (
snake0,
[],
null,
null,
computeHeuristic(grid0, snake0, [])
),
];

let u = 7000;
let u = 8000;

let best = openList[0];

Expand Down Expand Up @@ -130,7 +138,6 @@ export const computeBestRun = (
grid,
snake,
stack,
direction,
c,
computeHeuristic(grid, snake, stack)
)
Expand Down
4 changes: 2 additions & 2 deletions packages/demo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const drawOptions = {
colorSnake: "purple",
};

const gameOptions = { colors: [1, 2, 3, 4], maxSnakeLength: 5 };
const gameOptions = { colors: [1, 2, 3], maxSnakeLength: 5 };

const grid0 = generateRandomGrid(42, 7, { ...gameOptions, emptyP: 3 });
const grid0 = generateRandomGrid(18, 7, { ...gameOptions, emptyP: 2 });

const snake0 = [
{ x: 4, y: -1 },
Expand Down

0 comments on commit 1898ec1

Please sign in to comment.