11use crate :: { draw_pill, utils} ;
22use itertools:: Itertools ;
33use macroquad:: {
4- prelude:: { mouse_position, Color , IVec2 , BLACK , GREEN , MAGENTA , ORANGE , YELLOW } ,
4+ prelude:: { mouse_position, Color , IVec2 , BLACK , GREEN , MAGENTA , ORANGE , WHITE , YELLOW } ,
55 shapes:: { draw_circle, draw_circle_lines, draw_line, draw_triangle} ,
66 text:: { draw_text, get_text_center, measure_text} ,
77} ;
8+ use rand:: Rng ;
89use std:: {
910 collections:: { BTreeMap , HashMap } ,
1011 fmt:: Display ,
@@ -266,14 +267,9 @@ impl Graph {
266267 break ;
267268 }
268269
269- let current_node_id = untested_nodes
270- . first ( )
271- . unwrap ( ) ;
272- let current_node_copy = self
273- . points
274- . get_mut ( current_node_id)
275- . unwrap ( )
276- . clone ( ) ;
270+ let Some ( current_node_id) = untested_nodes. first ( ) else { return ; } ;
271+ let Some ( current_node) = self . points . get_mut ( current_node_id) else { return ; } ;
272+ let current_node_distance = current_node. distance ;
277273
278274 // Set the current node to visited
279275 self
@@ -282,6 +278,11 @@ impl Graph {
282278 . unwrap ( )
283279 . visited = true ;
284280
281+ // Skip testing the neighbours if the node is the end
282+ if * current_node_id == end {
283+ continue ;
284+ }
285+
285286 let mut new_untested_nodes = Vec :: < u8 > :: new ( ) ;
286287
287288 // Test the neighbours of the current node
@@ -294,16 +295,18 @@ impl Graph {
294295 continue ;
295296 }
296297
297- let neighbour = self
298- . points
299- . get_mut ( & line. to )
300- . unwrap ( ) ;
298+ let Some ( neighbour) = self . points . get_mut ( & line. to ) else { continue ; } ;
301299
302300 new_untested_nodes. push ( line. to ) ;
303301
304- if current_node_copy . distance + ( * line_length as u32 ) < neighbour. distance {
302+ if current_node_distance + ( * line_length as u32 ) < neighbour. distance {
305303 neighbour. parent = * current_node_id;
306- neighbour. distance = current_node_copy. distance + ( * line_length as u32 ) ;
304+ neighbour. distance = current_node_distance + ( * line_length as u32 ) ;
305+ } else if current_node_distance + ( * line_length as u32 ) == neighbour. distance {
306+ if rand:: thread_rng ( ) . gen :: < bool > ( ) {
307+ neighbour. parent = * current_node_id;
308+ neighbour. distance = current_node_distance + ( * line_length as u32 ) ;
309+ }
307310 }
308311 }
309312
@@ -385,7 +388,7 @@ impl Graph {
385388 . position
386389 . y as f32 ,
387390 2.0 ,
388- GREEN ,
391+ WHITE ,
389392 ) ;
390393 }
391394 }
@@ -852,15 +855,15 @@ impl Graph {
852855 . points
853856 . iter ( )
854857 . for_each ( |point| {
855- println ! ( "{}: {:?}" , point. 0 , point. 1 ) ;
858+ println ! ( "{} => {:?}" , point. 0 , point. 1 ) ;
856859 } ) ;
857860
858861 println ! ( "Lines:" ) ;
859862 self
860863 . lines
861864 . iter ( )
862865 . for_each ( |line| {
863- println ! ( "{}: {}" , line. 0 , line. 1 ) ;
866+ println ! ( "{} => {}" , line. 0 , line. 1 ) ;
864867 } ) ;
865868
866869 match self . start {
0 commit comments