~ruther/avr-guess-the-number

0741078d7a4c0e6e049dcd3b0d3e26a55084dab5 — František Boháček 2 years ago 049de19
feat: improve number generation
1 files changed, 21 insertions(+), 5 deletions(-)

M src/entrypoint.rs
M src/entrypoint.rs => src/entrypoint.rs +21 -5
@@ 281,17 281,33 @@ impl Game {
        self.current_number = None;
    }

    #[inline]
    fn generate_number(&mut self) -> [u8; 4] {
        let first = self.rng.take_u8();
        let second = self.rng.take_u8();

        let mut digits = [
            ((first >> 4) & 0xF) + 1,
            first & 0xF,
            (second >> 4) & 0xF,
            second & 0xF];

        for digit in digits.iter_mut() {
            if *digit >= 10 {
                *digit -= 10;
            }
        }

        digits
    }

    fn start_new_game(&mut self) {
        if let Some(animation) = &mut self.animation {
            animation.cleanup(&mut self.seven_segment, &mut self.led_matrix);
            self.animation = None;
        }

        let mut guessing_number = [0; DIGITS];
        for i in 0..DIGITS {
            guessing_number[i] = self.rng.take_u8() % 10;
        }

        let guessing_number = self.generate_number();
        let current_number = [0; 4];

        self.guessing_number = Some(guessing_number);

Do not follow this link