# HG changeset patch # User dermetfan # Date 1435317291 -7200 # Fri Jun 26 13:14:51 2015 +0200 # Node ID 71a3def45471eed9c1ad87ac53441e28d27026d3 # Parent e67447d53abdabca02b16c059e3179aafeaf428d start paused and randomize arg diff --git a/src/main.rs b/src/main.rs --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,8 @@ use ncurses::*; fn main() { + const START_PAUSED: bool = true; + const BG_COLORS: i16 = 0; const CURSOR_COLORS: i16 = 1; @@ -18,7 +20,7 @@ initscr(); cbreak(); noecho(); - nodelay(stdscr, true); + nodelay(stdscr, !START_PAUSED); keypad(stdscr, true); curs_set(CURSOR_VISIBILITY::CURSOR_INVISIBLE); start_color(); @@ -42,6 +44,7 @@ const ARG_DEAD: &'static str = "dead"; const ARG_ALIVE: &'static str = "alive"; const ARG_BORDER: &'static str = "border"; + const ARG_RAND: &'static str = "randomize"; let mut options = getopts::Options::new(); options.optflag("h", ARG_HELP, "display help and exit"); @@ -51,6 +54,7 @@ options.optopt("d", ARG_DEAD, "the character to display for dead cells", "CHAR"); options.optopt("a", ARG_ALIVE, "the character to display for living cells", "CHAR"); options.optopt("b", ARG_BORDER, "Draw a border around the grid. Valid TYPEs are: blank, single (default), double, strong, thin, thick, solid", "TYPE"); + options.optopt("x", ARG_RAND, "initialize the grid randomly with CHANCE for life in each cell ([0..1], default 0.5)", "CHANCE"); let print_help = |short: bool| { const TITLE: &'static str = "cursedlife\nCopyright 2015 Robin Stumm\nhttp://dermetfan.net"; @@ -160,15 +164,21 @@ }; let mut cells = vec![vec![false; cols]; rows]; - for row in &mut cells[..] { - for col in &mut row[..] { - *col = rand::random::(); + if matches.opt_present(ARG_RAND) { + let chance = match matches.opt_str(ARG_RAND) { + Some(chance) => chance.parse::().unwrap_or(0f32), + None => 0.5f32 + }; + for row in &mut cells[..] { + for col in &mut row[..] { + *col = rand::random::() < chance; + } } } let mut game = Game { cells: cells, dead: dead, alive: alive, border: border, color_pair: BG_COLORS, window: newwin(rows as i32, cols as i32, maxy / 2 - rows as i32 / 2, maxx / 2 - cols as i32 / 2) }; let mut cursor = Cursor { y: 0, x: 0, color_pair: CURSOR_COLORS }; - let mut paused = false; + let mut paused = START_PAUSED; 'game_loop: loop { let before_render = time::precise_time_s();