1+ use log:: info;
12use snk_grid:: {
23 direction:: { Direction , iter_directions} ,
34 point:: { Point , get_distance} ,
@@ -9,23 +10,20 @@ use std::collections::{BinaryHeap, HashSet};
910struct Node {
1011 pub snake : Snake4 ,
1112 pub weight : u32 ,
12- pub heuristic : u32 ,
13- pub parent : Option < ( Box < Node > , Direction ) > ,
14- }
15- impl Node {
16- fn f ( & self ) -> u32 {
17- self . weight + self . heuristic
18- }
13+ pub distance : u32 ,
14+ pub f : u32 ,
15+ pub path : Vec < Direction > ,
1916}
17+
2018impl Eq for Node { }
2119impl PartialEq for Node {
2220 fn eq ( & self , other : & Self ) -> bool {
23- self . parent == other. parent
21+ self . path == other. path
2422 }
2523}
2624impl Ord for Node {
2725 fn cmp ( & self , other : & Self ) -> std:: cmp:: Ordering {
28- self . f ( ) . cmp ( & other. f ( ) ) . reverse ( )
26+ self . f . cmp ( & other. f )
2927 }
3028}
3129impl PartialOrd for Node {
@@ -36,7 +34,7 @@ impl PartialOrd for Node {
3634
3735pub fn get_snake_path < F > (
3836 get_walk_cost : F ,
39- from : Snake4 ,
37+ from : & Snake4 ,
4038 to : Point ,
4139 max_weight : u32 ,
4240) -> Option < Vec < Direction > >
@@ -47,21 +45,61 @@ where
4745 let mut close_list: HashSet < Snake4 > = HashSet :: new ( ) ;
4846
4947 open_list. push ( Node {
50- heuristic : get_distance ( from. get_head ( ) , to) as u32 ,
51- snake : from,
48+ distance : 0 ,
49+ snake : from. clone ( ) ,
50+ weight : 0 ,
51+ f : 0 ,
52+ path : Vec :: new ( ) ,
53+ } ) ;
54+
55+ open_list. push ( Node {
56+ distance : 0 ,
57+ snake : from. clone ( ) ,
5258 weight : 0 ,
53- parent : None ,
59+ f : 3 ,
60+ path : Vec :: new ( ) ,
5461 } ) ;
5562
63+ open_list. push ( Node {
64+ distance : 0 ,
65+ snake : from. clone ( ) ,
66+ weight : 0 ,
67+ f : 1 ,
68+ path : Vec :: new ( ) ,
69+ } ) ;
70+ open_list. push ( Node {
71+ distance : 0 ,
72+ snake : from. clone ( ) ,
73+ weight : 0 ,
74+ f : 5 ,
75+ path : Vec :: new ( ) ,
76+ } ) ;
77+
78+ log:: info!(
79+ "open_list {:?}" ,
80+ open_list
81+ . clone( )
82+ . into_iter( )
83+ . map( |n| n. f)
84+ . collect:: <Vec <_>>( )
85+ ) ;
86+
87+ let mut loop_count = 0 ;
88+
5689 while let Some ( node) = open_list. pop ( ) {
57- // println!(
58- // "{:?}",
59- // open_list
60- // .clone()
61- // .into_iter()
62- // .map(|n| n.weight)
63- // .collect::<Vec<_>>()
64- // );
90+ loop_count += 1 ;
91+
92+ log:: info!(
93+ "loop {} node {} {:?} open_list {:?}" ,
94+ loop_count,
95+ node. f,
96+ node. snake. get_head( ) ,
97+ open_list
98+ . clone( )
99+ . into_iter( )
100+ . map( |n| n. f)
101+ . collect:: <Vec <_>>( )
102+ ) ;
65103
66104 for dir in iter_directions ( ) {
67105 if snake_will_self_collide ( & node. snake , dir) {
@@ -78,48 +116,61 @@ where
78116
79117 if to == head {
80118 // unroll and return
81- let mut path = vec ! [ dir] ;
119+ let mut path = node. path . clone ( ) ;
120+ path. push ( dir) ;
82121
83- let mut parent = node. parent ;
84- while let Some ( ( node, dir) ) = parent {
85- path. push ( dir) ;
86- parent = node. parent ;
87- }
122+ log:: info!( " {:?} : {:?}" , to, head) ;
123+ log:: info!( "found loop {} length: {}" , loop_count, path. len( ) ) ;
88124
89125 path. reverse ( ) ;
90126 return Some ( path) ;
91127 }
92128
93129 let weight = node. weight + get_walk_cost ( head) ;
94- let heuristic = get_distance ( head, to) as u32 ;
130+ let distance = get_distance ( head, to) as u32 ;
131+ log:: info!( " {:?} {:?}" , head, distance) ;
95132
96- if weight + heuristic > max_weight {
97- continue ;
98- }
133+ let f = weight + distance;
134+ // if f > max_weight {
135+ // continue;
136+ // }
99137
138+ let mut path = node. path . clone ( ) ;
139+ path. push ( dir) ;
100140 open_list. push ( Node {
101141 snake,
102142 weight,
103- heuristic,
104- parent : Some ( ( Box :: new ( node. clone ( ) ) , dir) ) ,
143+ distance,
144+ f,
145+ path,
105146 } ) ;
106147 }
107148
108149 close_list. insert ( node. snake ) ;
150+
151+ log:: info!(
152+ "open_list {:?}" ,
153+ open_list
154+ . clone( )
155+ . into_iter( )
156+ . map( |n| n. f)
157+ . collect:: <Vec <_>>( )
158+ ) ;
109159 }
110160
111161 None
112162}
113163
114164#[ test]
165+ #[ ignore]
115166fn it_should_found_simple_path ( ) {
116167 let snake = Snake4 :: from_points ( [
117168 Point { x : 0 , y : 0 } ,
118169 Point { x : 1 , y : 0 } ,
119170 Point { x : 2 , y : 0 } ,
120171 Point { x : 3 , y : 0 } ,
121172 ] ) ;
122- let res = get_snake_path ( |_| 1 , snake, Point { x : 0 , y : 3 } , 9999 ) ;
173+ let res = get_snake_path ( |_| 1 , & snake, Point { x : 0 , y : 3 } , 9999 ) ;
123174
124175 assert_eq ! (
125176 res,
0 commit comments