Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve visuals and code for life game #21

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 15 additions & 23 deletions src/life/conway_life.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ pub struct ConwayLifeOptions {
pub struct LifeCell {
pub character: char,
pub color: style::Color,
pub gen: usize,
}

pub struct ConwayLife {
Expand All @@ -48,26 +47,21 @@ pub struct ConwayLife {
}

impl LifeCell {
pub fn new(character: char, gen: usize) -> Self {
pub fn new(character: char) -> Self {
Self {
character,
color: style::Color::Rgb { r: 0, g: 255, b: 0 },
gen,
}
}

// Update the generation with wrapping at 256
pub fn update_gen(&mut self, rng: &mut rand::prelude::ThreadRng) {
self.gen = (self.gen + 1) % 255;
self.update_color_and_char(rng); // Assuming this updates color/char based on gen
}

pub fn update_color_and_char(&mut self, rng: &mut rand::prelude::ThreadRng) {
self.gen += 1;
match self.gen {
pub fn update_color_and_char(
&mut self,
rng: &mut rand::prelude::ThreadRng,
current_gen: u8,
) {
let green_color = 255_u8.wrapping_sub(current_gen);
match current_gen {
0..=250 => {
let gen_u8: u8 = (self.gen % 255) as u8;
let green_color = 255_u8.wrapping_sub(gen_u8);
self.color = style::Color::Rgb {
r: 0,
g: green_color,
Expand All @@ -78,7 +72,7 @@ impl LifeCell {
_ => {
self.color = style::Color::Rgb {
r: 128,
g: 0,
g: green_color,
b: 128,
}; // Purple
let random_index = rng.gen_range(0..DEAD_CELLS_CHARS.len());
Expand Down Expand Up @@ -117,17 +111,17 @@ impl TerminalEffect for ConwayLife {
let alive_neighbors = neighbors.len();

if let Some(cell) = self.cells.get_mut(&(nx, ny)) {
cell.update_gen(&mut self.rng);
cell.update_color_and_char(&mut self.rng, self.current_gen);

// Survival: an alive cell with 2 or 3 alive neighbors stays alive
if alive_neighbors == 2 || alive_neighbors == 3 {
next_cells.insert((nx, ny), cell.clone());
}
} else {
// Birth: a dead cell with exactly 3 alive neighbors becomes alive
if alive_neighbors == 3 {
let mut new_cell =
LifeCell::new('*', self.current_gen.try_into().unwrap());
new_cell.update_gen(&mut self.rng); // Initialize generation and update color/char
let mut new_cell = LifeCell::new('*');
new_cell.update_color_and_char(&mut self.rng, self.current_gen); // Initialize generation and update color/char
next_cells.insert((nx, ny), new_cell);
// Replace 'X' with the desired initial state
}
Expand Down Expand Up @@ -155,7 +149,7 @@ impl ConwayLife {

let mut cells = HashMap::new();
for _ in 0..options.initial_cells {
let lc = LifeCell::new('*', 0);
let lc = LifeCell::new('*');
let x = rng.gen_range(0..options.screen_size.0);
let y = rng.gen_range(0..options.screen_size.1);

Expand Down Expand Up @@ -201,8 +195,7 @@ fn insert_glider(
}
});

let gen_u8: u8 = (current_gen % 255) as u8;
let green_color = 255_u8.wrapping_sub(gen_u8);
let green_color = 255_u8.wrapping_sub(current_gen);

for coords in rotated_glider {
cells.insert(
Expand All @@ -214,7 +207,6 @@ fn insert_glider(
g: green_color,
b: 0,
},
gen: 0,
},
);
}
Expand Down
Loading