Skip to content

Commit 8901110

Browse files
committedSep 29, 2020
Revert some styling changes
1 parent f7fd462 commit 8901110

File tree

3 files changed

+116
-85
lines changed

3 files changed

+116
-85
lines changed
 

‎rustfmt.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
hard_tabs = true
2+
max_width = 120

‎src/main.rs

+85-58
Original file line numberDiff line numberDiff line change
@@ -9,70 +9,80 @@ use std::str::FromStr;
99
use std::{thread, time::Duration};
1010

1111
mod map;
12-
use map::Map;
1312
use map::Direction;
13+
use map::Map;
1414

1515
fn main() {
1616
let matches = App::new("Maze Generator")
17-
.arg(Arg::with_name("ROWS")
18-
.long("rows")
19-
.default_value("5")
20-
.validator(check_arg_is_number)
21-
.help("Number of rows of the generated map")
22-
.display_order(0),
17+
.arg(
18+
Arg::with_name("ROWS")
19+
.long("rows")
20+
.default_value("5")
21+
.validator(check_arg_is_number)
22+
.help("Number of rows of the generated map")
23+
.display_order(0),
2324
)
24-
.arg(Arg::with_name("COLUMNS")
25-
.long("columns")
26-
.default_value("5")
27-
.validator(check_arg_is_number)
28-
.help("Number of columns of the generated map")
29-
.display_order(1),
25+
.arg(
26+
Arg::with_name("COLUMNS")
27+
.long("columns")
28+
.default_value("5")
29+
.validator(check_arg_is_number)
30+
.help("Number of columns of the generated map")
31+
.display_order(1),
3032
)
31-
.arg(Arg::with_name("START_ROW")
32-
.long("start_row")
33-
.default_value("0")
34-
.validator(check_arg_is_number)
35-
.help("The row to start generating from")
36-
.display_order(2),
33+
.arg(
34+
Arg::with_name("START_ROW")
35+
.long("start_row")
36+
.default_value("0")
37+
.validator(check_arg_is_number)
38+
.help("The row to start generating from")
39+
.display_order(2),
3740
)
38-
.arg(Arg::with_name("START_COLUMN")
39-
.long("start_column")
40-
.default_value("0")
41-
.validator(check_arg_is_number)
42-
.help("The column to start generating from")
43-
.display_order(3),
41+
.arg(
42+
Arg::with_name("START_COLUMN")
43+
.long("start_column")
44+
.default_value("0")
45+
.validator(check_arg_is_number)
46+
.help("The column to start generating from")
47+
.display_order(3),
4448
)
45-
.arg(Arg::with_name("DELAY")
46-
.long("delay")
47-
.default_value("50")
48-
.validator(check_arg_is_number)
49-
.help("The ms delay between steps")
50-
.display_order(4),
49+
.arg(
50+
Arg::with_name("DELAY")
51+
.long("delay")
52+
.default_value("50")
53+
.validator(check_arg_is_number)
54+
.help("The ms delay between steps")
55+
.display_order(4),
5156
)
52-
.arg(Arg::with_name("DFS")
53-
.long("dfs")
54-
.help("Use the depth first search algorithm for maze generation [default]")
55-
.display_order(5)
57+
.arg(
58+
Arg::with_name("DFS")
59+
.long("dfs")
60+
.help("Use the depth first search algorithm for maze generation [default]")
61+
.display_order(5),
5662
)
57-
.arg(Arg::with_name("TREE")
58-
.long("tree")
59-
.help("Use the binary tree maze algorithm for maze generation")
60-
.display_order(6)
63+
.arg(
64+
Arg::with_name("TREE")
65+
.long("tree")
66+
.help("Use the binary tree maze algorithm for maze generation")
67+
.display_order(6),
6168
)
62-
.arg(Arg::with_name("PRIM")
63-
.long("prim")
64-
.help("Use Prim's algorithm for maze generation")
65-
.display_order(7)
69+
.arg(
70+
Arg::with_name("PRIM")
71+
.long("prim")
72+
.help("Use Prim's algorithm for maze generation")
73+
.display_order(7),
6674
)
67-
.arg(Arg::with_name("AB")
68-
.long("ab")
69-
.help("Use the Aldous-Broder algorithm for maze generation")
70-
.display_order(8)
75+
.arg(
76+
Arg::with_name("AB")
77+
.long("ab")
78+
.help("Use the Aldous-Broder algorithm for maze generation")
79+
.display_order(8),
7180
)
72-
.arg(Arg::with_name("DIV")
73-
.long("div")
74-
.help("Use the recursive division method for maze generation")
75-
.display_order(9)
81+
.arg(
82+
Arg::with_name("DIV")
83+
.long("div")
84+
.help("Use the recursive division method for maze generation")
85+
.display_order(9),
7686
)
7787
.group(ArgGroup::with_name("ALGORITHM").args(&[
7888
"DFS",
@@ -152,9 +162,7 @@ fn main() {
152162
.expect("Could not move cursor.");
153163
}
154164
}
155-
stdout
156-
.flush()
157-
.expect("Could not flush.");
165+
stdout.flush().expect("Could not flush.");
158166

159167
if delay > 0 {
160168
thread::sleep(Duration::from_millis(delay));
@@ -163,13 +171,31 @@ fn main() {
163171
let map = if matches.is_present("TREE") {
164172
Map::generate_three(rows, columns, initial_peek_fn, peek_fn)
165173
} else if matches.is_present("PRIM") {
166-
Map::generate_prim(rows, columns, (start_row, start_column), initial_peek_fn, peek_fn)
174+
Map::generate_prim(
175+
rows,
176+
columns,
177+
(start_row, start_column),
178+
initial_peek_fn,
179+
peek_fn,
180+
)
167181
} else if matches.is_present("AB") {
168-
Map::generate_ab(rows, columns, (start_row, start_column), initial_peek_fn, peek_fn)
182+
Map::generate_ab(
183+
rows,
184+
columns,
185+
(start_row, start_column),
186+
initial_peek_fn,
187+
peek_fn,
188+
)
169189
} else if matches.is_present("DIV") {
170190
Map::generate_div(rows, columns, initial_peek_fn, peek_fn)
171191
} else {
172-
Map::generate_dfs(rows, columns, (start_row, start_column), initial_peek_fn, peek_fn)
192+
Map::generate_dfs(
193+
rows,
194+
columns,
195+
(start_row, start_column),
196+
initial_peek_fn,
197+
peek_fn,
198+
)
173199
};
174200
stdout
175201
.execute(cursor::Show)
@@ -182,7 +208,8 @@ fn main() {
182208
.map(|d| format!("{}", d))
183209
.collect::<String>()
184210
);
185-
} else {
211+
}
212+
else {
186213
println!("No path through maze");
187214
}
188215
}

‎src/map.rs

+29-27
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@ const DOWN: usize = 0b0001;
1616
pub struct WallJunction(usize);
1717

1818
impl WallJunction {
19-
pub fn new() -> WallJunction {
20-
WallJunction(0)
21-
}
22-
2319
fn set(&mut self, bit: usize, activate: bool) {
2420
if activate {
2521
self.0 |= bit;
26-
} else {
22+
}
23+
else {
2724
self.0 &= !bit;
2825
}
2926
}
@@ -57,6 +54,12 @@ impl WallJunction {
5754
}
5855
}
5956

57+
impl Default for WallJunction {
58+
fn default() -> Self {
59+
WallJunction(0)
60+
}
61+
}
62+
6063
impl From<WallJunction> for char {
6164
fn from(wj: WallJunction) -> Self {
6265
match wj.0 {
@@ -82,7 +85,7 @@ impl From<WallJunction> for char {
8285

8386
impl fmt::Display for WallJunction {
8487
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85-
write!(f, "{}", char::from(self.clone()))
88+
write!(f, "{}", char::from(*self))
8689
}
8790
}
8891

@@ -136,7 +139,8 @@ impl Map {
136139
for r in 0..(rows * 2 - 1) {
137140
if r % 2 == 0 {
138141
map.push(vec![value; columns - 1]);
139-
} else {
142+
}
143+
else {
140144
map.push(vec![value; columns]);
141145
}
142146
}
@@ -173,14 +177,15 @@ impl Map {
173177
let moved_positions = DIRECTIONS
174178
.iter()
175179
.filter_map(|d| map.move_in_direction(&next, d).map(|m| (m, d)))
176-
.filter(|(m, _)| !visited.contains(&m)).collect::<Vec<((usize, usize), &Direction)>>();
180+
.filter(|(m, _)| !visited.contains(&m))
181+
.collect::<Vec<((usize, usize), &Direction)>>();
177182
if moved_positions.len() > 1 {
178183
to_visit.push(next);
179184
}
180185
if let Some((moved, dir)) = moved_positions.choose(&mut rng) {
181186
map.set(next.0, next.1, dir, false);
182-
to_visit.push(moved.clone());
183-
visited.insert(moved.clone());
187+
to_visit.push(*moved);
188+
visited.insert(*moved);
184189
peek(&map, &next, &dir);
185190
}
186191
}
@@ -247,7 +252,7 @@ impl Map {
247252
visited.insert(start);
248253
let mut walls = map.walls_around(&start);
249254

250-
while walls.len() > 0 {
255+
while !walls.is_empty() {
251256
walls.shuffle(&mut rng);
252257
let (from, dir) = walls.pop().unwrap();
253258
if let Some(to) = map.move_in_direction(&from, &dir) {
@@ -284,17 +289,18 @@ impl Map {
284289
visited.insert(start);
285290
let mut has_neighbors = vec![start];
286291

287-
while has_neighbors.len() > 0 {
292+
while !has_neighbors.is_empty() {
288293
let index = rng.gen_range(0, has_neighbors.len());
289294
let next = has_neighbors[index];
290295
let moved_positions = DIRECTIONS
291296
.iter()
292297
.filter_map(|d| map.move_in_direction(&next, d).map(|m| (m, d)))
293-
.filter(|(m, _)| !visited.contains(&m)).collect::<Vec<((usize, usize), &Direction)>>();
298+
.filter(|(m, _)| !visited.contains(&m))
299+
.collect::<Vec<((usize, usize), &Direction)>>();
294300
if moved_positions.len() <= 1 {
295301
has_neighbors.remove(index);
296302
}
297-
if moved_positions.len() > 0 {
303+
if !moved_positions.is_empty() {
298304
let moved = moved_positions.choose(&mut rng).unwrap();
299305
map.set(next.0, next.1, moved.1, false);
300306
peek(&map, &next, moved.1);
@@ -306,12 +312,7 @@ impl Map {
306312
map
307313
}
308314

309-
pub fn generate_div<F, G>(
310-
rows: usize,
311-
columns: usize,
312-
mut initial_peek: F,
313-
mut peek: G,
314-
) -> Map
315+
pub fn generate_div<F, G>(rows: usize, columns: usize, mut initial_peek: F, mut peek: G) -> Map
315316
where
316317
F: FnMut(&Map),
317318
G: FnMut(&Map, &(usize, usize), &Direction),
@@ -468,7 +469,7 @@ impl Map {
468469
.iter()
469470
.filter_map(|dir| {
470471
if self.is(pos.0, pos.1, dir) == Some(true) {
471-
return Some((pos.clone(), dir.clone()));
472+
return Some((*pos, *dir));
472473
}
473474
None
474475
})
@@ -517,8 +518,8 @@ impl Map {
517518

518519
pub fn get_chars(&self, pos: &(usize, usize), dir: &Direction) -> (char, char) {
519520
if dir == &Direction::Left || dir == &Direction::Right {
520-
let mut above = WallJunction::new();
521-
let mut below = WallJunction::new();
521+
let mut above = WallJunction::default();
522+
let mut below = WallJunction::default();
522523

523524
if self.is(pos.0, pos.1, dir).unwrap_or(true) {
524525
above.set_down(true);
@@ -568,8 +569,8 @@ impl Map {
568569
(char::from(above), char::from(below))
569570
}
570571
else {
571-
let mut left = WallJunction::new();
572-
let mut right = WallJunction::new();
572+
let mut left = WallJunction::default();
573+
let mut right = WallJunction::default();
573574

574575
if self.is(pos.0, pos.1, dir).unwrap_or(true) {
575576
left.set_right(true);
@@ -640,7 +641,8 @@ fn build_path(
640641
},
641642
);
642643
part
643-
} else {
644+
}
645+
else {
644646
Vec::new()
645647
}
646648
}
@@ -656,7 +658,7 @@ impl fmt::Display for Map {
656658

657659
for r in 0..(self.rows - 1) {
658660
above = below;
659-
below = vec![WallJunction::new(); self.columns + 1];
661+
below = vec![WallJunction::default(); self.columns + 1];
660662
below[0] = VERTICAL;
661663
below[self.columns] = VERTICAL;
662664

0 commit comments

Comments
 (0)
Please sign in to comment.