Tweak initialization probabilities

Also rename the cell states to dead resp. alive,
which is standard nomenclature for game of life.
This commit is contained in:
Juergen Stuber 2023-04-14 12:26:12 +02:00
parent 5f4624dab2
commit e451a9cc36

View File

@ -18,8 +18,8 @@ use pixelfoo::color::Color;
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Square { enum Square {
Empty, Dead,
Full, Alive,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -51,8 +51,8 @@ fn send<T: Write>(w: &mut T, board: &Board) -> std::io::Result<()> {
for x in board.bbox().x_range() { for x in board.bbox().x_range() {
let square = board.map[p2d(x, y)]; let square = board.map[p2d(x, y)];
let c = match square { let c = match square {
Square::Empty => Color::black(), Square::Dead => Color::black(),
Square::Full => Color::green(), Square::Alive => Color::green(),
}; };
w.write_all(&c.rgb())?; w.write_all(&c.rgb())?;
} }
@ -74,14 +74,16 @@ fn main() -> std::io::Result<()> {
let mut rng = thread_rng(); let mut rng = thread_rng();
let bbox = bb2d(0..x_size as i64, 0..y_size as i64); let bbox = bb2d(0..x_size as i64, 0..y_size as i64);
let p_full = 0.25; let p_alive_init = 0.125;
let p_alive_edge = 0.25;
let mut board = Board::with(bbox, |_p| { let mut board = Board::with(bbox, |_p| {
// Initialize board randomly. // Initialize board randomly.
let r = rng.gen::<f64>(); let r = rng.gen::<f64>();
if r < p_full { if r < p_alive_init {
Square::Full Square::Alive
} else { } else {
Square::Empty Square::Dead
} }
}); });
@ -94,25 +96,25 @@ fn main() -> std::io::Result<()> {
// Use the game of life rule in the interior. // Use the game of life rule in the interior.
let nc = p let nc = p
.neighbors_l_infty() .neighbors_l_infty()
.filter(|&np| board.get(np) == Square::Full) .filter(|&np| board.get(np) == Square::Alive)
.count(); .count();
if nc == 3 || (nc == 2 && board.get(p) == Square::Full) { if nc == 3 || (nc == 2 && board.get(p) == Square::Alive) {
Square::Full Square::Alive
} else { } else {
Square::Empty Square::Dead
} }
} else { } else {
if p.y() < bbox.y_min() || p.y() > bbox.y_max() { if p.y() < bbox.y_min() || p.y() > bbox.y_max() {
// The invisible long edge is filled randomly. // The invisible long edge is filled randomly.
let r = rng.gen::<f64>(); let r = rng.gen::<f64>();
if r < p_full { if r < p_alive_edge {
Square::Full Square::Alive
} else { } else {
Square::Empty Square::Dead
} }
} else { } else {
// The short edges are empty. // The short edges are empty.
Square::Empty Square::Dead
} }
} }
}); });