WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 42f5b68

Browse files
committed
.
1 parent c135277 commit 42f5b68

File tree

6 files changed

+94
-3
lines changed

6 files changed

+94
-3
lines changed

packages/solver-r/src/grid.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ impl Grid {
4848
}
4949
}
5050

51+
pub const DIRECTIONS: [Point; 4] = [
52+
Point { x: 1, y: 0 },
53+
Point { x: -1, y: 0 },
54+
Point { x: 0, y: 1 },
55+
Point { x: 0, y: -1 },
56+
];
57+
5158
#[test]
5259
fn it_should_sort_cell() {
5360
assert_eq!(Cell::Empty < Cell::Color1, true);

packages/solver-r/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
mod grid;
2+
mod snake;
3+
mod snake_walk;
24
mod solver;
35

46
use grid::{Cell, Grid};
57
use js_sys;
68
use solver::get_free_cell;
79
use wasm_bindgen::prelude::*;
810

9-
use log::info;
10-
use log::Level;
11-
1211
#[wasm_bindgen]
1312
extern "C" {
1413
fn alert(s: &str);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use crate::grid::Point;
2+
3+
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
4+
pub enum Direction {
5+
Left = 0,
6+
Right = 1,
7+
Up = 2,
8+
Down = 3,
9+
}
10+
11+
fn get_direction_vector(dir: &Direction) -> Point {
12+
match dir {
13+
Direction::Down => Point { x: 0, y: -1 },
14+
Direction::Up => Point { x: 0, y: 1 },
15+
Direction::Left => Point { x: -1, y: 0 },
16+
Direction::Right => Point { x: 1, y: 0 },
17+
}
18+
}
19+
20+
#[derive(Clone)]
21+
pub struct SnakeC {
22+
pub head: Point,
23+
pub body: Vec<Direction>,
24+
}
25+
impl SnakeC {
26+
pub fn get_cells(&self) -> Vec<Point> {
27+
let mut e = self.head.clone();
28+
let mut out = Vec::new();
29+
30+
out.push(e.clone());
31+
for dir in self.body.iter() {
32+
let v = get_direction_vector(dir);
33+
e.x -= v.x;
34+
e.y -= v.y;
35+
out.push(e.clone());
36+
}
37+
38+
out
39+
}
40+
}
41+
42+
#[test]
43+
fn it_should_get_the_snake_cell() {
44+
let s = SnakeC {
45+
head: Point { x: 10, y: 5 },
46+
body: vec![Direction::Up, Direction::Up, Direction::Left],
47+
};
48+
49+
assert_eq!(
50+
s.get_cells(),
51+
vec![
52+
//
53+
Point { x: 10, y: 5 },
54+
Point { x: 10, y: 4 },
55+
Point { x: 10, y: 3 },
56+
Point { x: 11, y: 3 },
57+
]
58+
);
59+
}

packages/solver-r/src/snake.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use crate::grid::Point;
2+
3+
/**
4+
* head is at 0
5+
*/
6+
pub type Snake = Vec<Point>;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use std::collections::HashSet;
2+
3+
use crate::grid::{Cell, Grid, Point, DIRECTIONS};
4+
use crate::snake::Snake;
5+
6+
pub fn get_route_to_eat_all(
7+
grid: &Grid,
8+
walkable: Cell,
9+
initial_snake: &Snake,
10+
cells_to_eat: HashSet<Point>,
11+
) -> Vec<Snake> {
12+
for dir in DIRECTIONS {}
13+
Vec::new()
14+
}

packages/types/__fixtures__/grid.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ export const tunnels = createFromAscii(`
8484
#.# #.# #.#
8585
#.# ### # #
8686
`);
87+
export const line = createFromAscii(`
88+
89+
#######
90+
.. #
91+
##### #
92+
`);
8793

8894
const createRandom = (width: number, height: number, emptyP: number) => {
8995
const grid = createEmptyGrid(width, height);

0 commit comments

Comments
 (0)