浏览代码

more avoidance of the word inserter

Josh Bicking 4 年之前
父节点
当前提交
440497020e
共有 3 个文件被更改,包括 63 次插入42 次删除
  1. 57 38
      src/board.rs
  2. 3 1
      src/config.rs
  3. 3 3
      src/main.rs

+ 57 - 38
src/board.rs

@@ -18,8 +18,8 @@ use util::{copy_shuffle};
 
 mod board {}
 
-const ROWS: usize = 17;
-const COLS: usize = 24;
+const DEFAULT_ROWS: usize = 17;
+const DEFAULT_COLS: usize = 24;
 
 // Different actions are printed to the console at different speeds (the terminal displaying output, vs the player "typing")
 const TERMINAL_PRINTING_SPEED: time::Duration = time::Duration::from_millis(20);
@@ -32,7 +32,9 @@ pub struct Screen {
 }
 
 struct Board {
-    c: [char; COLS * ROWS]
+    rows: usize,
+    cols: usize,
+    c: Vec<char>
 }
 
 struct Coord {
@@ -40,35 +42,64 @@ struct Coord {
     end: usize
 }
 
-impl Default for Screen {
-    fn default() -> Screen {
-        let window = initscr();
-        noecho();
-        window_enable_colors();
-        Screen {
-            w: window,
-            attempts_left: 4,
-            b: Board{c: *garbage_board()}
+impl Board {
+    fn new(rows: Option<usize>, cols: Option<usize>) -> Board {
+        let r = match rows {
+            Some(r) => r,
+            None => DEFAULT_ROWS
+        };
+        let c = match cols {
+            Some(c) => c,
+            None => DEFAULT_COLS
+        };
+        Board {
+            rows: r,
+            cols: c,
+            c: Board::garbage_vec(r, c)
         }
     }
-}
 
-impl Board {
-    pub fn get(&self, i: usize) -> char {
+    fn garbage_vec(rows: usize, cols: usize) -> Vec<char> {
+        let g: Vec<char> = "!@#$%^*()_-=+\\|/[]{}?\"\':;,.<>".chars().collect();
+        let mut b = Vec::with_capacity(rows * cols);
+        let mut rng = rand::thread_rng();
+
+        for _ in 0..rows * cols {
+            b.push(*g.choose(&mut rng).unwrap());
+        }
+
+        b
+    }
+
+    fn get(&self, i: usize) -> char {
         self.c[i]
     }
-    pub fn getxy(&self, x: usize, y: usize) -> char {
+
+    fn getxy(&self, x: usize, y: usize) -> char {
         self.c[x*y]
     }
-    pub fn set(&mut self, i: usize, c: char) {
+
+    fn set(&mut self, i: usize, c: char) {
         self.c[i] = c;
     }
-    pub fn setxy(&mut self, x: usize, y: usize, c: char) {
+
+    fn setxy(&mut self, x: usize, y: usize, c: char) {
         self.c[x*y] = c;
     }
 }
 
 impl Screen {
+    pub fn new(conf: &Config) -> Screen {
+        let window = initscr();
+        noecho();
+        window_enable_colors();
+        Screen {
+            w: window,
+            attempts_left: 4,
+            b: Board::new(conf.rows, conf.cols)
+        }
+    }
+
     pub fn intro(&self) {
         self.w.clear();
         thread::sleep(time::Duration::from_millis(250));
@@ -89,17 +120,16 @@ impl Screen {
             false,
             TERMINAL_PRINTING_SPEED
         );
-
-        let border_hex = *border_hex_gen();
+        let border_hex = border_hex_gen(self.b.rows);
 
         let shuffled_words = copy_shuffle(&conf.words);
 
         let word_len = conf.words[0].len();
         // Assumes validate() ensured `words` is not empty, and each word is the same length
         let required_tiles = word_len * conf.words.len() + (conf.words.len() - 1);
-        let board_tiles = COLS * ROWS;
+        let board_tiles = self.b.rows * self.b.cols;
         if required_tiles > board_tiles {
-            panic!("{} words of length {} requires {} tiles, but a {} by {} board only allows for {}", conf.words.len(), word_len, required_tiles, COLS, ROWS, board_tiles);
+            panic!("{} words of length {} requires {} tiles, but an {}x{} board only allows for {} tiles", conf.words.len(), word_len, required_tiles, self.b.cols, self.b.rows, board_tiles);
         }
 
         name_this(&self.b);
@@ -141,31 +171,20 @@ fn window_enable_colors() {
     }
 }
 
-fn border_hex_gen() -> Box<[u32; ROWS * 2]> {
+fn border_hex_gen(rows: usize) -> Vec<u32> {
     // Build the hex values, printed alongside the text and garbage.
+    // The vector will be of length rows * 2
     // TODO: this would make a nice generator, once those are stable.
     let mut rng = rand::thread_rng();
     let mut hex = rng.gen_range(0,200) + 63744;
 
-    let mut ls: [u32; ROWS * 2] = [0; ROWS * 2];
-    for i in 0..ROWS * 2 {
+    let mut ls = Vec::with_capacity(rows * 2);
+    for i in 0..rows * 2 {
         ls[i] = hex;
         hex += 12;
     }
 
-    Box::new(ls)
-}
-
-fn garbage_board() -> Box<[char; COLS * ROWS]> {
-    let g: Vec<char> = "!@#$%^*()_-=+\\|/[]{}?\"\':;,.<>".chars().collect();
-    let mut b: [char; COLS * ROWS] = ['\0'; COLS * ROWS];
-    let mut rng = rand::thread_rng();
-
-    for i in 0..ROWS * COLS {
-            b[i] = *g.choose(&mut rng).unwrap();
-    }
-
-    Box::new(b)
+    ls
 }
 
 // Think of the board in word_len "chunks": place a word in each chunk. Then "shake" each one around a bit, in a random order, 3 times/word.

+ 3 - 1
src/config.rs

@@ -14,7 +14,9 @@ mod config {}
 #[derive(Debug, PartialEq, Deserialize)]
 pub struct Config {
     pub words: Vec<String>,
-    pub choose: u32
+    pub choose: u32,
+    pub rows: Option<usize>,
+    pub cols: Option<usize>
 }
 
 // const CONFIG_FOLDERS: &'static [&'static str] = &["XDG_CONFIG_HOME", "HOME"];

+ 3 - 3
src/main.rs

@@ -30,12 +30,12 @@ fn main() {
         panic!("No default config file found, and no config file path specified")
     }
 
-    let config = config::load_config_file(config_location);
+    let conf = config::load_config_file(config_location);
 
-    let screen: board::Screen = Default::default();
+    let screen = board::Screen::new(&conf);
     screen.intro();
 
     screen.w.clear();
 
-    screen.initialize_game(config);
+    screen.initialize_game(conf);
 }