Skip to content

Commit

Permalink
use full terminal size; improve
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaechus committed Mar 25, 2023
1 parent 93b40ac commit adb5bf5
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 147 deletions.
70 changes: 35 additions & 35 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 11 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
[package]
name = "gameoflife"
version = "1.1.2"
edition = "2021"
authors = ["Maxwell Anderson <[email protected]>"]
license = "MIT"
description = "Conway's Game of Life running in a terminal"
version = "1.2.0"
authors = ["Maxwell Anderson <[email protected]>"]
description = "Conway's Game of Life for the terminal"
repository = "https://github.com/Zaechus/gameoflife"
license = "MIT"
categories = ["games"]
edition = "2021"

[dependencies]
crossterm = "0.26"
ctrlc = "3.2"
rand = "0.8"
rayon = "1.6"
rayon = "1.7"

[profile.release]
panic = "abort"
strip = true
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Current Crates.io Version](https://img.shields.io/crates/v/gameoflife)](https://crates.io/crates/gameoflife)

Conway's Game of Life running in a terminal
Conway's Game of Life for the terminal

### Usage
```
Expand Down
60 changes: 31 additions & 29 deletions src/game.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
use std::{cell::RefCell, io::stdout, process, thread, time::Duration};
use std::{io::stdout, process, thread, time::Duration};

use crossterm::{cursor, execute, terminal};

use crate::grid::Grid;

pub struct ConwaysGame {
grid: RefCell<Grid>,
}
pub fn play(interval: u64) {
ctrlc::set_handler(move || {
execute!(
stdout(),
terminal::Clear(terminal::ClearType::All),
cursor::Show
)
.unwrap();
process::exit(0);
})
.unwrap();

impl ConwaysGame {
pub fn new(interval: u32) -> Self {
Self {
grid: RefCell::new(Grid::new(interval)),
}
}
execute!(
stdout(),
terminal::Clear(terminal::ClearType::All),
cursor::Hide
)
.unwrap();

let mut grid = Grid::new();

loop {
grid.print().unwrap();

let handle = thread::spawn(move || {
thread::sleep(Duration::from_millis(interval));
});

grid.change_cells();
grid.update_cells();

pub fn play(&self) {
ctrlc::set_handler(move || {
execute!(
stdout(),
terminal::Clear(terminal::ClearType::All),
cursor::Show
)
.unwrap();
process::exit(0);
})
.expect("Error setting Ctrl-C handler");

loop {
self.grid.borrow_mut().print().unwrap();
self.grid.borrow_mut().change_cells();
self.grid.borrow_mut().update_cells();

thread::sleep(Duration::from_millis(self.grid.borrow().interval() as u64));
}
handle.join().unwrap();
}
}
14 changes: 7 additions & 7 deletions src/gamecell.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
use std::sync::atomic::{AtomicBool, Ordering};

pub struct GameCell {
pub(crate) struct GameCell {
alive: bool,
will_live: AtomicBool,
}

impl GameCell {
pub fn new(alive: bool) -> Self {
pub(crate) fn new(alive: bool) -> Self {
Self {
alive,
will_live: AtomicBool::new(false),
}
}

pub fn is_alive(&self) -> bool {
pub(crate) fn is_alive(&self) -> bool {
self.alive
}

pub fn state(&self) -> u8 {
pub(crate) fn state(&self) -> u8 {
self.alive as u8
}

pub fn symbol(&self) -> char {
pub(crate) fn symbol(&self) -> char {
if self.alive {
'█'
} else {
' '
}
}

pub fn update(&mut self) {
pub(crate) fn update(&mut self) {
self.alive = *self.will_live.get_mut()
}

pub fn set_will_live(&self, b: bool) {
pub(crate) fn set_will_live(&self, b: bool) {
self.will_live.store(b, Ordering::Relaxed)
}
}
Loading

0 comments on commit adb5bf5

Please sign in to comment.