-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcgol.rs
executable file
·75 lines (66 loc) · 1.8 KB
/
cgol.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env cargo script
//! ```cargo
//! [dependencies]
//! rand = "0.8"
//! ```
#![allow(unused)]
extern crate rand;
use rand::Rng;
use std::thread::sleep;
use std::time::Duration;
const WIDTH: usize = 50;
const HEIGHT: usize = 30;
const DENSITY: f64 = 0.2; // Probability a cell is alive
fn initialize_grid() -> Vec<Vec<bool>> {
let mut rng = rand::thread_rng();
let mut grid = vec![vec![false; WIDTH]; HEIGHT];
for row in grid.iter_mut() {
for cell in row.iter_mut() {
*cell = rng.gen::<f64>() < DENSITY;
}
}
grid
}
fn count_neighbors(grid: &Vec<Vec<bool>>, x: usize, y: usize) -> usize {
let mut count = 0;
for dx in [WIDTH - 1, 0, 1].iter().cloned() {
for dy in [HEIGHT - 1, 0, 1].iter().cloned() {
if dx == 0 && dy == 0 { continue; }
let nx = (x + dx) % WIDTH;
let ny = (y + dy) % HEIGHT;
if grid[ny][nx] { count += 1; }
}
}
count
}
fn compute_next_state(grid: &Vec<Vec<bool>>) -> Vec<Vec<bool>> {
let mut new_grid = vec![vec![false; WIDTH]; HEIGHT];
for y in 0..HEIGHT {
for x in 0..WIDTH {
let alive = grid[y][x];
let neighbors = count_neighbors(grid, x, y);
new_grid[y][x] = match (alive, neighbors) {
(true, 2) | (_, 3) => true,
_ => false,
};
}
}
new_grid
}
fn print_grid(grid: &Vec<Vec<bool>>) {
for row in grid {
for &cell in row {
print!("{}", if cell { "█" } else { " " });
}
println!();
}
}
fn main() {
let mut grid = initialize_grid();
loop {
print_grid(&grid);
grid = compute_next_state(&grid);
sleep(Duration::from_millis(100));
println!("\x1B[2J\x1B[1;1H"); // Clear screen
}
}