conway
This commit is contained in:
commit
656ed32f8a
7 changed files with 434 additions and 0 deletions
92
src/main.rs
Normal file
92
src/main.rs
Normal file
|
@ -0,0 +1,92 @@
|
|||
#![no_main]
|
||||
#![no_std]
|
||||
|
||||
use core::iter::repeat;
|
||||
|
||||
use heapless::Vec;
|
||||
use log::info;
|
||||
use system::with_stdout;
|
||||
use uefi::{prelude::*, print, println};
|
||||
|
||||
#[entry]
|
||||
fn main() -> Status {
|
||||
uefi::helpers::init().unwrap();
|
||||
info!("Hello world!");
|
||||
|
||||
const W: usize = 32;
|
||||
const H: usize = 32;
|
||||
|
||||
let mut life: Vec<Vec<bool, W>, H> = life();
|
||||
for (i, j) in [(15, 16), (16, 15), (16, 16), (17, 16), (17, 17)] {
|
||||
life[i][j] = true;
|
||||
}
|
||||
|
||||
for i in 0..100_000 {
|
||||
with_stdout(|stdout| stdout.clear()).unwrap();
|
||||
println!("Generation: {}", i);
|
||||
life = gol(life.clone());
|
||||
for l in life.iter() {
|
||||
for cell in l.iter() {
|
||||
if *cell {
|
||||
print!("X")
|
||||
} else {
|
||||
print!(" ")
|
||||
}
|
||||
}
|
||||
println!("");
|
||||
}
|
||||
boot::stall(1_000);
|
||||
}
|
||||
|
||||
boot::stall(100_000_000);
|
||||
Status::SUCCESS
|
||||
}
|
||||
|
||||
fn gol<const W: usize, const H: usize>(cells: Vec<Vec<bool, W>, H>) -> Vec<Vec<bool, W>, H> {
|
||||
let mut future = life();
|
||||
cells.iter().enumerate().for_each(|(row, line)| {
|
||||
line.iter().enumerate().for_each(|(column, cell)| {
|
||||
let neighbors = [
|
||||
(-1 as i64, -1),
|
||||
(-1, 0),
|
||||
(-1, 1 as i64),
|
||||
(0, -1),
|
||||
(0, 1),
|
||||
(1, -1),
|
||||
(1, 0),
|
||||
(1, 1),
|
||||
]
|
||||
.iter()
|
||||
.filter_map(|offset| {
|
||||
let neighbor = (
|
||||
(row as i64).checked_add(offset.0),
|
||||
(column as i64).checked_add(offset.1),
|
||||
);
|
||||
match neighbor {
|
||||
(Some(r), Some(w))
|
||||
if r >= 0
|
||||
&& w >= 0
|
||||
&& (r as usize) < H
|
||||
&& (w as usize) < W
|
||||
&& cells[r as usize][w as usize] =>
|
||||
{
|
||||
Some(())
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
})
|
||||
.count();
|
||||
future[row][column] =
|
||||
(*cell && (neighbors == 2 || neighbors == 3)) || (!*cell && neighbors == 3);
|
||||
})
|
||||
});
|
||||
future
|
||||
}
|
||||
|
||||
fn life<const W: usize, const H: usize>() -> Vec<Vec<bool, W>, H> {
|
||||
let mut future: Vec<Vec<bool, W>, H> = Vec::default();
|
||||
for _ in 0..H {
|
||||
future.push(repeat(false).take(W).collect()).unwrap();
|
||||
}
|
||||
future
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue