diff options
| author | omagdy7 <omar.professional8777@gmail.com> | 2022-09-14 22:11:44 +0200 |
|---|---|---|
| committer | omagdy7 <omar.professional8777@gmail.com> | 2022-09-14 22:11:44 +0200 |
| commit | 72c46d547eeee2fe7700ec2dbbb97b9c0f67a373 (patch) | |
| tree | a5316503df2d9f15d8c4af532230427e711f79d1 /src | |
| parent | 4023db5205bacef6cf6cb9d41ab4cdfa90cd5b32 (diff) | |
| download | gof-rs-72c46d547eeee2fe7700ec2dbbb97b9c0f67a373.tar.xz gof-rs-72c46d547eeee2fe7700ec2dbbb97b9c0f67a373.zip | |
Added a few scripts to parse the large pattern file of game of life patterns and also added the functionality to scroll through the list and it affects the current world
Diffstat (limited to 'src')
| -rw-r--r-- | src/debug.txt | 13 | ||||
| -rw-r--r-- | src/generation.rs | 34 | ||||
| -rw-r--r-- | src/ui.rs | 69 |
3 files changed, 65 insertions, 51 deletions
diff --git a/src/debug.txt b/src/debug.txt deleted file mode 100644 index 1aec867..0000000 --- a/src/debug.txt +++ /dev/null @@ -1,13 +0,0 @@ -warning: function `render_gen` is never used - --> src/generation.rs:38:8 - | -38 | pub fn render_gen(chunk: &Rect, gen: &Gen) { - | ^^^^^^^^^^ - | - = note: `#[warn(dead_code)]` on by default - -warning: `gof-rs` (bin "gof-rs") generated 1 warning - Finished dev [unoptimized + debuginfo] target(s) in 0.02s - Running `/home/pengu/test/rust-dev/gof-rs/target/debug/gof-rs` -heigt: 44 -width: 62 diff --git a/src/generation.rs b/src/generation.rs index f889c72..249754d 100644 --- a/src/generation.rs +++ b/src/generation.rs @@ -13,8 +13,11 @@ use crossterm::{ }; use rand::{thread_rng, Rng}; use std::{ + env, error::Error, - io::{self, Write}, + fs::File, + io::{self, BufRead, BufReader, Write}, + path::Path, thread::sleep, time::Duration, }; @@ -56,7 +59,7 @@ pub fn new_gen(chunk: &Rect, app: &mut App) -> Gen { let mut grid: Vec<Vec<Cell>> = Vec::new(); for _ in 0..rows { let mut row: Vec<Cell> = Vec::new(); - for _ in 0..cols{ + for _ in 0..cols { let rand = thread_rng().gen_range(0..10); row.push(cells[rand % 5]); } @@ -67,7 +70,7 @@ pub fn new_gen(chunk: &Rect, app: &mut App) -> Gen { pub fn gen_to_spans(gen: &Gen) -> Vec<Spans> { let mut spans = vec![]; - let alive_cells = vec!["🟥","🟧","🟨","🟩","🟦", "🟪" ,"🟫"]; + let alive_cells = vec!["🟥", "🟧", "🟨", "🟩", "🟦", "🟪", "🟫"]; for i in 0..gen.len() { let mut txt = String::new(); for j in 0..gen[0].len() { @@ -75,7 +78,8 @@ pub fn gen_to_spans(gen: &Gen) -> Vec<Spans> { match gen[i][j] { // Cell::Alive => print!("😎"), // Cell::Alive => txt.push_str("🦀"), - Cell::Alive => txt.push_str(alive_cells[rand % alive_cells.len()]), + // Cell::Alive => txt.push_str(alive_cells[rand % alive_cells.len()]), + Cell::Alive => txt.push_str(alive_cells[0]), Cell::Dead => txt.push_str("⬛️"), // Cell::Alive => txt.push('X'), // Cell::Dead => txt.push('-'), @@ -86,7 +90,7 @@ pub fn gen_to_spans(gen: &Gen) -> Vec<Spans> { spans } -// pub fn +// pub fn pub fn is_valid_idx(i: i32, j: i32, m: i32, n: i32) -> bool { i >= 0 && i < m && j >= 0 && j < n @@ -151,6 +155,26 @@ pub fn next_gen(app: &mut App) -> Gen { nxt_gen } +pub fn gen_from_file(path: &Path) -> Gen { + let mut gen = Gen::new(); + let file = File::open(path).expect("File not found"); + let reader = BufReader::new(file); + + for line in reader.lines() { + let line = line.unwrap(); + let mut row: Vec<Cell> = vec![]; + for ch in line.chars() { + if ch == '.' { + row.push(Cell::Dead); + } else { + row.push(Cell::Alive); + } + } + gen.push(row); + } + gen +} + pub fn init() -> Result<(), Box<dyn Error>> { // setup terminal enable_raw_mode()?; @@ -6,6 +6,7 @@ use crossterm::{ use std::{ error::Error, io, + path::Path, thread::sleep, time::{Duration, Instant}, }; @@ -73,7 +74,7 @@ impl<T> StatefulList<T> { /// Check the event handling at the bottom to see how to change the state on incoming events. /// Check the drawing logic for items on how to specify the highlighting style for selected items. pub struct App<'a> { - pub items: StatefulList<(&'a str, usize)>, + pub items: StatefulList<(&'a str, &'a Path)>, pub flag_cur: bool, pub layout: Layout, pub cur_gen: Gen, @@ -85,21 +86,30 @@ impl<'a> App<'a> { pub fn new() -> App<'a> { App { items: StatefulList::with_items(vec![ - ("Glider", 1), - ("achimsotherp16", 2), - ("achimsp11", 3), - ("achimsp144", 4), - ("achimsp16", 4), - ("achimsp4", 4), - ("achimsp5original", 4), - ("achimsp8", 4), - ("aforall", 4), - ("againstthegraingeryshep", 4), - ("aircraftcarrier", 4), - ("almostgun1", 4), - ("almostgun2", 4), - ("almostknightship", 4), - ("alternatewickstretcher1", 4), + ( + "glider", + Path::new("/home/pengu/test/rust-dev/gof-rs/presets/glider.txt"), + ), + ( + "pattern1", + Path::new("/home/pengu/test/rust-dev/gof-rs/presets/pattern1.txt"), + ), + ( + "pattern2", + Path::new("/home/pengu/test/rust-dev/gof-rs/presets/pattern2.txt"), + ), + ( + "pattern3", + Path::new("/home/pengu/test/rust-dev/gof-rs/presets/pattern3.txt"), + ), + // ( + // "pattern4", + // Path::new("/home/pengu/test/rust-dev/gof-rs/presets/pattern4.txt"), + // ), + // ( + // "pattern5", + // Path::new("/home/pengu/test/rust-dev/gof-rs/presets/pattern5.txt"), + // ), ]), flag_cur: false, layout: Layout::default() @@ -109,13 +119,6 @@ impl<'a> App<'a> { nxt_gen: Gen::new(), } } - - // Rotate through the event list. - // This only exists to simulate some kind of "progress" - // fn on_tick(&mut self) { - // let event = self.events.remove(0); - // self.events.push(event); - // } } pub fn run_app<B: Backend>( @@ -124,6 +127,7 @@ pub fn run_app<B: Backend>( tick_rate: Duration, ) -> io::Result<()> { let mut last_tick = Instant::now(); + let mut item_cnt = 0; loop { terminal.draw(|f| ui_list(f, &mut app))?; @@ -133,7 +137,12 @@ pub fn run_app<B: Backend>( match key.code { KeyCode::Char('q') => return Ok(()), KeyCode::Left | KeyCode::Char('h') => app.items.unselect(), - KeyCode::Down | KeyCode::Char('j') => app.items.next(), + KeyCode::Down | KeyCode::Char('j') => { + app.items.next(); + app.cur_gen = + gen_from_file(app.items.items[item_cnt % app.items.items.len()].1); + item_cnt += 1; + } KeyCode::Up | KeyCode::Char('k') => app.items.previous(), KeyCode::Char('n') => { terminal.draw(|f| ui_game(f, &mut app))?; @@ -148,7 +157,7 @@ pub fn run_app<B: Backend>( _ => {} } } - } + } }, _ => {} } @@ -158,9 +167,8 @@ pub fn run_app<B: Backend>( } fn ui_list<B: Backend>(f: &mut Frame<B>, app: &mut App) { - let chunks = app.layout.split(f.size()); - // Iterate through all elements in the `items` app and append some debug text to it. + let items: Vec<ListItem> = app .items .items @@ -171,7 +179,6 @@ fn ui_list<B: Backend>(f: &mut Frame<B>, app: &mut App) { }) .collect(); - // Create a List from all list items and highlight the currently selected one let items = List::new(items) .block(Block::default().borders(Borders::ALL).title("List")) .highlight_style( @@ -181,12 +188,11 @@ fn ui_list<B: Backend>(f: &mut Frame<B>, app: &mut App) { ) .highlight_symbol("> "); - // We can now render the item list f.render_stateful_widget(items, chunks[0], &mut app.items.state); - if !app.flag_cur { app.cur_gen = new_gen(&chunks[1], app); + // app.cur_gen = gen_from_file(Path::new("/home/pengu/test/rust-dev/gof-rs/presets/glider.txt")); } let spans = gen_to_spans(&app.cur_gen); @@ -205,10 +211,8 @@ fn ui_list<B: Backend>(f: &mut Frame<B>, app: &mut App) { .block(create_block(" Game Of Life ")) .alignment(Alignment::Center); f.render_widget(paragraph, chunks[1]); - } - fn ui_game<B: Backend>(f: &mut Frame<B>, app: &mut App) { let chunks = app.layout.split(f.size()); @@ -253,5 +257,4 @@ fn ui_game<B: Backend>(f: &mut Frame<B>, app: &mut App) { .block(create_block(" Game Of Life ")) .alignment(Alignment::Center); f.render_widget(paragraph, chunks[1]); - } |
