1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
use std::cmp::Ordering; use crate::coord::Coord; use crate::search::Search; #[derive(Eq, Clone, Copy)] pub struct Node { pub parent: Option<Coord>, pub x: i32, pub y: i32, pub cost: i32, pub distance: i32, pub visited: bool, } impl Node { pub fn new(parent: Option<&Node>, x: i32, y: i32, cost: i32, distance: i32) -> Node { Node { parent: match parent { Some(parent) => Some(Coord::new(parent.x, parent.y)), None => None, }, x: x, y: y, cost: cost, distance: distance, visited: false, } } pub fn guess_total_cost(&self) -> i32 { self.cost + self.distance } pub fn format_path(&self, search: &Search) -> Vec<Coord> { let path = &mut Vec::<Coord>::new(); path.push(Coord::new(self.x, self.y)); let mut parent = &self.parent; let mut node; while parent.is_some() { match parent { None => (), Some(p) => { node = search.get_node(&p.x, &p.y).unwrap(); path.push(Coord::new(p.x, p.y)); parent = &node.parent; }, } } path.reverse(); path.to_vec() } } impl Ord for Node { fn cmp(&self, other: &Self) -> Ordering { match self.guess_total_cost().cmp(&other.guess_total_cost()) { Ordering::Less => Ordering::Greater, Ordering::Equal => Ordering::Equal, Ordering::Greater => Ordering::Less, } } } impl PartialOrd for Node { fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(self.cmp(other)) } } impl PartialEq for Node { fn eq(&self, other: &Self) -> bool { (self.x == other.x) & (self.y == other.y) } }