• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

naomijub / bevy_knossos / 14371219842

10 Apr 2025 02:49AM UTC coverage: 89.22%. Remained the same
14371219842

Pull #9

github

web-flow
Merge e3045a87a into 5987a9153
Pull Request #9: Update bevy 016 and Edition 2024

3 of 4 new or added lines in 3 files covered. (75.0%)

32 existing lines in 9 files now uncovered.

869 of 974 relevant lines covered (89.22%)

1.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.63
/src/maze/algorithms/binary_tree.rs
1
use super::Algorithm;
2
use crate::maze::grid::cell::Cell;
3
use crate::maze::grid::Grid;
4
use crate::utils::types::Coords;
5
use clap::ValueEnum;
6
use rand::prelude::*;
7

8
/// An enumeration over supported biases for the "Binary Tree" algorithm
9
///
10
/// Each bias represents the two of four sides of the maze that will be spanned
11
/// by a single corridor.
12
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
13
pub enum Bias {
14
    /// Produces two long corridors on the Northern and Western sides of the maze
15
    NorthWest,
16

17
    /// Produces two long corridors on the Northern and Eastern sides of the maze
18
    NorthEast,
19

20
    /// Produces two long corridors on the Southern and Western sides of the maze
21
    SouthWest,
22

23
    /// Produces two long corridors on the Southern and Eastern sides of the maze
24
    SouthEast,
25
}
26

27
/// The "Binary Tree" algorithm for generating mazes
28
///
29
/// This is an almost-trivially simple one, but you pay for that simplicity with a few side effects:
30
/// a notable bias (routes tend to run diagonally) and long corridors spanning two sides. Still,
31
/// this is quite a performant algorithm since it operates without any state at all looking at the
32
/// current cell only, without regard for the rest of the cells and rows in the maze.
33
pub struct BinaryTree {
34
    bias: Bias,
35
}
36

37
impl BinaryTree {
38
    /// Create a new instance of the algorithm with a given bias
39
    ///
40
    /// # Example
41
    /// ```
42
    /// use bevy_knossos::maze::{BinaryTree, Bias};
43
    ///
44
    /// let algorithm = BinaryTree::new(Bias::NorthWest);
45
    /// ```
46
    pub const fn new(bias: Bias) -> BinaryTree {
1✔
47
        BinaryTree { bias }
48
    }
49

50
    fn populate_dirs(&self, coords: Coords, grid: &Grid) -> Vec<Cell> {
1✔
51
        let mut dirs = vec![];
1✔
52
        let (x, y) = coords;
1✔
53

54
        match self.bias {
1✔
55
            Bias::NorthWest => {
56
                if y > 0 {
1✔
57
                    dirs.push(Cell::NORTH)
2✔
58
                }
59
                if x > 0 {
1✔
60
                    dirs.push(Cell::WEST)
1✔
61
                }
62
            }
63
            Bias::NorthEast => {
64
                if y > 0 {
1✔
65
                    dirs.push(Cell::NORTH)
1✔
66
                }
67
                if x + 1 < grid.width() {
2✔
68
                    dirs.push(Cell::EAST)
1✔
69
                }
70
            }
71
            Bias::SouthWest => {
72
                if y + 1 < grid.height() {
2✔
73
                    dirs.push(Cell::SOUTH)
1✔
74
                }
75
                if x > 0 {
1✔
76
                    dirs.push(Cell::WEST)
1✔
77
                }
78
            }
79
            Bias::SouthEast => {
80
                if y + 1 < grid.height() {
2✔
81
                    dirs.push(Cell::SOUTH)
1✔
82
                }
83
                if x + 1 < grid.width() {
2✔
84
                    dirs.push(Cell::EAST)
1✔
85
                }
86
            }
87
        }
88

89
        dirs
1✔
90
    }
91
}
92

93
/// An implementation of the "Binary Tree" algorithm for generating mazes.
94
/// Does not support start coords.
95
///
96
/// The algorithm is pretty simple: for every cell in the grid, randomly carve a passage either
97
/// north, or west.
98
///  
99
/// # Warn
100
///
101
/// The `generate` function will warn in case a start_coords is passed.
102
impl Algorithm for BinaryTree {
103
    fn generate(&mut self, grid: &mut Grid, _c: Option<Coords>, rng: &mut StdRng) {
1✔
104
        if _c.is_some() {
1✔
105
            eprintln!("Algorithm `{}` doesn't suppoer `start_coords`", self.name())
×
106
        }
107
        for y in 0..grid.height() {
2✔
108
            for x in 0..grid.width() {
2✔
109
                let dirs = self.populate_dirs((x, y), grid);
1✔
110
                if let Some(dir) = dirs.choose(rng) {
2✔
111
                    grid.carve_passage((x, y), *dir).ok();
2✔
112
                }
113
            }
114
        }
115
    }
116

UNCOV
117
    fn has_start_coords(&self) -> bool {
×
118
        false
119
    }
120

UNCOV
121
    fn name(&self) -> &'static str {
×
122
        "BinaryTree"
123
    }
124
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc