diff --git a/src/core/cellular_automaton.rs b/src/core/cellular_automaton.rs index eff40e0..26815d0 100644 --- a/src/core/cellular_automaton.rs +++ b/src/core/cellular_automaton.rs @@ -84,6 +84,10 @@ impl CA1D { self.next_world = Box::new(next_world); } + pub fn set_world_initialisation(&mut self, world_initialisation: Vec) { + self.current_world = Box::new(world_initialisation); + } + pub fn set_num_states(&mut self, num_states: u8) { self.num_states = num_states; } @@ -107,5 +111,9 @@ impl CA1D { std::mem::swap(&mut self.current_world, &mut self.next_world); } + pub fn print(&self) { + println!("{}", self); + } + } diff --git a/src/core/cellular_automaton_builder.rs b/src/core/cellular_automaton_builder.rs new file mode 100644 index 0000000..819df1f --- /dev/null +++ b/src/core/cellular_automaton_builder.rs @@ -0,0 +1,48 @@ +use crate::core::cellular_automaton::CA1D; +use crate::core::cellular_automaton_configuration::CA1DConfiguration; +use crate::core::cellular_automaton_configuration::CA1DConfigurationError; +use crate::core::lookup_table_builder::LookupTableBuilder; +use crate::core::lookup_table_builder::LookupTableBuilderError; + +pub struct CellularAutomatonBuilder; + +#[derive(Debug)] +pub enum CellularAutomatonBuilderError { + ConfigurationError(CA1DConfigurationError), + LookupTableBuilderError(LookupTableBuilderError), +} + +impl From for CellularAutomatonBuilderError { + fn from(err: CA1DConfigurationError) -> Self { + CellularAutomatonBuilderError::ConfigurationError(err) + } +} + +impl From for CellularAutomatonBuilderError { + fn from(err: LookupTableBuilderError) -> Self { + CellularAutomatonBuilderError::LookupTableBuilderError(err) + } +} + +impl CellularAutomatonBuilder { + pub fn new() -> Self { + Self {} + } + + pub fn build(&self, configuration_file_name: &str) -> Result { + let ca1d_configuration = CA1DConfiguration::new_from_json_file(configuration_file_name)?; + + let lookup_table_builder = LookupTableBuilder::new(); + let lookup_table = lookup_table_builder.build(ca1d_configuration.get_rules_file_name())?; + + let mut ca1d = CA1D::new( + ca1d_configuration.get_num_states(), + ca1d_configuration.get_num_cells(), + lookup_table, + ); + + ca1d.set_world_initialisation(ca1d_configuration.get_world_initialisation()); + + Ok(ca1d) + } +} \ No newline at end of file diff --git a/src/core/cellular_automaton_configuration.rs b/src/core/cellular_automaton_configuration.rs index 73a3283..69dc364 100644 --- a/src/core/cellular_automaton_configuration.rs +++ b/src/core/cellular_automaton_configuration.rs @@ -64,4 +64,16 @@ impl CA1DConfiguration { pub fn get_world_initialisation(&self) -> Vec { self.world_initialisation.clone().into() } + + pub fn get_rules_file_name(&self) -> &str { + &self.rules_file_name + } + + pub fn get_num_states(&self) -> u8 { + self.num_states + } + + pub fn get_num_cells(&self) -> usize { + self.num_cells + } } \ No newline at end of file diff --git a/src/core/mod.rs b/src/core/mod.rs index e251d93..983ba91 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -3,3 +3,4 @@ pub mod lookup_table_builder; pub mod rules; pub mod cellular_automaton; pub mod cellular_automaton_configuration; +pub mod cellular_automaton_builder; diff --git a/tests/test_cellular_automaton_builder.rs b/tests/test_cellular_automaton_builder.rs new file mode 100644 index 0000000..e8e652e --- /dev/null +++ b/tests/test_cellular_automaton_builder.rs @@ -0,0 +1,15 @@ +use mono_axis::core::cellular_automaton_builder::CellularAutomatonBuilder; + +#[test] +fn it_works() { + let cab = CellularAutomatonBuilder::new(); + let mut ca = cab.build("examples/example_01/configuration.json").unwrap(); + + for _ in 0..7 { + ca.step(); + } + + let result = ca.get_current_world(); + + assert_eq!(*result, vec![0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0]); +} \ No newline at end of file