added script to generate random patterns

This commit is contained in:
JOLIMAITRE Matthieu 2022-10-15 04:36:53 +02:00
parent dc2a80bf4f
commit 1c1ad2c4fd
5 changed files with 208 additions and 164 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "golrs"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
description = "a TUI for vizualising a rust implementation of the game of life."
authors = ["Matthieu JOLIMAITRE <matthieu@imagevo.fr>"]

View file

@ -2,6 +2,10 @@
const { args } = Deno;
if (args[0] == "--help") {
console.log("usage: [bin] <size> <frequency>");
}
const size = parseInt(args[0] ?? "5");
const frequency = parseFloat(args[1] ?? "0.5");

1
rustfmt.toml Normal file
View file

@ -0,0 +1 @@
hard_tabs = true

View file

@ -38,8 +38,8 @@ pub fn main() {
let content = fs::read_to_string(path).unwrap();
let actives = deserialize(&content);
let simulation = Sim::spawn(actives);
let view = View::spawn::<HashedWorld>(simulation.handle());
let simulation = Sim::<HashedWorld>::spawn(actives);
let view = View::spawn(simulation.handle());
simulation.join();
view.join();

View file

@ -13,16 +13,24 @@ use termion::{event::Key, input::TermRead, raw::IntoRawMode};
use crate::{pos, Pos, SimHandle, World};
pub struct View {
thread: JoinHandle<()>,
}
impl View {
pub fn spawn<W>(handle: SimHandle<W>) -> Self
pub struct View<W>
where
W: World,
{
let thread = thread::spawn(|| view_loop(handle));
Self { thread }
thread: JoinHandle<()>,
sender: mpsc::Sender<ViewCmd<W>>,
}
impl<W> View<W>
where
W: World,
{
pub fn spawn(handle: SimHandle<W>) -> Self
where
W: World,
{
let (sender, receiver) = mpsc::channel();
let thread = thread::spawn(|| view_loop(receiver, handle));
Self { thread, sender }
}
pub fn join(self) {
@ -30,6 +38,37 @@ impl View {
}
}
pub struct ViewRemote<W>
where
W: World,
{
sender: mpsc::Sender<ViewCmd<W>>,
}
pub enum ViewCmd<W>
where
W: World,
{
Refresh,
UpdateWorld(W),
}
impl<W> ViewRemote<W>
where
W: World,
{
fn new(sender: mpsc::Sender<ViewCmd<W>>) -> Self {
Self { sender }
}
pub fn refresh(&self) {
self.sender.send(ViewCmd::Refresh).unwrap();
}
pub fn set_world(&self, world: W) {
self.sender.send(ViewCmd::UpdateWorld(world)).unwrap();
}
}
#[derive(Debug)]
pub enum Dir {
Up,
@ -70,7 +109,7 @@ fn input_loop(sender: mpsc::Sender<InputCmd>) {
const VIEW_REFRESH_INTERVAL: Duration = Duration::from_millis(100);
#[allow(unreachable_code)]
fn view_loop<W>(handle: SimHandle<W>)
fn view_loop<W>(receiver: mpsc::Receiver<ViewCmd<W>>, handle: SimHandle<W>)
where
W: World,
{
@ -78,7 +117,7 @@ where
let _input_handle = thread::spawn(|| input_loop(sender));
let (x, y) = termion::terminal_size().unwrap();
let mut tick_delay = 200u64;
let mut tick_delay = 200;
let mut view_origin = pos!(-(x as i32) / 2, -(y as i32));
let mut debug = false;
loop {