Skip to content

Commit 37d733b

Browse files
committed
..
1 parent 2955eeb commit 37d733b

File tree

9 files changed

+65
-51
lines changed

9 files changed

+65
-51
lines changed

cargo/snk-grid/src/color.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
2+
#[repr(u8)]
3+
pub enum Color {
4+
Empty = 0,
5+
Color1 = 1,
6+
Color2 = 2,
7+
Color3 = 3,
8+
Color4 = 4,
9+
}
10+
impl Default for Color {
11+
fn default() -> Self {
12+
Color::Empty
13+
}
14+
}
15+
impl Color {
16+
pub fn is_walkable(&self, walkable: Color) -> bool {
17+
*self <= walkable
18+
}
19+
pub fn is_empty(&self) -> bool {
20+
*self == Color::Empty
21+
}
22+
pub fn cost(&self) -> u32 {
23+
match self {
24+
Color::Empty => 0,
25+
Color::Color1 => 1,
26+
Color::Color2 => 150,
27+
Color::Color3 => 150 * 150,
28+
Color::Color4 => 150 * 150 * 150,
29+
}
30+
}
31+
}
32+
33+
#[test]
34+
fn it_should_sort_cell() {
35+
assert_eq!(Color::Empty < Color::Color1, true);
36+
assert_eq!(Color::Color1 < Color::Color2, true);
37+
assert_eq!(Color::Color2 < Color::Color3, true);
38+
assert_eq!(Color::Color3 < Color::Color4, true);
39+
}
40+
#[test]
41+
fn it_should_sum_cost() {
42+
// it should not panic
43+
let _somehow_max_cost = Color::Color4.cost() * 350;
44+
}

cargo/snk-grid/src/grid.rs

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,4 @@
1-
use crate::point::Point;
2-
3-
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
4-
#[repr(u8)]
5-
pub enum Color {
6-
Empty = 0,
7-
Color1 = 1,
8-
Color2 = 2,
9-
Color3 = 3,
10-
Color4 = 4,
11-
}
12-
impl Default for Color {
13-
fn default() -> Self {
14-
Color::Empty
15-
}
16-
}
17-
impl Color {
18-
pub fn is_walkable(&self, walkable: Color) -> bool {
19-
*self <= walkable
20-
}
21-
pub fn is_empty(&self) -> bool {
22-
*self == Color::Empty
23-
}
24-
}
1+
use crate::{color::Color, point::Point};
252

263
pub fn iter_rectangle_fill(width: i8, height: i8) -> impl Iterator<Item = Point> {
274
(0..height).flat_map(move |y| (0..width).map(move |x| Point { x, y }))
@@ -115,13 +92,6 @@ impl Grid<Color> {
11592
}
11693
}
11794

118-
#[test]
119-
fn it_should_sort_cell() {
120-
assert_eq!(Color::Empty < Color::Color1, true);
121-
assert_eq!(Color::Color1 < Color::Color2, true);
122-
assert_eq!(Color::Color2 < Color::Color3, true);
123-
assert_eq!(Color::Color3 < Color::Color4, true);
124-
}
12595
#[test]
12696
fn it_should_grid_create() {
12797
let grid = Grid::<Color>::create_with_default(30, 10);

cargo/snk-grid/src/grid_ascii.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::{
2-
grid::{Color, Grid, iter_rectangle_fill},
2+
color::Color,
3+
grid::{Grid, iter_rectangle_fill},
34
point::Point,
45
};
56

cargo/snk-grid/src/grid_samples.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use crate::grid::{Color, Grid, iter_rectangle_fill};
1+
use crate::{
2+
color::Color,
3+
grid::{Grid, iter_rectangle_fill},
4+
};
25

36
// Pseudorandom number generator from the "Xorshift RNGs" paper by George Marsaglia.
47
// https://github.com/rust-lang/rust/blob/1.55.0/library/core/src/slice/sort.rs#L559-L573

cargo/snk-grid/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod color;
12
pub mod direction;
23
pub mod grid;
34
pub mod grid_ascii;

cargo/snk-js/src/lib.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
use js_sys;
22
use log::info;
3-
use snk_grid::{
4-
direction::Direction,
5-
grid::{Color, Grid},
6-
grid_samples::SampleGrid,
7-
};
3+
use snk_grid::{color::Color, direction::Direction, grid::Grid, grid_samples::SampleGrid};
84
use wasm_bindgen::prelude::*;
95

106
#[wasm_bindgen]

cargo/snk-solver/src/cost_to_outside.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use snk_grid::{
2+
color::Color,
23
direction::iter_neighbour,
3-
grid::{Color, Grid, iter_rectangle_hull},
4+
grid::{Grid, iter_rectangle_hull},
45
point::Point,
56
};
6-
use std::{collections::HashSet, usize};
7+
use std::collections::HashSet;
78

89
pub fn update_cost_to_outside(
9-
cost_to_outside: &mut Grid<usize>,
10+
cost_to_outside: &mut Grid<u32>,
1011
grid: &Grid<Color>,
1112
mut changed: HashSet<Point>,
1213
) -> () {
@@ -21,7 +22,8 @@ pub fn update_cost_to_outside(
2122
// propagate the change to its neightbourn
2223
for pn in iter_neighbour(p) {
2324
if cost_to_outside.is_inside(pn) {
24-
let new_cost = c + (grid.get(pn) as usize) * 100;
25+
let cost = grid.get(pn).cost();
26+
let new_cost = c + cost;
2527
if new_cost < cost_to_outside.get(pn) {
2628
cost_to_outside.set(pn, new_cost);
2729
changed.insert(pn);
@@ -33,13 +35,13 @@ pub fn update_cost_to_outside(
3335

3436
//
3537
// cost_to_outside : for each cell return the minimal cost ( = sum of dot, with greater color costing more ) to get outside
36-
pub fn create_cost_to_outside(grid: &Grid<Color>) -> Grid<usize> {
37-
let mut cost_to_outside = Grid::<usize>::create_with_value(grid.width, grid.height, usize::MAX);
38+
pub fn create_cost_to_outside(grid: &Grid<Color>) -> Grid<u32> {
39+
let mut cost_to_outside = Grid::<u32>::create_with_value(grid.width, grid.height, u32::MAX);
3840

3941
let mut changed = HashSet::<Point>::new();
4042

4143
for p in iter_rectangle_hull(grid.width as i8, grid.height as i8) {
42-
let cost = (grid.get(p) as usize) * 100;
44+
let cost = grid.get(p).cost();
4345
cost_to_outside.set(p, cost);
4446
changed.insert(p);
4547
}

cargo/snk-solver/src/fitness.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use snk_grid::{
2+
color::Color,
23
direction::Direction,
3-
grid::{Color, Grid, iter_rectangle_fill},
4+
grid::{Grid, iter_rectangle_fill},
45
snake::{Snake, Snake4},
56
};
67

cargo/snk-solver/src/solver.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
use snk_grid::{
2-
direction::Direction,
3-
grid::{Color, Grid},
4-
snake::Snake4,
5-
};
1+
use snk_grid::{color::Color, direction::Direction, grid::Grid, snake::Snake4};
62

73
pub fn solve(grid: &Grid<Color>, snake: &Snake4) -> Vec<Direction> {
84
vec![]

0 commit comments

Comments
 (0)