Skip to content

Commit 34d5617

Browse files
committed
.
1 parent be9fca7 commit 34d5617

File tree

5 files changed

+77
-23
lines changed

5 files changed

+77
-23
lines changed

packages/demo/demo.rust.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import { grid } from "./sample";
99

1010
const freeCells = api.iget_free_cell(g);
1111

12-
console.log(freeCells);
13-
1412
{
1513
const { canvas, draw, highlightCell } = createCanvas(g);
1614
document.body.appendChild(canvas);

packages/solver-r/Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/solver-r/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ default = ["console_error_panic_hook"]
1313
[dependencies]
1414
wasm-bindgen = "0.2.100"
1515
js-sys = "0.3.77"
16+
console_log = "1.0.0"
17+
log = "0.4"
1618

1719
# The `console_error_panic_hook` crate provides better debugging of panics by
1820
# logging them with `console.error`. This is great for development, but requires

packages/solver-r/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ use js_sys;
66
use solver::get_free_cell;
77
use wasm_bindgen::prelude::*;
88

9+
use log::info;
10+
use log::Level;
11+
912
#[wasm_bindgen]
1013
extern "C" {
1114
fn alert(s: &str);

packages/solver-r/src/solver.rs

Lines changed: 60 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,21 @@ pub fn get_free_cell(grid: &Grid, walkable: Cell) -> HashSet<Point> {
66
let mut free: HashSet<Point> = HashSet::new();
77
let mut open_list: HashSet<Point> = HashSet::new();
88

9-
for x in -1..((grid.width as i8) + 1) {
9+
for x in 0..(grid.width as i8) {
1010
open_list.insert(Point { x, y: 0 });
1111
open_list.insert(Point {
1212
x,
1313
y: (grid.height as i8) - 1,
1414
});
1515
}
16+
for y in 0..(grid.height as i8) {
17+
open_list.insert(Point { x: 0, y });
18+
open_list.insert(Point {
19+
x: (grid.width as i8) - 1,
20+
y,
21+
});
22+
}
23+
open_list.retain(|p| grid.get_cell(&p) <= walkable);
1624

1725
let directions = [
1826
Point { x: 1, y: 0 },
@@ -24,35 +32,66 @@ pub fn get_free_cell(grid: &Grid, walkable: Cell) -> HashSet<Point> {
2432
while let Some(p) = open_list.iter().next().cloned() {
2533
open_list.remove(&p);
2634

27-
let exit_count: u8 = directions.iter().fold(0, |sum, dir| {
28-
let neighbour = Point {
29-
x: p.x + dir.x,
30-
y: p.y + dir.y,
31-
};
32-
33-
if !grid.is_inside(&neighbour) {
34-
sum + 2
35-
} else if free.contains(&neighbour) {
36-
sum + 1
37-
} else {
38-
sum
39-
}
40-
});
35+
let has_enough_free_exits = {
36+
let mut exit_count = 0;
37+
let mut visited: HashSet<Point> = HashSet::new();
38+
39+
for dir in directions {
40+
let neighbour = Point {
41+
x: p.x + dir.x,
42+
y: p.y + dir.y,
43+
};
44+
45+
if !visited.contains(&neighbour)
46+
&& (free.contains(&neighbour) || !grid.is_inside(&neighbour))
47+
{
48+
visited.insert(neighbour);
49+
exit_count += 1;
50+
}
4151

42-
if exit_count >= 2 {
43-
if grid.is_inside(&p) && grid.get_cell(&p) <= walkable {
44-
free.insert(p);
52+
if grid.is_inside(&neighbour) && grid.get_cell(&neighbour) <= walkable {
53+
for alt in [-1, 1] {
54+
let corner = {
55+
if neighbour.x != 0 {
56+
Point {
57+
x: neighbour.x,
58+
y: neighbour.y + alt,
59+
}
60+
} else {
61+
Point {
62+
x: neighbour.x + alt,
63+
y: neighbour.y,
64+
}
65+
}
66+
};
67+
68+
if !visited.contains(&neighbour)
69+
&& !visited.contains(&corner)
70+
&& (free.contains(&corner) || !grid.is_inside(&corner))
71+
{
72+
visited.insert(neighbour);
73+
visited.insert(corner);
74+
exit_count += 1;
75+
}
76+
}
77+
}
4578
}
4679

80+
exit_count >= 2
81+
};
82+
83+
if has_enough_free_exits {
84+
free.insert(p);
85+
4786
for dir in directions {
4887
let neighbour = Point {
4988
x: p.x + dir.x,
5089
y: p.y + dir.y,
5190
};
5291

53-
if grid.is_inside(&neighbour)
54-
&& (grid.get_cell(&neighbour) <= walkable)
55-
&& !free.contains(&neighbour)
92+
if !free.contains(&neighbour)
93+
&& grid.is_inside(&neighbour)
94+
&& grid.get_cell(&neighbour) <= walkable
5695
{
5796
open_list.insert(neighbour);
5897
}

0 commit comments

Comments
 (0)