demo-memory.rs

1.2 kB · rust · 39 lines

1use mrlycore::errors::Result;2use mrlyfig::{ink, save, Board};3use mrlynum::memory::{cells, counts, Rule};45const WIDTH: usize = 3;6const CODE: u64 = 23;7const LEVELS: usize = 9;89fn main() -> Result<()> {10    let rule = Rule::new(1, WIDTH, CODE)?;11    let tally = counts(&rule, LEVELS);12    assert_eq!(tally[..8], [2, 4, 4, 6, 9, 13, 19, 28]);1314    let mut board = Board::square();15    let area = board.frame(0.08);16    let band = area.h / LEVELS as f64;17    let gap = (band * 0.16).max(2.0).round();1819    for depth in 1..=LEVELS {20        let word = cells(&rule, depth);21        assert_eq!(word.len() as u64, tally[depth - 1]);22        let span = area.w / (1u64 << depth) as f64;23        let y = (area.y + (depth - 1) as f64 * band).round();24        let h = (band - gap).round().max(1.0);25        let paint = if depth == LEVELS {26            ink::orange()27        } else {28            ink::yellow()29        };30        for seat in word {31            let x = (area.x + seat as f64 * span).round();32            let w = (area.x + (seat + 1) as f64 * span).round() - x;33            board.rect(x, y, w.max(1.0), h, paint);34        }35    }3637    save("demo-memory", &board)?;38    Ok(())39}