added script to generate random patterns
This commit is contained in:
parent
dc2a80bf4f
commit
1c1ad2c4fd
5 changed files with 208 additions and 164 deletions
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "golrs"
|
name = "golrs"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "a TUI for vizualising a rust implementation of the game of life."
|
description = "a TUI for vizualising a rust implementation of the game of life."
|
||||||
authors = ["Matthieu JOLIMAITRE <matthieu@imagevo.fr>"]
|
authors = ["Matthieu JOLIMAITRE <matthieu@imagevo.fr>"]
|
||||||
|
|
|
@ -2,6 +2,10 @@
|
||||||
|
|
||||||
const { args } = Deno;
|
const { args } = Deno;
|
||||||
|
|
||||||
|
if (args[0] == "--help") {
|
||||||
|
console.log("usage: [bin] <size> <frequency>");
|
||||||
|
}
|
||||||
|
|
||||||
const size = parseInt(args[0] ?? "5");
|
const size = parseInt(args[0] ?? "5");
|
||||||
const frequency = parseFloat(args[1] ?? "0.5");
|
const frequency = parseFloat(args[1] ?? "0.5");
|
||||||
|
|
||||||
|
|
1
rustfmt.toml
Normal file
1
rustfmt.toml
Normal file
|
@ -0,0 +1 @@
|
||||||
|
hard_tabs = true
|
|
@ -38,8 +38,8 @@ pub fn main() {
|
||||||
|
|
||||||
let content = fs::read_to_string(path).unwrap();
|
let content = fs::read_to_string(path).unwrap();
|
||||||
let actives = deserialize(&content);
|
let actives = deserialize(&content);
|
||||||
let simulation = Sim::spawn(actives);
|
let simulation = Sim::<HashedWorld>::spawn(actives);
|
||||||
let view = View::spawn::<HashedWorld>(simulation.handle());
|
let view = View::spawn(simulation.handle());
|
||||||
|
|
||||||
simulation.join();
|
simulation.join();
|
||||||
view.join();
|
view.join();
|
||||||
|
|
53
src/view.rs
53
src/view.rs
|
@ -13,16 +13,24 @@ use termion::{event::Key, input::TermRead, raw::IntoRawMode};
|
||||||
|
|
||||||
use crate::{pos, Pos, SimHandle, World};
|
use crate::{pos, Pos, SimHandle, World};
|
||||||
|
|
||||||
pub struct View {
|
pub struct View<W>
|
||||||
|
where
|
||||||
|
W: World,
|
||||||
|
{
|
||||||
thread: JoinHandle<()>,
|
thread: JoinHandle<()>,
|
||||||
|
sender: mpsc::Sender<ViewCmd<W>>,
|
||||||
}
|
}
|
||||||
impl View {
|
impl<W> View<W>
|
||||||
pub fn spawn<W>(handle: SimHandle<W>) -> Self
|
where
|
||||||
|
W: World,
|
||||||
|
{
|
||||||
|
pub fn spawn(handle: SimHandle<W>) -> Self
|
||||||
where
|
where
|
||||||
W: World,
|
W: World,
|
||||||
{
|
{
|
||||||
let thread = thread::spawn(|| view_loop(handle));
|
let (sender, receiver) = mpsc::channel();
|
||||||
Self { thread }
|
let thread = thread::spawn(|| view_loop(receiver, handle));
|
||||||
|
Self { thread, sender }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn join(self) {
|
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)]
|
#[derive(Debug)]
|
||||||
pub enum Dir {
|
pub enum Dir {
|
||||||
Up,
|
Up,
|
||||||
|
@ -70,7 +109,7 @@ fn input_loop(sender: mpsc::Sender<InputCmd>) {
|
||||||
const VIEW_REFRESH_INTERVAL: Duration = Duration::from_millis(100);
|
const VIEW_REFRESH_INTERVAL: Duration = Duration::from_millis(100);
|
||||||
|
|
||||||
#[allow(unreachable_code)]
|
#[allow(unreachable_code)]
|
||||||
fn view_loop<W>(handle: SimHandle<W>)
|
fn view_loop<W>(receiver: mpsc::Receiver<ViewCmd<W>>, handle: SimHandle<W>)
|
||||||
where
|
where
|
||||||
W: World,
|
W: World,
|
||||||
{
|
{
|
||||||
|
@ -78,7 +117,7 @@ where
|
||||||
let _input_handle = thread::spawn(|| input_loop(sender));
|
let _input_handle = thread::spawn(|| input_loop(sender));
|
||||||
|
|
||||||
let (x, y) = termion::terminal_size().unwrap();
|
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 view_origin = pos!(-(x as i32) / 2, -(y as i32));
|
||||||
let mut debug = false;
|
let mut debug = false;
|
||||||
loop {
|
loop {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue