amazeing 0.8.1

Amazeing is a maze generator/solver application with simulation/visualization.
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use clap::builder::Styles;
use clap::builder::styling::Color::Ansi;
use clap::builder::styling::{
    AnsiColor::{Blue, Cyan, Green, Red, Yellow},
    Style,
};
use clap::{Parser, Subcommand, ValueEnum};
use std::fmt::Display;
use std::path::PathBuf;

pub const CLAP_STYLE: Styles = Styles::styled()
    .header(Style::new().bold().fg_color(Some(Ansi(Green))))
    .usage(Style::new().bold().fg_color(Some(Ansi(Green))))
    .literal(Style::new().fg_color(Some(Ansi(Blue))).bold())
    .placeholder(Style::new().fg_color(Some(Ansi(Cyan))))
    .error(Style::new().fg_color(Some(Ansi(Red))).bold())
    .valid(Style::new().fg_color(Some(Ansi(Green))))
    .invalid(Style::new().fg_color(Some(Ansi(Yellow))));

/// A maze generator/solver application with simulation/visualization.
///
/// See https://eendroroy.github.io/amazeing for more details
#[derive(Debug, Clone, Parser)]
#[command(version, about, long_about, styles=CLAP_STYLE)]
pub struct AmazeingArgs {
    #[clap(subcommand)]
    pub command: ArgCommand,

    /// Display size (zoom)
    #[clap(global = true, long, short = 'Z', display_order = 101, default_value_t = 1f32)]
    pub zoom: f32,

    /// Color file (.toml) path
    #[clap(global = true, long, short = 'C', display_order = 102, value_name = "Colors.toml")]
    pub colors: Option<PathBuf>,

    /// Frame rate per second (controls simulation speed)
    #[clap(global = true, long, short = 'F', display_order = 103, default_value_t = 60.)]
    pub fps: f32,

    /// Draw maze bound (perimeter)
    #[clap(global = true, long, short = 'P', display_order = 103, default_value_t = false)]
    pub show_perimeter: bool,
}

/// {ui-name} amazeing create
#[derive(Debug, Clone, PartialEq, Subcommand)]
pub enum ArgCommand {
    /// Create a Maze
    #[clap(visible_alias = "C")]
    Create(CreateArgs),
    /// View a Maze
    #[clap(visible_alias = "V")]
    View(ViewArgs),
    /// Solve a Maze
    #[clap(visible_alias = "S")]
    Solve(SolveArgs),
}

#[derive(Debug, Clone, PartialEq, Parser)]
pub struct CreateArgs {
    /// Unit shape
    #[clap(global = true, long, short, default_value_t = ArgUnitShape::default(), value_name = "UnitShape")]
    pub unit_shape: ArgUnitShape,

    /// File path to dump Maze data
    ///
    /// optional if '--verbose' flag provided
    ///
    /// if provided, generated maze will be dumped at path
    #[clap(global = true, long, short)]
    pub maze: Option<PathBuf>,

    /// Number of rows
    #[clap(long, short)]
    pub rows: usize,

    /// Number of columns
    #[clap(long, short)]
    pub cols: usize,

    /// Maze Generation Procedure
    #[clap(global = true, long, short, default_value_t = ArgProcedure::Dfs)]
    pub procedure: ArgProcedure,

    /// Heuristic function (to use with AStar)
    #[clap(global = true, long, short = 'H', default_value_t = ArgHeuristic::Dijkstra, required_if_eq("procedure", "a-star"))]
    pub heuristic_function: ArgHeuristic,

    /// Weight randomization factor (to use with AStar)
    #[clap(global = true, long, short, default_value_t = 2)]
    pub jumble_factor: u32,

    /// Weight direction (ordering) (to use with AStar)
    #[clap(global = true, long, short, default_value_t = ArgWeightDirection::default())]
    pub weight_direction: ArgWeightDirection,

    /// Show a simulation of the generation process
    #[clap(global = true, long, short, default_value_t = false)]
    pub verbose: bool,
}

#[derive(Debug, Clone, PartialEq, Parser)]
pub struct ViewArgs {
    /// Maze file path
    #[clap(long, short)]
    pub maze: PathBuf,

    /// View and update
    #[clap(long, short, default_value_t = false)]
    pub update: bool,
}

#[derive(Debug, Clone, PartialEq, Parser)]
pub struct SolveArgs {
    /// Maze file path
    #[clap(long, short)]
    pub maze: PathBuf,

    /// Maze Solving Procedure
    #[clap(long, short, default_value_t = ArgProcedure::Dfs)]
    pub procedure: ArgProcedure,

    /// Heuristic function (to use with AStar)
    #[clap(long, short = 'H', default_value_t = ArgHeuristic::default(), required_if_eq("procedure", "a-star"))]
    pub heuristic_function: ArgHeuristic,

