diff --git a/src/lib.rs b/src/lib.rs index a2d56fe..5d9d9b1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +/// information about one `Cell`: either `Dead` or `Alive` #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Cell { Dead = 0, @@ -12,6 +13,7 @@ impl Cell { } } +/// the `Universe` in which game plays. Represented as a `Vec` of `Cell`s. #[derive(Debug)] pub struct Universe { width: u32, @@ -28,8 +30,8 @@ impl Universe { fn live_neighbour_count(&self, row: u32, col: u32) -> u8 { let mut sum = 0; - for delta_row in [self.height - 1, 0, 1].iter().copied() { - for delta_col in [self.width - 1, 0, 1].iter().copied() { + for delta_row in [self.height - 1, 0, 1] { + for delta_col in [self.width - 1, 0, 1] { if delta_row == 0 && delta_col == 0 { continue; } @@ -66,20 +68,6 @@ impl Universe { } } - /// Get the dead and alive values of the entire universe. - pub fn get_cells(&self) -> &[Cell] { - &self.cells - } - - /// Set cells to be alive in a universe by passing the row and column - /// of each cell as an array. - pub fn set_cells(&mut self, cells: &[(u32, u32)]) { - for (row, col) in cells.iter().copied() { - let idx = self.get_index(row, col); - self.cells[idx] = Cell::Alive; - } - } - /// Create universe with width, height: inserting starting shape into the middle /// /// # Errors @@ -114,11 +102,8 @@ impl Universe { Ok(uni) } -} -/// Public functions exported to JavaScript as well. -impl Universe { - /// update life: Universe + /// update life: `Universe` pub fn tick(&mut self) { let mut next = self.cells.clone(); @@ -152,11 +137,6 @@ impl Universe { self.cells = next; } - /// turn formatted self to string - pub fn render(&self) -> String { - self.to_string() - } - pub fn width(&self) -> u32 { self.width } @@ -165,27 +145,6 @@ impl Universe { self.height } - /// return `cells` as pointer: starting mem-pointer - pub fn cells(&self) -> *const Cell { - self.cells.as_ptr() - } - - /// Set the width of the universe. - /// - /// Resets all cells to the dead state. - pub fn set_width(&mut self, width: u32) { - self.width = width; - self.cells = (0..width * self.height).map(|_i| Cell::Dead).collect(); - } - - /// Set the height of the universe. - /// - /// Resets all cells to the dead state. - pub fn set_height(&mut self, height: u32) { - self.height = height; - self.cells = (0..self.width * height).map(|_i| Cell::Dead).collect(); - } - /// toggles cell at (`row`;`col`) pub fn toggle_cell(&mut self, row: u32, col: u32) { let idx = self.get_index(row, col); diff --git a/src/main.rs b/src/main.rs index 7a959f1..c189961 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use conways_game_of_life_cli_rs::*; +const DEFAULT_DUR: Duration = Duration::from_millis(400); const HELP: &str = r#"Blocking poll() & non-blocking read() - Prints current state of Conway's Game of Life if there's no event - Use Esc or `q` to quit @@ -50,20 +51,18 @@ fn print_events() -> io::Result<()> { let mut i: usize = 0; let mut universe = get_shape(wh, i).unwrap(); - const DEFAULT_DUR: Duration = Duration::from_millis(400); let mut poll_t = DEFAULT_DUR; let mut paused = false; let mut prev_poll_t = DEFAULT_DUR; loop { - // Wait up to 1s for another event + // Wait up to `poll_t` for another event if poll(poll_t)? { // It's guaranteed that read() won't block if `poll` returns `Ok(true)` let event = read()?; if kmaps::quit().contains(&event) { println!("Quitting...\r"); - // queue!(io::stdout(), Print("Quitting..."))?; break; } else if kmaps::slower().contains(&event) { if poll_t < Duration::from_millis(40) { @@ -135,36 +134,7 @@ fn print_events() -> io::Result<()> { } else { eprintln!("Couldn't make larger"); } - // } else if event == MouseEventKind::Down(MouseButton::Left.into()) { - // } else if event == Event::Mouse(MouseEvent { kind, column , row , modifiers }) { - // } else { - // println!("Unknown: Event::{:?}\r", event); } else { - // match event { - // crossterm::event::Event::FocusGained => eprintln!("Focus Gained."), - // crossterm::event::Event::FocusLost => eprintln!("Focus Lost."), - // crossterm::event::Event::Key(k) => { - // eprintln!("Unknown key: {:?}", k); - // } - // crossterm::event::Event::Mouse(m) => { - // if m.kind == MouseEventKind::Up(MouseButton::Left) - // || m.kind == MouseEventKind::Drag(MouseButton::Left) - // { - // // eprintln!("row: {}, col: {}\r", m.row - 1, m.column - 1); - // std::thread::sleep(DEFAULT_DUR); - // if m.row > 0 - // && m.row <= universe.height() as u16 - // && m.column > 0 - // && m.column < universe.width() as u16 - // { - // universe.toggle_cell((m.row - 1).into(), (m.column - 1).into()); - // } - // println!("{}", universe); - // } - // } - // crossterm::event::Event::Paste(_) => eprintln!("Paste"), - // crossterm::event::Event::Resize(_, _) => eprintln!("Resize"), - // } eprintln!("Unknown event: {:?}", event); } } else { @@ -173,13 +143,9 @@ fn print_events() -> io::Result<()> { execute!(io::stdout(), MoveTo(0, 0), Clear(ClearType::FromCursorDown))?; println!("{}", universe); } - // io::stdout().flush()?; } - execute!( - io::stdout(), - LeaveAlternateScreen /*, DisableMouseCapture*/ - )?; + execute!(io::stdout(), LeaveAlternateScreen)?; Ok(()) }