Skip to content

Instantly share code, notes, and snippets.

@TyOverby TyOverby/astar_grid.rs Secret
Created Nov 4, 2014

Embed
What would you like to do?
struct Grid2d {
// your grid goes here, assume that we have an "is_moveable" method implemented on Grid2d
}
struct Grid2dSearch<'a> {
grid: &'a Grid2d,
start: (i32, i32),
end: (i32, i32)
}
impl SearchProblem<(i32, i32), i32, MoveItems<((i32, i32), i32)>> for Grid2dSearch {
fn start(&self) -> (i32, i32) { self.start }
fn is_end(&self, p: &(i32, i32)) -> bool { *p == self.end }
fn heuristic(&self, &(p_x, p_y): &(i32, i32)) -> i32 {
let (s_x, s_y) = self.end;
(s_x - p_x).abs() + (s_y - p_y).abs()
}
fn neighbors(&self, &(x, y): &(i32, i32)) -> MoveItems<((i32, i32), i32)> {
let mut vec = vec![];
for i in range_inclusive(-1, 1) {
for k in range_inclusive(-1, 1) {
if !(i == 0 && k == 0) {
if self.grid.is_moveable(x+i, y+k) {
vec.push(((x + i, y + k), 1));
}
}
}
}
vec.into_iter()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.