Skip to content

Commit b6370fa

Browse files
committed
Minor refactorings
1 parent 779fe54 commit b6370fa

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,4 @@ edition = "2021"
88
[dependencies]
99
itertools = "0.11.0"
1010
macroquad = { version = "0.4.2" }
11-
12-
[dev-dependencies]
1311
rand = { version = "0.8.5" }

rustfmt.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ single_line_if_else_max_width = 120
1616
tab_spaces = 2
1717
use_field_init_shorthand = true
1818
# use_small_heuristics = "Max"
19+
fn_single_line = true
20+
where_single_line = true
21+
struct_lit_single_line = true
22+
empty_item_single_line = true

src/graph.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use crate::{draw_pill, utils};
22
use itertools::Itertools;
33
use 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;
89
use 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

Comments
 (0)