use crate::{
cells::{Coord, State, ALIVE, DEAD},
config::Config,
error::Error,
rules::Rule,
search::Status,
world::World,
};
use std::fmt::Write;
#[cfg(feature = "serialize")]
use crate::save::WorldSer;
pub trait Search {
fn search(&mut self, max_step: Option<u64>) -> Status;
fn get_cell_state(&self, coord: Coord) -> Result<Option<State>, Error>;
fn config(&self) -> &Config;
fn is_gen_rule(&self) -> bool;
fn cell_count_gen(&self, t: isize) -> usize;
fn cell_count(&self) -> usize;
fn conflicts(&self) -> u64;
fn set_max_cell_count(&mut self, max_cell_count: Option<usize>);
#[cfg(feature = "serialize")]
fn ser(&self) -> WorldSer;
fn rle_gen(&self, t: isize) -> String {
let mut str = String::new();
writeln!(
str,
"x = {}, y = {}, rule = {}",
self.config().width,
self.config().height,
self.config().rule_string
)
.unwrap();
for y in 0..self.config().height {
for x in 0..self.config().width {
let state = self.get_cell_state((x, y, t)).unwrap();
match state {
Some(DEAD) => str.push('.'),
Some(ALIVE) => {
if self.is_gen_rule() {
str.push('A')
} else {
str.push('o')
}
}
Some(State(i)) => str.push((b'A' + i as u8 - 1) as char),
_ => str.push('?'),
};
}
if y == self.config().height - 1 {
str.push('!')
} else {
str.push('$')
};
str.push('\n');
}
str
}
fn plaintext_gen(&self, t: isize) -> String {
let mut str = String::new();
for y in 0..self.config().height {
for x in 0..self.config().width {
let state = self.get_cell_state((x, y, t)).unwrap();
match state {
Some(DEAD) => str.push('.'),
Some(_) => str.push('o'),
None => str.push('?'),
};
}
str.push('\n');
}
str
}
}
impl<'a, R: Rule> Search for World<'a, R> {
fn search(&mut self, max_step: Option<u64>) -> Status {
self.search(max_step)
}
fn get_cell_state(&self, coord: Coord) -> Result<Option<State>, Error> {
self.get_cell_state(coord)
}
fn config(&self) -> &Config {
&self.config
}
fn is_gen_rule(&self) -> bool {
R::IS_GEN
}
fn cell_count_gen(&self, t: isize) -> usize {
self.cell_count[t as usize]
}
fn cell_count(&self) -> usize {
*self.cell_count.iter().min().unwrap()
}
fn conflicts(&self) -> u64 {
self.conflicts
}
fn set_max_cell_count(&mut self, max_cell_count: Option<usize>) {
self.set_max_cell_count(max_cell_count)
}
#[cfg(feature = "serialize")]
fn ser(&self) -> WorldSer {
self.ser()
}
}