    /// Show a simulation of the solving process
    #[clap(long, short, default_value_t = false)]
    pub verbose: bool,
}

#[derive(Debug, Clone, PartialEq, ValueEnum, Default)]
pub enum ArgUnitShape {
    #[clap(alias = "t")]
    Triangle,
    #[clap(alias = "s")]
    Square,
    #[clap(alias = "r")]
    Rhombus,
    #[default]
    #[clap(alias = "h")]
    Hexagon,
    #[clap(alias = "hr")]
    HexagonRectangle,
    #[clap(alias = "o")]
    Octagon,
    #[clap(alias = "os")]
    OctagonSquare,
}

impl Display for ArgUnitShape {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ArgUnitShape::Triangle => write!(f, "triangle"),
            ArgUnitShape::Square => write!(f, "square"),
            ArgUnitShape::Rhombus => write!(f, "rhombus"),
            ArgUnitShape::Hexagon => write!(f, "hexagon"),
            ArgUnitShape::HexagonRectangle => write!(f, "hexagon-rectangle"),
            ArgUnitShape::Octagon => write!(f, "octagon"),
            ArgUnitShape::OctagonSquare => write!(f, "octagon-square"),
        }
    }
}

#[derive(Debug, Clone, PartialEq, ValueEnum, Default)]
pub enum ArgWeightDirection {
    #[clap(alias = "f")]
    Forward,
    #[clap(alias = "b")]
    #[default]
    Backward,
}

impl Display for ArgWeightDirection {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ArgWeightDirection::Forward => write!(f, "forward"),
            ArgWeightDirection::Backward => write!(f, "backward"),
        }
    }
}

#[derive(Debug, Clone, PartialEq, ValueEnum, Default)]
pub enum ArgProcedure {
    #[clap(alias = "b")]
    Bfs,
    #[default]
    #[clap(alias = "d")]
    Dfs,
    #[clap(alias = "p")]
    Prim,
    #[clap(alias = "i")]
    Iddfs,
    #[clap(alias = "gbf")]
    GreedyBestFirst,
    #[clap(alias = "bb")]
    BidirectionalBfs,
    #[clap(alias = "bs")]
    BeamSearch,
    #[clap(alias = "bgbf")]
    BidirectionalGreedyBestFirst,
    #[clap(alias = "sas")]
    SimulatedAnnealingSearch,
    #[clap(alias = "ab")]
    AldousBroder,
    #[clap(alias = "ba")]
    BidirectionalAStart,
    #[clap(alias = "a")]
    AStar,
}

impl Display for ArgProcedure {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ArgProcedure::Bfs => write!(f, "bfs"),
            ArgProcedure::Dfs => write!(f, "dfs"),
            ArgProcedure::Prim => write!(f, "prim"),
            ArgProcedure::Iddfs => write!(f, "iddfs"),
            ArgProcedure::GreedyBestFirst => write!(f, "greedy-best-first"),
            ArgProcedure::BidirectionalBfs => write!(f, "bidirectional-bfs"),
            ArgProcedure::BeamSearch => write!(f, "beam-search"),
            ArgProcedure::BidirectionalGreedyBestFirst => write!(f, "bidirectional-greedy-best-first"),
            ArgProcedure::SimulatedAnnealingSearch => write!(f, "simulated-annealing-search"),
            ArgProcedure::AldousBroder => write!(f, "aldous-broder"),
            ArgProcedure::BidirectionalAStart => write!(f, "bidirectional-a-start"),
            ArgProcedure::AStar => write!(f, "a-star"),
        }
    }
}

#[derive(Debug, Clone, PartialEq, ValueEnum, Default)]
pub enum ArgHeuristic {
    #[clap(alias = "m")]
    Manhattan,
    #[clap(alias = "e")]
    Euclidean,
    #[clap(alias = "c")]
    Chebyshev,
    #[clap(alias = "o")]
    Octile,
    #[default]
    #[clap(alias = "d")]
    Dijkstra,
}

