-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandom_100.rs
94 lines (85 loc) · 2.81 KB
/
random_100.rs
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use iridescent::{Styled, GREEN, RED};
use rand::{thread_rng, Rng};
use seastar::{astar, Grid, Point};
fn setup(w: usize, h: usize) -> (Grid, Point, Point) {
let mut grid = Grid::new(w, h);
let mut rng = thread_rng();
let start = Point {
x: rng.gen_range(0..w as isize),
y: rng.gen_range(0..h as isize),
};
let end = Point {
x: rng.gen_range(0..w as isize),
y: rng.gen_range(0..h as isize),
};
for y in 0..h {
for x in 0..w {
if rng.gen_bool(0.2) {
if x == start.x as usize && y == start.y as usize
|| x == end.x as usize && y == end.y as usize
{
continue; // Leave as None
} else {
*grid.get_mut(x as isize, y as isize).unwrap() = true;
}
}
}
}
(grid, start, end)
}
fn draw_grid(grid: &Grid, path: Option<&Vec<Point>>) {
if let Some(path) = path {
for y in 0..grid.height() {
for x in 0..grid.width() {
let point = Point::new(x as isize, y as isize);
if path.contains(&point) {
if x == path[0].x as usize && y == path[0].y as usize {
print!("{}", "S".foreground(RED));
} else if x == path[path.len() - 1].x as usize
&& y == path[path.len() - 1].y as usize
{
print!("{}", "E".foreground(RED));
} else {
print!("{}", "o".foreground(GREEN));
}
} else if grid.get(x as isize, y as isize).unwrap() {
print!("#");
} else {
print!("{}", ".".dim());
}
}
println!();
}
} else {
for y in 0..grid.height() {
for x in 0..grid.width() {
if grid.get(x as isize, y as isize).unwrap() {
print!("#");
} else if x == 0 && y == 0 {
print!("{}", "S".foreground(RED));
} else if x == grid.width() - 1 && y == grid.height() - 1 {
print!("{}", "E".foreground(RED));
} else {
print!("{}", ".".dim());
}
}
println!();
}
}
}
fn main() {
let (grid, start, end) = setup(100, 100);
let now = std::time::Instant::now();
if let Some(path) = astar(&grid, start, end) {
let elapsed = now.elapsed();
draw_grid(&grid, Some(&path));
println!(
"Estimated Duration: {:?} | Path length: {}",
elapsed,
path.len()
);
} else {
draw_grid(&grid, None);
println!("No path found!");
}
}