-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
129 additions
and
147 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.