From b64e0ea5e31e718e435b24244119f59f7128bc27 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 10 Jul 2020 04:14:21 +0200 Subject: Add `Preset` selector to `game_of_life` example --- examples/game_of_life/src/preset.rs | 142 ++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 examples/game_of_life/src/preset.rs (limited to 'examples/game_of_life/src/preset.rs') diff --git a/examples/game_of_life/src/preset.rs b/examples/game_of_life/src/preset.rs new file mode 100644 index 00000000..1a471141 --- /dev/null +++ b/examples/game_of_life/src/preset.rs @@ -0,0 +1,142 @@ +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Preset { + Custom, + XKCD, + Glider, + SmallExploder, + Exploder, + TenCellRow, + LightweightSpaceship, + Tumbler, + Acorn, + GliderGun, +} + +pub static ALL: &[Preset] = &[ + Preset::Custom, + Preset::XKCD, + Preset::Glider, + Preset::SmallExploder, + Preset::Exploder, + Preset::TenCellRow, + Preset::LightweightSpaceship, + Preset::Tumbler, + Preset::Acorn, + Preset::GliderGun, +]; + +impl Preset { + pub fn life(self) -> Vec<(isize, isize)> { + #[rustfmt::skip] + let cells = match self { + Preset::Custom => vec![], + Preset::XKCD => vec![ + " xxx ", + " x x ", + " x x ", + " x ", + "x xxx ", + " x x x ", + " x x", + " x x ", + " x x ", + ], + Preset::Glider => vec![ + " x ", + " x", + "xxx" + ], + Preset::SmallExploder => vec![ + " x ", + "xxx", + "x x", + " x ", + ], + Preset::Exploder => vec![ + "x x x", + "x x", + "x x", + "x x", + "x x x", + ], + Preset::TenCellRow => vec![ + "xxxxxxxxxx", + ], + Preset::LightweightSpaceship => vec![ + " xxxxx", + "x x", + " x", + "x x ", + ], + Preset::Tumbler => vec![ + " xx xx ", + " xx xx ", + " x x ", + "x x x x", + "x x x x", + "xx xx", + ], + Preset::Acorn => vec![ + " x ", + " x ", + "xx xxx", + ], + Preset::GliderGun => vec![ + " x ", + " x x ", + " xx xx xx", + " x x xx xx", + "xx x x xx ", + "xx x x xx x x ", + " x x x ", + " x x ", + " xx ", + ] + }; + + let start_row = -(cells.len() as isize / 2); + + cells + .into_iter() + .enumerate() + .flat_map(|(i, cells)| { + let start_column = -(cells.len() as isize / 2); + + cells + .chars() + .enumerate() + .filter(|(_, c)| !c.is_whitespace()) + .map(move |(j, _)| { + (start_row + i as isize, start_column + j as isize) + }) + }) + .collect() + } +} + +impl Default for Preset { + fn default() -> Preset { + Preset::XKCD + } +} + +impl std::fmt::Display for Preset { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + match self { + Preset::Custom => "Custom", + Preset::XKCD => "xkcd #2293", + Preset::Glider => "Glider", + Preset::SmallExploder => "Small Exploder", + Preset::Exploder => "Exploder", + Preset::TenCellRow => "10 Cell Row", + Preset::LightweightSpaceship => "Lightweight spaceship", + Preset::Tumbler => "Tumbler", + Preset::Acorn => "Acorn", + Preset::GliderGun => "Gosper Glider Gun", + } + ) + } +} -- cgit From 94383d82a5d080a28de025fadc6b7ba27e37927d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 10 Jul 2020 07:41:31 +0200 Subject: Style `PickList` in `game_of_life` example --- examples/game_of_life/src/preset.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'examples/game_of_life/src/preset.rs') diff --git a/examples/game_of_life/src/preset.rs b/examples/game_of_life/src/preset.rs index 1a471141..05157b6a 100644 --- a/examples/game_of_life/src/preset.rs +++ b/examples/game_of_life/src/preset.rs @@ -8,8 +8,8 @@ pub enum Preset { TenCellRow, LightweightSpaceship, Tumbler, - Acorn, GliderGun, + Acorn, } pub static ALL: &[Preset] = &[ @@ -21,8 +21,8 @@ pub static ALL: &[Preset] = &[ Preset::TenCellRow, Preset::LightweightSpaceship, Preset::Tumbler, - Preset::Acorn, Preset::GliderGun, + Preset::Acorn, ]; impl Preset { @@ -76,11 +76,6 @@ impl Preset { "x x x x", "xx xx", ], - Preset::Acorn => vec![ - " x ", - " x ", - "xx xxx", - ], Preset::GliderGun => vec![ " x ", " x x ", @@ -91,7 +86,12 @@ impl Preset { " x x x ", " x x ", " xx ", - ] + ], + Preset::Acorn => vec![ + " x ", + " x ", + "xx xxx", + ], }; let start_row = -(cells.len() as isize / 2); @@ -134,8 +134,8 @@ impl std::fmt::Display for Preset { Preset::TenCellRow => "10 Cell Row", Preset::LightweightSpaceship => "Lightweight spaceship", Preset::Tumbler => "Tumbler", - Preset::Acorn => "Acorn", Preset::GliderGun => "Gosper Glider Gun", + Preset::Acorn => "Acorn", } ) } -- cgit