This commit is contained in:
platane
2025-03-23 15:24:13 +01:00
parent c135277bdf
commit 42f5b68655
6 changed files with 94 additions and 3 deletions

View File

@@ -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]
fn it_should_sort_cell() {
assert_eq!(Cell::Empty < Cell::Color1, true);

View File

@@ -1,4 +1,6 @@
mod grid;
mod snake;
mod snake_walk;
mod solver;
use grid::{Cell, Grid};
@@ -6,9 +8,6 @@ use js_sys;
use solver::get_free_cell;
use wasm_bindgen::prelude::*;
use log::info;
use log::Level;
#[wasm_bindgen]
extern "C" {
fn alert(s: &str);

View 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 },
]
);
}

View File

@@ -0,0 +1,6 @@
use crate::grid::Point;
/**
* head is at 0
*/
pub type Snake = Vec<Point>;

View 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()
}

View File

@@ -84,6 +84,12 @@ export const tunnels = createFromAscii(`
#.# #.# #.#
#.# ### # #
`);
export const line = createFromAscii(`
#######
.. #
##### #
`);
const createRandom = (width: number, height: number, emptyP: number) => {
const grid = createEmptyGrid(width, height);