| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| use super::{Image, ImageSize, HtmlLog}; |
| use std::marker::PhantomData; |
|
|
| |
| |
| |
| |
| |
| pub trait CARule { |
| fn apply(center: u8, neighbors: &[u8; 8]) -> u8; |
| } |
|
|
| #[derive(Debug)] |
| pub struct CellularAutomaton<R: CARule> { |
| current: Image, |
| next: Image, |
| outside_color: Option<u8>, |
| _rule: PhantomData<R>, |
| } |
|
|
| impl<R: CARule> CellularAutomaton<R> { |
| #[allow(dead_code)] |
| pub fn new(size: ImageSize, outside_color: Option<u8>) -> Self { |
| Self { |
| current: Image::zero(size.width, size.height), |
| next: Image::zero(size.width, size.height), |
| outside_color, |
| _rule: PhantomData, |
| } |
| } |
|
|
| #[allow(dead_code)] |
| pub fn with_image(image: &Image, outside_color: Option<u8>) -> Self { |
| Self { |
| current: image.clone(), |
| next: image.clone_zero(), |
| outside_color, |
| _rule: PhantomData, |
| } |
| } |
|
|
| #[allow(dead_code)] |
| pub fn step(&mut self, count: u8) { |
| for _ in 0..count { |
| self.step_once(); |
| } |
| } |
|
|
| #[allow(dead_code)] |
| pub fn step_and_log(&mut self, count: u8) { |
| HtmlLog::image(&self.current); |
| for _ in 0..count { |
| self.step_once(); |
| HtmlLog::image(&self.current); |
| } |
| } |
|
|
| #[allow(dead_code)] |
| pub fn images_for_n_steps(&mut self, count: u8) -> Vec<Image> { |
| let mut images = Vec::<Image>::new(); |
| images.push(self.current.clone()); |
| for _ in 0..count { |
| self.step_once(); |
| images.push(self.current.clone()); |
| } |
| images |
| } |
|
|
| pub fn step_once(&mut self) { |
| for y in 0..self.current.height() { |
| for x in 0..self.current.width() { |
|
|
| |
| let mut neighbors: [u8; 8] = [0; 8]; |
| let mut index: usize = 0; |
| for i in -1..=1 { |
| for j in -1..=1 { |
| if i == 0 && j == 0 { |
| continue; |
| } |
| let value: u8 = if let Some(outside_color) = self.outside_color { |
| self.current.get(x as i32 + i, y as i32 + j).unwrap_or(outside_color) |
| } else { |
| self.current.get_wrap(x as i32 + i, y as i32 + j).unwrap_or(0) |
| }; |
| neighbors[index] = value; |
| index += 1; |
| } |
| } |
|
|
| |
| let center: u8 = self.current.get(x as i32, y as i32).unwrap_or(0); |
|
|
| |
| let set_value: u8 = R::apply(center, &neighbors); |
|
|
| _ = self.next.set(x as i32, y as i32, set_value); |
| } |
| } |
| std::mem::swap(&mut self.current, &mut self.next); |
| } |
|
|
| #[allow(dead_code)] |
| pub fn image(&self) -> &Image { |
| &self.current |
| } |
| } |
|
|
| pub mod rule { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| pub struct GameOfLife; |
|
|
| impl super::CARule for GameOfLife { |
| fn apply(center: u8, neighbors: &[u8; 8]) -> u8 { |
| let alive_count: usize = neighbors.iter().filter(|&&value| value > 0).count(); |
| match (center, alive_count) { |
| (0, 3) => 1, |
| (1, 2) => 1, |
| (1, 3) => 1, |
| _ => 0, |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| pub struct GameOfLifeExtra; |
|
|
| impl super::CARule for GameOfLifeExtra { |
| fn apply(center: u8, neighbors: &[u8; 8]) -> u8 { |
| let alive_count = neighbors.iter().filter(|&&state| state == 1 || state == 2).count(); |
|
|
| if (center == 1 || center == 2) && (alive_count < 2 || alive_count > 3) { |
| |
| |
| return 3; |
| } |
|
|
| if (center == 1 || center == 2) && (alive_count == 2 || alive_count == 3) { |
| |
| return 1; |
| } |
|
|
| if (center == 0 || center == 3) && (alive_count == 3) { |
| |
| return 2; |
| } |
|
|
| if center == 0 { |
| |
| return 0; |
| } |
|
|
| if center == 3 { |
| |
| return 0; |
| } |
|
|
| |
| 3 |
| } |
| } |
|
|
|
|
| |
| |
| |
| pub struct HighLife; |
|
|
| impl super::CARule for HighLife { |
| fn apply(center: u8, neighbors: &[u8; 8]) -> u8 { |
| let alive_count: usize = neighbors.iter().filter(|&&value| value > 0).count(); |
| match (center, alive_count) { |
| |
| (0, 3) | (0, 6) => 1, |
| |
| (1, 2) | (1, 3) => 1, |
| |
| _ => 0, |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| pub struct Wireworld; |
|
|
| impl super::CARule for Wireworld { |
| fn apply(center: u8, neighbors: &[u8; 8]) -> u8 { |
| if center == 1 { |
| return 2; |
| } |
|
|
| if center == 2 { |
| return 3; |
| } |
| |
| let electron_head_count: usize = neighbors.iter().filter(|&&value| value == 1).count(); |
| if center == 3 && (electron_head_count == 1 || electron_head_count == 2) { |
| return 1; |
| } |
|
|
| center |
| } |
| } |
|
|
| |
| |
| pub struct Serviettes; |
|
|
| impl super::CARule for Serviettes { |
| fn apply(center: u8, neighbors: &[u8; 8]) -> u8 { |
| let alive_count: usize = neighbors.iter().filter(|&&value| value > 0).count(); |
| match (center, alive_count) { |
| |
| (0, 2) | (0, 3) | (0, 4) => 1, |
| |
| _ => 0, |
| } |
| } |
| } |
|
|
| |
| |
| |
| pub struct Cave; |
|
|
| impl super::CARule for Cave { |
| fn apply(center: u8, neighbors: &[u8; 8]) -> u8 { |
| let alive_count: usize = neighbors.iter().filter(|&&value| value > 0).count(); |
| if alive_count < 4 { |
| return 0; |
| } |
| if alive_count > 5 { |
| return 1; |
| } |
| center |
| } |
| } |
|
|
| |
| |
| pub struct Maze; |
|
|
| impl super::CARule for Maze { |
| fn apply(center: u8, neighbors: &[u8; 8]) -> u8 { |
| let alive_count: usize = neighbors.iter().filter(|&&value| value > 0).count(); |
| if alive_count == 3 { |
| return 1; |
| } |
| if alive_count < 1 || alive_count > 5 { |
| return 0; |
| } |
| center |
| } |
| } |
| } |
|
|
| #[cfg(test)] |
| mod tests { |
| use super::*; |
| use crate::arc::ImageTryCreate; |
|
|
| #[test] |
| fn test_10000_gameoflife_glider() { |
| |
| let pixels: Vec<u8> = vec![ |
| 0, 0, 0, 0, 0, 0, |
| 0, 0, 0, 1, 0, 0, |
| 0, 1, 0, 1, 0, 0, |
| 0, 0, 1, 1, 0, 0, |
| 0, 0, 0, 0, 0, 0, |
| 0, 0, 0, 0, 0, 0, |
| ]; |
| let input: Image = Image::try_create(6, 6, pixels).expect("image"); |
| let mut ca: CellularAutomaton<_> = CellularAutomaton::<rule::GameOfLife>::with_image(&input, None); |
|
|
| |
| ca.step_once(); |
| let actual: Image = ca.current; |
| |
| |
| let expected_pixels: Vec<u8> = vec![ |
| 0, 0, 0, 0, 0, 0, |
| 0, 0, 1, 0, 0, 0, |
| 0, 0, 0, 1, 1, 0, |
| 0, 0, 1, 1, 0, 0, |
| 0, 0, 0, 0, 0, 0, |
| 0, 0, 0, 0, 0, 0, |
| ]; |
| let expected: Image = Image::try_create(6, 6, expected_pixels).expect("image"); |
| assert_eq!(actual, expected); |
| } |
|
|
| #[test] |
| fn test_10001_gameoflife_glider_wraparound() { |
| |
| let pixels: Vec<u8> = vec![ |
| 0, 1, 0, 0, 0, |
| 1, 1, 0, 0, 1, |
| 0, 0, 0, 0, 0, |
| 0, 0, 0, 0, 0, |
| 1, 0, 0, 0, 0, |
| ]; |
| let input: Image = Image::try_create(5, 5, pixels).expect("image"); |
| let mut ca: CellularAutomaton<_> = CellularAutomaton::<rule::GameOfLife>::with_image(&input, None); |
|
|
| |
| ca.step_once(); |
| let actual: Image = ca.current; |
| |
| |
| let expected_pixels: Vec<u8> = vec![ |
| 0, 1, 0, 0, 1, |
| 1, 1, 0, 0, 0, |
| 1, 0, 0, 0, 0, |
| 0, 0, 0, 0, 0, |
| 0, 0, 0, 0, 0, |
| ]; |
| let expected: Image = Image::try_create(5, 5, expected_pixels).expect("image"); |
| assert_eq!(actual, expected); |
| } |
|
|
| #[test] |
| fn test_10002_gameoflife_glider_use_outside_color() { |
| |
| let pixels: Vec<u8> = vec![ |
| 0, 1, 0, 0, 0, |
| 1, 1, 0, 0, 1, |
| 0, 0, 0, 0, 0, |
| 0, 0, 0, 0, 0, |
| 1, 0, 0, 0, 0, |
| ]; |
| let input: Image = Image::try_create(5, 5, pixels).expect("image"); |
| let mut ca: CellularAutomaton<_> = CellularAutomaton::<rule::GameOfLife>::with_image(&input, Some(0)); |
|
|
| |
| ca.step_once(); |
| let actual: Image = ca.current; |
| |
| |
| let expected_pixels: Vec<u8> = vec![ |
| 1, 1, 0, 0, 0, |
| 1, 1, 0, 0, 0, |
| 0, 0, 0, 0, 0, |
| 0, 0, 0, 0, 0, |
| 0, 0, 0, 0, 0, |
| ]; |
| let expected: Image = Image::try_create(5, 5, expected_pixels).expect("image"); |
| assert_eq!(actual, expected); |
| } |
|
|
| #[test] |
| fn test_20000_gameoflife_extra_glider() { |
| |
| let pixels: Vec<u8> = vec![ |
| 0, 0, 0, 0, 0, 0, |
| 0, 0, 0, 1, 0, 0, |
| 0, 1, 0, 1, 0, 0, |
| 0, 0, 1, 1, 0, 0, |
| 0, 0, 0, 0, 0, 0, |
| 0, 0, 0, 0, 0, 0, |
| ]; |
| let input: Image = Image::try_create(6, 6, pixels).expect("image"); |
| let mut ca: CellularAutomaton<_> = CellularAutomaton::<rule::GameOfLifeExtra>::with_image(&input, None); |
|
|
| |
| ca.step_once(); |
| let actual: Image = ca.current; |
| |
| |
| let expected_pixels: Vec<u8> = vec![ |
| 0, 0, 0, 0, 0, 0, |
| 0, 0, 2, 3, 0, 0, |
| 0, 3, 0, 1, 2, 0, |
| 0, 0, 1, 1, 0, 0, |
| 0, 0, 0, 0, 0, 0, |
| 0, 0, 0, 0, 0, 0, |
| ]; |
| let expected: Image = Image::try_create(6, 6, expected_pixels).expect("image"); |
| assert_eq!(actual, expected); |
| } |
|
|
| #[test] |
| fn test_20001_gameoflife_extra_glider() { |
| |
| let pixels: Vec<u8> = vec![ |
| 0, 0, 0, 0, 0, 0, |
| 0, 0, 2, 3, 0, 0, |
| 0, 3, 0, 1, 2, 0, |
| 0, 0, 1, 1, 0, 0, |
| 0, 0, 0, 0, 0, 0, |
| 0, 0, 0, 0, 0, 0, |
| ]; |
| let input: Image = Image::try_create(6, 6, pixels).expect("image"); |
| let mut ca: CellularAutomaton<_> = CellularAutomaton::<rule::GameOfLifeExtra>::with_image(&input, None); |
|
|
| |
| ca.step_once(); |
| let actual: Image = ca.current; |
| |
| |
| let expected_pixels: Vec<u8> = vec![ |
| 0, 0, 0, 0, 0, 0, |
| 0, 0, 3, 2, 0, 0, |
| 0, 0, 0, 3, 1, 0, |
| 0, 0, 1, 1, 2, 0, |
| 0, 0, 0, 0, 0, 0, |
| 0, 0, 0, 0, 0, 0, |
| ]; |
| let expected: Image = Image::try_create(6, 6, expected_pixels).expect("image"); |
| assert_eq!(actual, expected); |
| } |
|
|
| #[test] |
| fn test_30000_highlife_predecessor_replicator() { |
| |
| let pixels: Vec<u8> = vec![ |
| 0, 0, 0, 0, 0, 0, |
| 0, 0, 1, 1, 1, 0, |
| 0, 1, 0, 0, 0, 0, |
| 0, 1, 0, 0, 0, 0, |
| 0, 1, 0, 0, 0, 0, |
| 0, 0, 0, 0, 0, 0, |
| ]; |
| let input: Image = Image::try_create(6, 6, pixels).expect("image"); |
| let mut ca: CellularAutomaton<_> = CellularAutomaton::<rule::HighLife>::with_image(&input, None); |
|
|
| |
| ca.step_once(); |
| let actual: Image = ca.current; |
| |
| |
| let expected_pixels: Vec<u8> = vec![ |
| 0, 0, 0, 1, 0, 0, |
| 0, 0, 1, 1, 0, 0, |
| 0, 1, 0, 1, 0, 0, |
| 1, 1, 1, 0, 0, 0, |
| 0, 0, 0, 0, 0, 0, |
| 0, 0, 0, 0, 0, 0, |
| ]; |
| let expected: Image = Image::try_create(6, 6, expected_pixels).expect("image"); |
| assert_eq!(actual, expected); |
| } |
|
|
| #[test] |
| fn test_40000_wireworld_diode() { |
| |
| let pixels: Vec<u8> = vec![ |
| 0, 0, 0, 0, 0, 0, 0, 0, |
| 0, 0, 0, 3, 3, 0, 0, 0, |
| 3, 2, 1, 0, 3, 3, 3, 3, |
| 0, 0, 0, 3, 3, 0, 0, 0, |
| 0, 0, 0, 0, 0, 0, 0, 0, |
| ]; |
| let input: Image = Image::try_create(8, 5, pixels).expect("image"); |
| let mut ca: CellularAutomaton<_> = CellularAutomaton::<rule::Wireworld>::with_image(&input, None); |
|
|
| |
| ca.step_once(); |
| let actual: Image = ca.current; |
| |
| |
| let expected_pixels: Vec<u8> = vec![ |
| 0, 0, 0, 0, 0, 0, 0, 0, |
| 0, 0, 0, 1, 3, 0, 0, 0, |
| 3, 3, 2, 0, 3, 3, 3, 3, |
| 0, 0, 0, 1, 3, 0, 0, 0, |
| 0, 0, 0, 0, 0, 0, 0, 0, |
| ]; |
| let expected: Image = Image::try_create(8, 5, expected_pixels).expect("image"); |
| assert_eq!(actual, expected); |
| } |
|
|
| #[test] |
| fn test_40001_wireworld_diode() { |
| |
| let pixels: Vec<u8> = vec![ |
| 0, 0, 0, 0, 0, 0, 0, 0, |
| 0, 0, 0, 3, 3, 0, 0, 0, |
| 3, 2, 1, 3, 0, 3, 3, 3, |
| 0, 0, 0, 3, 3, 0, 0, 0, |
| 0, 0, 0, 0, 0, 0, 0, 0, |
| ]; |
| let input: Image = Image::try_create(8, 5, pixels).expect("image"); |
| let mut ca: CellularAutomaton<_> = CellularAutomaton::<rule::Wireworld>::with_image(&input, None); |
|
|
| |
| ca.step_once(); |
| let actual: Image = ca.current; |
| |
| |
| let expected_pixels: Vec<u8> = vec![ |
| 0, 0, 0, 0, 0, 0, 0, 0, |
| 0, 0, 0, 1, 3, 0, 0, 0, |
| 3, 3, 2, 1, 0, 3, 3, 3, |
| 0, 0, 0, 1, 3, 0, 0, 0, |
| 0, 0, 0, 0, 0, 0, 0, 0, |
| ]; |
| let expected: Image = Image::try_create(8, 5, expected_pixels).expect("image"); |
| assert_eq!(actual, expected); |
| } |
|
|
| #[test] |
| fn test_50000_serviettes() { |
| |
| let pixels: Vec<u8> = vec![ |
| 0, 0, 0, 0, 0, 0, |
| 0, 0, 0, 0, 0, 0, |
| 0, 0, 1, 1, 0, 0, |
| 0, 0, 1, 1, 0, 0, |
| 0, 0, 0, 0, 0, 0, |
| 0, 0, 0, 0, 0, 0, |
| ]; |
| let input: Image = Image::try_create(6, 6, pixels).expect("image"); |
| let mut ca: CellularAutomaton<_> = CellularAutomaton::<rule::Serviettes>::with_image(&input, None); |
|
|
| |
| ca.step_once(); |
| let actual: Image = ca.current; |
| |
| |
| let expected_pixels: Vec<u8> = vec![ |
| 0, 0, 0, 0, 0, 0, |
| 0, 0, 1, 1, 0, 0, |
| 0, 1, 0, 0, 1, 0, |
| 0, 1, 0, 0, 1, 0, |
| 0, 0, 1, 1, 0, 0, |
| 0, 0, 0, 0, 0, 0, |
| ]; |
| let expected: Image = Image::try_create(6, 6, expected_pixels).expect("image"); |
| assert_eq!(actual, expected); |
| } |
|
|
| #[test] |
| fn test_60000_cave() { |
| |
| let pixels: Vec<u8> = vec![ |
| 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, |
| 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, |
| 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, |
| 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, |
| 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, |
| 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, |
| 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, |
| 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, |
| 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, |
| 1, 1, 0, 1, 0, 0, 0, 0, 0, 0 |
| ]; |
| let input: Image = Image::try_create(10, 10, pixels).expect("image"); |
| let mut ca: CellularAutomaton<_> = CellularAutomaton::<rule::Cave>::with_image(&input, Some(0)); |
|
|
| |
| ca.step_once(); |
| let actual: Image = ca.current; |
| |
| |
| let expected_pixels: Vec<u8> = vec![ |
| 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, |
| 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, |
| 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, |
| 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, |
| 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, |
| 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, |
| 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, |
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, |
| 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 |
| ]; |
| let expected: Image = Image::try_create(10, 10, expected_pixels).expect("image"); |
| assert_eq!(actual, expected); |
| } |
|
|
| #[test] |
| fn test_70000_maze() { |
| |
| let pixels: Vec<u8> = vec![ |
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, |
| 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, |
| 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, |
| 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, |
| 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, |
| 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, |
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| ]; |
| let input: Image = Image::try_create(10, 10, pixels).expect("image"); |
| let mut ca: CellularAutomaton<_> = CellularAutomaton::<rule::Maze>::with_image(&input, Some(0)); |
|
|
| |
| ca.step_once(); |
| let actual: Image = ca.current; |
| |
| |
| let expected_pixels: Vec<u8> = vec![ |
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, |
| 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, |
| 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, |
| 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, |
| 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, |
| 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, |
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| ]; |
| let expected: Image = Image::try_create(10, 10, expected_pixels).expect("image"); |
| assert_eq!(actual, expected); |
| } |
| } |
|
|