.
This commit is contained in:
@@ -48,6 +48,13 @@ impl Grid {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const DIRECTIONS: [Point; 4] = [
|
||||||
|
Point { x: 1, y: 0 },
|
||||||
|
Point { x: -1, y: 0 },
|
||||||
|
Point { x: 0, y: 1 },
|
||||||
|
Point { x: 0, y: -1 },
|
||||||
|
];
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_should_sort_cell() {
|
fn it_should_sort_cell() {
|
||||||
assert_eq!(Cell::Empty < Cell::Color1, true);
|
assert_eq!(Cell::Empty < Cell::Color1, true);
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
mod grid;
|
mod grid;
|
||||||
|
mod snake;
|
||||||
|
mod snake_walk;
|
||||||
mod solver;
|
mod solver;
|
||||||
|
|
||||||
use grid::{Cell, Grid};
|
use grid::{Cell, Grid};
|
||||||
@@ -6,9 +8,6 @@ use js_sys;
|
|||||||
use solver::get_free_cell;
|
use solver::get_free_cell;
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
use log::info;
|
|
||||||
use log::Level;
|
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
extern "C" {
|
extern "C" {
|
||||||
fn alert(s: &str);
|
fn alert(s: &str);
|
||||||
|
|||||||
59
packages/solver-r/src/snake-compact.rs
Normal file
59
packages/solver-r/src/snake-compact.rs
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
use crate::grid::Point;
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
|
||||||
|
pub enum Direction {
|
||||||
|
Left = 0,
|
||||||
|
Right = 1,
|
||||||
|
Up = 2,
|
||||||
|
Down = 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_direction_vector(dir: &Direction) -> Point {
|
||||||
|
match dir {
|
||||||
|
Direction::Down => Point { x: 0, y: -1 },
|
||||||
|
Direction::Up => Point { x: 0, y: 1 },
|
||||||
|
Direction::Left => Point { x: -1, y: 0 },
|
||||||
|
Direction::Right => Point { x: 1, y: 0 },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct SnakeC {
|
||||||
|
pub head: Point,
|
||||||
|
pub body: Vec<Direction>,
|
||||||
|
}
|
||||||
|
impl SnakeC {
|
||||||
|
pub fn get_cells(&self) -> Vec<Point> {
|
||||||
|
let mut e = self.head.clone();
|
||||||
|
let mut out = Vec::new();
|
||||||
|
|
||||||
|
out.push(e.clone());
|
||||||
|
for dir in self.body.iter() {
|
||||||
|
let v = get_direction_vector(dir);
|
||||||
|
e.x -= v.x;
|
||||||
|
e.y -= v.y;
|
||||||
|
out.push(e.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
out
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_should_get_the_snake_cell() {
|
||||||
|
let s = SnakeC {
|
||||||
|
head: Point { x: 10, y: 5 },
|
||||||
|
body: vec![Direction::Up, Direction::Up, Direction::Left],
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
s.get_cells(),
|
||||||
|
vec![
|
||||||
|
//
|
||||||
|
Point { x: 10, y: 5 },
|
||||||
|
Point { x: 10, y: 4 },
|
||||||
|
Point { x: 10, y: 3 },
|
||||||
|
Point { x: 11, y: 3 },
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
6
packages/solver-r/src/snake.rs
Normal file
6
packages/solver-r/src/snake.rs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
use crate::grid::Point;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* head is at 0
|
||||||
|
*/
|
||||||
|
pub type Snake = Vec<Point>;
|
||||||
14
packages/solver-r/src/snake_walk.rs
Normal file
14
packages/solver-r/src/snake_walk.rs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
use crate::grid::{Cell, Grid, Point, DIRECTIONS};
|
||||||
|
use crate::snake::Snake;
|
||||||
|
|
||||||
|
pub fn get_route_to_eat_all(
|
||||||
|
grid: &Grid,
|
||||||
|
walkable: Cell,
|
||||||
|
initial_snake: &Snake,
|
||||||
|
cells_to_eat: HashSet<Point>,
|
||||||
|
) -> Vec<Snake> {
|
||||||
|
for dir in DIRECTIONS {}
|
||||||
|
Vec::new()
|
||||||
|
}
|
||||||
@@ -84,6 +84,12 @@ export const tunnels = createFromAscii(`
|
|||||||
#.# #.# #.#
|
#.# #.# #.#
|
||||||
#.# ### # #
|
#.# ### # #
|
||||||
`);
|
`);
|
||||||
|
export const line = createFromAscii(`
|
||||||
|
|
||||||
|
#######
|
||||||
|
.. #
|
||||||
|
##### #
|
||||||
|
`);
|
||||||
|
|
||||||
const createRandom = (width: number, height: number, emptyP: number) => {
|
const createRandom = (width: number, height: number, emptyP: number) => {
|
||||||
const grid = createEmptyGrid(width, height);
|
const grid = createEmptyGrid(width, height);
|
||||||
|
|||||||
Reference in New Issue
Block a user