Skip to content

Commit badef7d

Browse files
committed
working, kind of
1 parent 3bb7cff commit badef7d

File tree

5 files changed

+79
-28
lines changed

5 files changed

+79
-28
lines changed

cargo/snk-js/src/lib.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,8 @@ pub fn solve(grid: &IColorGrid, snake: Vec<IPoint>) -> Vec<IPoint> {
9494

9595
log::info!("{:?}", res);
9696

97-
let mut p = snake.get_head();
9897
res.into_iter()
99-
.map(|dir| {
100-
p = p + dir.to_point();
101-
p
102-
})
98+
.map(|dir| dir.to_point())
10399
.map(IPoint::from)
104100
.collect()
105101
}

cargo/snk-solver/src/snake_path.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,26 @@ pub fn get_snake_path(
5858
while let Some(node) = open_list.pop() {
5959
loop_count += 1;
6060

61-
if loop_count > 10_000 {
62-
println!("loop_count exceeded");
63-
return None;
61+
if loop_count > 20_000 {
62+
panic!("loop_count exceeded")
6463
}
6564

6665
{
6766
let head = node.snake.get_head();
6867

6968
if to == head {
70-
let mut path = node.path;
71-
72-
path.reverse();
73-
return Some((path, node.cost));
69+
return Some((node.path, node.cost));
7470
}
7571
}
7672

7773
for dir in iter_directions() {
74+
// log::info!(
75+
// " - snake {:?} dir {:?}, {:?}",
76+
// node.snake,
77+
// dir,
78+
// snake_will_self_collide(&node.snake, dir)
79+
// );
80+
7881
if snake_will_self_collide(&node.snake, dir) {
7982
continue;
8083
}

cargo/snk-solver/src/solver.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ pub fn solve(color_grid: &Grid<Color>, snake: &Snake4) -> Vec<Direction> {
100100
&color_grid,
101101
&snake,
102102
point,
103-
tunnel.in_cost + Color::Color1.into(),
103+
Cost::max(),
104+
// tunnel.in_cost + Cost::from(Color::Color1) ,
104105
)
105106
.unwrap();
106107

packages/demo3/dev.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@ console.log(`Listening on ${server.url}`);
3333
);
3434
const cwd = path.resolve(__dirname, "../../cargo/snk-js");
3535
const gitIgnore = path.resolve(__dirname, "../snk-js/.gitignore");
36-
childProcess.execSync(
37-
`${wasmPackFile} build --dev --target web --out-dir ../../packages/snk-js && rm ${gitIgnore}`,
38-
{ cwd },
39-
);
36+
37+
try {
38+
childProcess.execSync(
39+
`${wasmPackFile} build --dev --target web --out-dir ../../packages/snk-js && rm ${gitIgnore}`,
40+
{ cwd },
41+
);
42+
} catch (err) {
43+
console.error(err);
44+
}
4045
};
4146

4247
let timeout: number | Timer | undefined;

packages/demo3/solver/index.ts

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
import { getColor, isInside, setColorEmpty } from "@snk/types/grid";
2+
import {
3+
createSnakeFromCells,
4+
getHeadX,
5+
getHeadY,
6+
nextSnake,
7+
} from "@snk/types/snake";
18
import init, {
29
get_grid_sample,
310
IPoint,
@@ -12,23 +19,62 @@ await init(wasmUrl);
1219
init_panic_hook();
1320
init_log();
1421

15-
const grid = get_grid_sample("cave2");
16-
const { canvas, draw, highlightCell } = createCanvas(grid);
22+
const igrid = get_grid_sample(
23+
// "caves",
24+
"labyrinth",
25+
// "cave",
26+
// "one-dot",
27+
);
28+
const { ctx, canvas, draw } = createCanvas(igrid);
1729
document.body.appendChild(canvas);
1830

19-
const path = solve(grid, [
31+
const isnake = [
2032
IPoint.create(0, -1),
2133
IPoint.create(1, -1),
2234
IPoint.create(2, -1),
2335
IPoint.create(3, -1),
24-
]);
36+
];
37+
const snake0 = createSnakeFromCells(isnake.map(({ x, y }) => ({ x, y })));
2538

26-
draw(
27-
{ width: grid.width, height: grid.height, data: grid.cells },
28-
[] as any,
29-
[],
30-
);
39+
const path = solve(igrid, isnake);
40+
41+
let i = 0;
42+
const onChange = () => {
43+
ctx.clearRect(0, 0, 9999, 9999);
44+
45+
const grid = {
46+
width: igrid.width,
47+
height: igrid.height,
48+
data: new Uint8Array(igrid.cells),
49+
};
50+
51+
let snake = snake0;
52+
53+
for (let k = 0; k < i && k < path.length; k++) {
54+
snake = nextSnake(snake, path[k].x, path[k].y);
55+
if (isInside(grid, getHeadX(snake), getHeadY(snake))) {
56+
setColorEmpty(grid, getHeadX(snake), getHeadY(snake));
57+
}
58+
}
59+
60+
draw(grid, snake, []);
61+
};
3162

32-
console.log(path);
63+
onChange();
3364

34-
for (const { x, y } of path) highlightCell(x, y);
65+
const inputI = document.createElement("input") as any;
66+
inputI.type = "range";
67+
inputI.value = 0;
68+
inputI.max = path ? path.length : 0;
69+
inputI.step = 1;
70+
inputI.min = 0;
71+
inputI.style.width = "90%";
72+
inputI.style.padding = "20px 0";
73+
inputI.addEventListener("input", () => {
74+
i = +inputI.value;
75+
onChange();
76+
});
77+
document.body.append(inputI);
78+
document.body.onclick = () => {
79+
inputI.focus();
80+
};

0 commit comments

Comments
 (0)