|
| 1 | +use snk_grid::{ |
| 2 | + color::Color, |
| 3 | + direction::{Direction, add_direction, iter_directions, iter_neighbour}, |
| 4 | + grid::{Grid, iter_rectangle_hull}, |
| 5 | + grid_samples::{SampleGrid, get_grid_sample}, |
| 6 | + point::Point, |
| 7 | + snake::{Snake, Snake4, snake_will_self_collide}, |
| 8 | +}; |
| 9 | +use std::collections::{BinaryHeap, HashSet}; |
| 10 | + |
| 11 | +use crate::{ |
| 12 | + cost::Cost, |
| 13 | + path_to_outside_grid::{ExitDirection, create_path_to_outside}, |
| 14 | +}; |
| 15 | + |
| 16 | +#[derive(Clone, Debug)] |
| 17 | +struct Node { |
| 18 | + pub snake: Snake4, |
| 19 | + pub cost: Cost, |
| 20 | + pub path: Vec<Direction>, |
| 21 | +} |
| 22 | + |
| 23 | +impl Eq for Node {} |
| 24 | +impl PartialEq for Node { |
| 25 | + fn eq(&self, other: &Self) -> bool { |
| 26 | + self.snake == other.snake |
| 27 | + } |
| 28 | +} |
| 29 | +impl Ord for Node { |
| 30 | + fn cmp(&self, other: &Self) -> std::cmp::Ordering { |
| 31 | + other.cost.cmp(&self.cost) |
| 32 | + } |
| 33 | +} |
| 34 | +impl PartialOrd for Node { |
| 35 | + fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { |
| 36 | + Some(self.cmp(other)) |
| 37 | + } |
| 38 | +} |
| 39 | +pub fn get_snake_path_to_outside<FnOutside, FnCost>( |
| 40 | + is_outside: FnOutside, |
| 41 | + get_walk_cost: FnCost, |
| 42 | + snake: &Snake4, |
| 43 | +) -> (Vec<Direction>, Cost) |
| 44 | +where |
| 45 | + FnOutside: Fn(Point) -> bool, |
| 46 | + FnCost: Fn(Point) -> Cost, |
| 47 | +{ |
| 48 | + let mut open_list: BinaryHeap<Node> = BinaryHeap::new(); |
| 49 | + let mut close_list: HashSet<Snake4> = HashSet::new(); |
| 50 | + |
| 51 | + open_list.push(Node { |
| 52 | + snake: snake.clone(), |
| 53 | + cost: Cost::zero(), |
| 54 | + path: Vec::new(), |
| 55 | + }); |
| 56 | + |
| 57 | + while let Some(node) = open_list.pop() { |
| 58 | + let head = node.snake.get_head(); |
| 59 | + if is_outside(head) { |
| 60 | + return (node.path, node.cost); |
| 61 | + } |
| 62 | + |
| 63 | + for dir in iter_directions() { |
| 64 | + if snake_will_self_collide(&node.snake, dir) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + let snake = node.snake.clone_and_move(dir); |
| 69 | + |
| 70 | + if close_list.contains(&snake) { |
| 71 | + // need to update open_list |
| 72 | + println!("need to update open_list"); |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + let head = snake.get_head(); |
| 77 | + |
| 78 | + let cost = node.cost + get_walk_cost(head); |
| 79 | + let mut path = node.path.clone(); |
| 80 | + path.push(dir); |
| 81 | + |
| 82 | + open_list.push(Node { cost, path, snake }); |
| 83 | + } |
| 84 | + |
| 85 | + close_list.insert(node.snake); |
| 86 | + } |
| 87 | + |
| 88 | + assert!(false, "should have terminated"); |
| 89 | + (vec![], Cost::zero()) |
| 90 | +} |
| 91 | + |
| 92 | +#[test] |
| 93 | +fn it_should_get_snake_path_to_outside() { |
| 94 | + let grid = Grid::<_>::from( |
| 95 | + r#" |
| 96 | +_######## _ |
| 97 | +_# . . _ |
| 98 | +_# # _ |
| 99 | +_######## _ |
| 100 | +
|
| 101 | +"#, |
| 102 | + ); |
| 103 | + let pto = create_path_to_outside(&grid); |
| 104 | + let snake = Snake4::from_points([ |
| 105 | + Point { x: 2, y: 2 }, |
| 106 | + Point { x: 3, y: 2 }, |
| 107 | + Point { x: 4, y: 2 }, |
| 108 | + Point { x: 5, y: 2 }, |
| 109 | + ]); |
| 110 | + |
| 111 | + let is_outside = |p| !pto.is_inside(p) || pto.get(p).is_outside(); |
| 112 | + |
| 113 | + let (path, cost) = get_snake_path_to_outside(is_outside, |p| grid.get_color(p).into(), &snake); |
| 114 | + |
| 115 | + println!("{:?} {:?}", path, cost); |
| 116 | + assert!( |
| 117 | + cost < Cost::from(Color::Color2) * 2, |
| 118 | + "should have taken the smallest path" |
| 119 | + ); |
| 120 | +} |
0 commit comments