Skip to content

Commit 1898ec1

Browse files
committed
🚀 improve command computation
1 parent fd9d7da commit 1898ec1

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

packages/action/generateContributionSnake.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const generateContributionSnake = async (userName: string) => {
4242
colors: Array.from({ length: colorScheme.length - 1 }, (_, i) => i + 1),
4343
};
4444

45-
const gifOptions = { delay: 10 };
45+
const gifOptions = { delay: 3 };
4646

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

packages/compute/index.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,25 @@ const createComputeHeuristic = (
2020
const values = colors
2121
.map((k) => Array.from({ length: colorCount[k] }, () => k))
2222
.flat();
23+
const weights = colors
24+
.map((k) =>
25+
Array.from({ length: colorCount[k] }).map(
26+
(_, i, arr) => i / (arr.length - 1)
27+
)
28+
)
29+
.flat();
2330

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

2734
for (let i = 0; i < stack.length; i++) {
28-
const k = Math.abs(stack[i] - values[i]);
29-
score += k === 0 ? 100 : -100 * k;
35+
const u = stack[i] - values[i];
36+
37+
if (u !== 0) debugger;
38+
39+
if (u > 0) score -= 100 * u * (1 + 1 - weights[i]);
40+
else if (u < 0) score -= 100 * -u * (1 + weights[i]);
41+
else score += 100;
3042
}
3143

3244
return score;
@@ -45,13 +57,11 @@ const createCell = (
4557
grid: Grid,
4658
snake: Snake,
4759
stack: Color[],
48-
direction: Point | null,
4960
parent: any | null,
5061
heuristic: number
5162
) => ({
5263
key,
5364
parent,
54-
direction,
5565
grid,
5666
snake,
5767
stack,
@@ -60,13 +70,12 @@ const createCell = (
6070
});
6171

6272
const unwrap = (c: ReturnType<typeof createCell> | null): Point[] =>
63-
c && c.direction ? [...unwrap(c.parent), c.direction] : [];
64-
// c && c.parent
65-
// ? [
66-
// ...unwrap(c.parent),
67-
// { x: c.snake[1].x - c.snake[0].x, y: c.snake[1].y - c.snake[0].y },
68-
// ]
69-
// : [];
73+
c && c.parent
74+
? [
75+
...unwrap(c.parent),
76+
{ x: c.snake[0].x - c.snake[1].x, y: c.snake[0].y - c.snake[1].y },
77+
]
78+
: [];
7079

7180
export const computeBestRun = (
7281
grid0: Grid,
@@ -91,12 +100,11 @@ export const computeBestRun = (
91100
snake0,
92101
[],
93102
null,
94-
null,
95103
computeHeuristic(grid0, snake0, [])
96104
),
97105
];
98106

99-
let u = 7000;
107+
let u = 8000;
100108

101109
let best = openList[0];
102110

@@ -130,7 +138,6 @@ export const computeBestRun = (
130138
grid,
131139
snake,
132140
stack,
133-
direction,
134141
c,
135142
computeHeuristic(grid, snake, stack)
136143
)

packages/demo/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ const drawOptions = {
1515
colorSnake: "purple",
1616
};
1717

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

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

2222
const snake0 = [
2323
{ x: 4, y: -1 },

0 commit comments

Comments
 (0)