Skip to content

Commit

Permalink
feat(shapes): added banana_spark, which is cheeky and doesn't behave …
Browse files Browse the repository at this point in the history
…as it should
  • Loading branch information
JeromeSchmied committed Feb 25, 2024
1 parent 139d0c2 commit de67f05
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
38 changes: 26 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,29 @@ impl Universe {

/// Create universe with width, height: inserting starting shape into the middle
///
/// # Buggy!
///
/// doesn't work properly for blonk-tie, banana-spark, bunnies
///
/// # Errors
///
/// if shape can't fit universe
pub fn from_figur(wh: u32, figur: &[String]) -> Result<Universe, ShapeError> {
let figur = Universe::from_vec_str(figur);
let figur_alive = figur
.cells
.iter()
.filter(|cell| cell == &&Cell::Alive)
.count();

println!("{}\r", &figur);

if wh < figur.height() || wh < figur.width() {
return Err(ShapeError::TooBig);
}

let cells = (0..wh * wh).map(|_i| Cell::Dead).collect();
let mut uni = Universe {
let mut univ = Universe {
cells,
width: wh,
height: wh,
Expand All @@ -109,14 +119,23 @@ impl Universe {

let mut j = 0;
for row in start_row as usize..start_row as usize + figur.height() as usize {
let idx = uni.get_index(row as u32, start_col);
let idx = univ.get_index(row as u32, start_col);
for i in 0..figur.width() as usize {
uni.cells[idx + i] = figur.cells[j];
univ.cells[idx + i] = figur.cells[j];
j += 1;
}
}

Ok(uni)
let univ_alive = univ
.cells
.iter()
.filter(|cell| cell == &&Cell::Alive)
.count();
if figur_alive == univ_alive {
Ok(univ)
} else {
Err(ShapeError::Other)
}
}

/// update life: `Universe`
Expand Down Expand Up @@ -190,14 +209,7 @@ impl fmt::Display for Universe {
pub enum ShapeError {
OutOfRange,
TooBig,
}
impl std::fmt::Display for ShapeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
ShapeError::OutOfRange => write!(f, "index out of range"),
ShapeError::TooBig => write!(f, "display area not big enough for this shape"),
}
}
Other,
}

/// Returns universe created from `i`. shape if exists
Expand Down Expand Up @@ -226,6 +238,8 @@ pub fn get_shape(wh: u32, i: usize) -> Result<Universe, ShapeError> {

6 => Universe::from_figur(wh, &shapes::bonk_tie()),

7 => Universe::from_figur(wh, &shapes::banana_spark()),

_ => Err(ShapeError::OutOfRange),
}
// todo!();
Expand Down
7 changes: 6 additions & 1 deletion src/shapes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;

/// Number of currently supported shapes
pub const N: u8 = 7;
pub const N: u8 = 8;

pub fn copperhead() -> Vec<String> {
// ["_".repeat(5), "#_##".into(), "_".repeat(7), "#".into(), "_".repeat(6), "#".into(), "___##___#__###_"]
Expand Down Expand Up @@ -90,6 +90,11 @@ pub fn bonk_tie() -> Vec<String> {
.to_vec()
}

/// 2×3
pub fn banana_spark() -> Vec<String> {
["#_".into(), "_#".into(), "_#".into()].to_vec()
}

pub fn rand(width: u32, height: u32) -> Universe {
let cells = (0..width * height)
.map(|_i| {
Expand Down

0 comments on commit de67f05

Please sign in to comment.