impl Display for ArgHeuristic {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ArgHeuristic::Manhattan => write!(f, "manhattan"),
            ArgHeuristic::Euclidean => write!(f, "euclidean"),
            ArgHeuristic::Chebyshev => write!(f, "chebyshev"),
            ArgHeuristic::Octile => write!(f, "octile"),
            ArgHeuristic::Dijkstra => write!(f, "dijkstra"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use clap::Parser;
    use std::path::Path;

    #[test]
    fn parse_create_with_defaults() {
        let parsed = AmazeingArgs::try_parse_from(["amazeing", "create", "--rows", "9", "--cols", "11"])
            .expect("create args should parse");

        assert_eq!(parsed.zoom, 1.0);
        assert_eq!(parsed.fps, 60.0);
        assert!(!parsed.show_perimeter);

        match parsed.command {
            ArgCommand::Create(create) => {
                assert_eq!(create.rows, 9);
                assert_eq!(create.cols, 11);
                assert_eq!(create.procedure, ArgProcedure::Dfs);
                assert_eq!(create.heuristic_function, ArgHeuristic::Dijkstra);
                assert!(!create.verbose);
            }
            _ => panic!("expected create command"),
        }
    }

    #[test]
    fn parse_solve_with_global_flags() {
        let parsed = AmazeingArgs::try_parse_from([
            "amazeing",
            "--zoom",
            "1.5",
            "--fps",
            "24",
            "solve",
            "--maze",
            "assets/maze/001_005_005_square.maze",
            "--procedure",
            "a-star",
            "-H",
            "manhattan",
        ])
        .expect("solve args should parse");

        assert_eq!(parsed.zoom, 1.5);
        assert_eq!(parsed.fps, 24.0);

        match parsed.command {
            ArgCommand::Solve(solve) => {
                assert_eq!(solve.procedure, ArgProcedure::AStar);
                assert_eq!(solve.heuristic_function, ArgHeuristic::Manhattan);
                assert_eq!(solve.maze, Path::new("assets/maze/001_005_005_square.maze"));
            }
            _ => panic!("expected solve command"),
        }
    }

    #[test]
    fn parse_create_with_aldous_broder_procedure() {
        let parsed = AmazeingArgs::try_parse_from([
            "amazeing",
            "create",
            "--rows",
            "9",
            "--cols",
            "11",
            "--procedure",
            "aldous-broder",
        ])
        .expect("create args should parse for aldous-broder");

        match parsed.command {
            ArgCommand::Create(create) => {
                assert_eq!(create.procedure, ArgProcedure::AldousBroder);
            }
            _ => panic!("expected create command"),
        }
    }

    #[test]
    fn parse_solve_with_bidirectional_a_start_procedure() {
        let parsed = AmazeingArgs::try_parse_from([
            "amazeing",
            "solve",
            "--maze",
            "assets/maze/001_005_005_square.maze",
            "--procedure",
            "bidirectional-a-start",
        ])
        .expect("solve args should parse for bidirectional-a-start");

        match parsed.command {
            ArgCommand::Solve(solve) => {
                assert_eq!(solve.procedure, ArgProcedure::BidirectionalAStart);
            }
            _ => panic!("expected solve command"),
        }
    }

    #[test]
    fn parse_create_with_prim_procedure() {
        let parsed =
            AmazeingArgs::try_parse_from(["amazeing", "create", "--rows", "9", "--cols", "11", "--procedure", "prim"])
                .expect("create args should parse for prim");

        match parsed.command {
            ArgCommand::Create(create) => {
                assert_eq!(create.procedure, ArgProcedure::Prim);
            }
            _ => panic!("expected create command"),
        }
    }

    #[test]
    fn parse_solve_with_greedy_best_first_procedure() {
        let parsed = AmazeingArgs::try_parse_from([
            "amazeing",
            "solve",
            "--maze",
            "assets/maze/001_005_005_square.maze",
            "--procedure",
            "greedy-best-first",
        ])
        .expect("solve args should parse for greedy-best-first");

        match parsed.command {
            ArgCommand::Solve(solve) => {
                assert_eq!(solve.procedure, ArgProcedure::GreedyBestFirst);
            }
            _ => panic!("expected solve command"),
        }
    }

    #[test]
    fn parse_create_with_beam_search_procedure() {
        let parsed = AmazeingArgs::try_parse_from([
            "amazeing",
            "create",
            "--rows",
            "9",
            "--cols",
            "11",
            "--procedure",
            "beam-search",
        ])
        .expect("create args should parse for beam-search");

        match parsed.command {
            ArgCommand::Create(create) => assert_eq!(create.procedure, ArgProcedure::BeamSearch),
            _ => panic!("expected create command"),
        }
    }

    #[test]
    fn parse_solve_with_bidirectional_greedy_best_first_procedure() {
        let parsed = AmazeingArgs::try_parse_from([
            "amazeing",
            "solve",
            "--maze",
            "assets/maze/001_005_005_square.maze",
            "--procedure",
            "bidirectional-greedy-best-first",
        ])
        .expect("solve args should parse for bidirectional-greedy-best-first");

        match parsed.command {
            ArgCommand::Solve(solve) => {
                assert_eq!(solve.procedure, ArgProcedure::BidirectionalGreedyBestFirst);
            }
            _ => panic!("expected solve command"),
        }
    }
}