|
1 | 1 | use js_sys; |
2 | | -use snk_grid::{color::Color, grid_samples::SampleGrid, point::Point, snake::Snake4}; |
| 2 | +use snk_grid::{ |
| 3 | + color::Color, |
| 4 | + grid_samples::SampleGrid, |
| 5 | + point::Point, |
| 6 | + snake::{Snake, Snake4}, |
| 7 | +}; |
3 | 8 | use wasm_bindgen::prelude::*; |
4 | 9 |
|
5 | 10 | #[wasm_bindgen] |
@@ -51,11 +56,38 @@ pub fn get_best_tunnel_to_collect_point(grid: &IColorGrid, to: &IPoint) -> Vec<I |
51 | 56 | let res = |
52 | 57 | snk_solver::collect_cost::get_best_tunnel_to_collect_point(&grid, &exit_grid, to.into()); |
53 | 58 |
|
54 | | - log::info!("{:?} {:?} {:?}",res.path,res.in_cost,res.out_cost); |
55 | | - |
| 59 | + log::info!("{:?} {:?} {:?}", res.path, res.in_cost, res.out_cost); |
56 | 60 |
|
57 | 61 | res.path.into_iter().map(IPoint::from).collect() |
58 | 62 | } |
| 63 | +#[wasm_bindgen] |
| 64 | +pub fn get_snake_path_to_outside(grid: &IColorGrid, snake: Vec<IPoint>) -> Vec<IPoint> { |
| 65 | + let grid = snk_grid::grid::Grid::from(grid); |
| 66 | + let exit_grid = snk_solver::exit_grid::ExitGrid::create_from_grid_color(&grid); |
| 67 | + let snake = Snake4::from_points( |
| 68 | + snake |
| 69 | + .into_iter() |
| 70 | + .map(|p| Point::from(p)) |
| 71 | + .collect::<Vec<_>>() |
| 72 | + .try_into() |
| 73 | + .expect("snake should be 4 points"), |
| 74 | + ); |
| 75 | + let res = snk_solver::snake_path_to_outside::get_snake_path_to_outside( |
| 76 | + |p| exit_grid.is_outside(p), |
| 77 | + |p| grid.get_color(p).into(), |
| 78 | + &snake, |
| 79 | + ); |
| 80 | + |
| 81 | + let mut p = snake.get_head(); |
| 82 | + res.0 |
| 83 | + .into_iter() |
| 84 | + .map(|dir| { |
| 85 | + p = p + dir.to_point(); |
| 86 | + p |
| 87 | + }) |
| 88 | + .map(IPoint::from) |
| 89 | + .collect() |
| 90 | +} |
59 | 91 |
|
60 | 92 | // #[wasm_bindgen] |
61 | 93 | // pub fn solve(grid: IColorGrid) -> js_sys::Uint8Array { |
@@ -123,6 +155,10 @@ impl IColorGrid { |
123 | 155 | let o: Vec<u8> = self.cells.iter().map(|u| *u as u8).collect(); |
124 | 156 | js_sys::Uint8Array::from(&o[..]) |
125 | 157 | } |
| 158 | + |
| 159 | + pub fn set(&mut self, x: i8, y: i8, color: u8) { |
| 160 | + self.cells[(x as usize) * (self.height as usize) + (y as usize)] = color.into(); |
| 161 | + } |
126 | 162 | } |
127 | 163 |
|
128 | 164 | impl From<IColorGrid> for snk_grid::grid::Grid<Color> { |
|
0 commit comments