Josh Bicking 4 éve
szülő
commit
fe91911ca5
3 módosított fájl, 73 hozzáadás és 26 törlés
  1. 59 22
      src/board.rs
  2. 5 4
      src/main.rs
  3. 9 0
      src/util.rs

+ 59 - 22
src/board.rs

@@ -12,6 +12,7 @@ use self::rand::prelude::SliceRandom;
 use std::{thread, time};
 
 use config::{Config};
+use util::{copy_shuffle};
 
 // Board control and interaction. The view & controller.
 
@@ -20,23 +21,54 @@ mod board {}
 const ROWS: usize = 17;
 const COLS: usize = 24;
 
-pub struct Board {
+// 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);
+const TYPING_SPEED: time::Duration = time::Duration::from_millis(70);
+
+pub struct Screen {
     pub w: Window,
-    attempts_left: u8
+    attempts_left: u8,
+    b: Board
 }
 
-pub fn init_board() -> Board {
-    let window = initscr();
-    noecho();
-    window_enable_colors();
-    Board{w: window, attempts_left: 4}
+struct Board {
+    c: [char; COLS * ROWS]
 }
 
-// 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);
-const TYPING_SPEED: time::Duration = time::Duration::from_millis(70);
+struct Coord {
+    start: usize,
+    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 {
+    pub fn get(&self, i: usize) -> char {
+        self.c[i]
+    }
+    pub fn getxy(&self, x: usize, y: usize) -> char {
+        self.c[x*y]
+    }
+    pub fn set(&mut self, i: usize, c: char) {
+        self.c[i] = c;
+    }
+    pub fn setxy(&mut self, x: usize, y: usize, c: char) {
+        self.c[x*y] = c;
+    }
+}
+
+impl Screen {
     pub fn intro(&self) {
         self.w.clear();
         thread::sleep(time::Duration::from_millis(250));
@@ -59,10 +91,18 @@ impl Board {
         );
 
         let border_hex = *border_hex_gen();
-        let mut b = *garbage_board();
 
         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;
+        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);
+        }
+
+        name_this(&self.b);
         // TODO: insert each word, of size N, into the array, with at least 1 character between each
 
     }
@@ -116,23 +156,20 @@ fn border_hex_gen() -> Box<[u32; ROWS * 2]> {
     Box::new(ls)
 }
 
-fn garbage_board() -> Box<[[char; COLS]; ROWS]> {
+fn garbage_board() -> Box<[char; COLS * ROWS]> {
     let g: Vec<char> = "!@#$%^*()_-=+\\|/[]{}?\"\':;,.<>".chars().collect();
-    let mut b: [[char; COLS]; ROWS] = [['\0'; COLS]; ROWS];
+    let mut b: [char; COLS * ROWS] = ['\0'; COLS * ROWS];
     let mut rng = rand::thread_rng();
 
-    for i in 0..ROWS {
-        for j in 0..COLS {
-            b[i][j] = *g.choose(&mut rng).unwrap();
-        }
+    for i in 0..ROWS * COLS {
+            b[i] = *g.choose(&mut rng).unwrap();
     }
 
     Box::new(b)
 }
 
-fn copy_shuffle<T: Clone>(vec: &Vec<T>) -> Vec<T> {
-    let mut rng = rand::thread_rng();
-    let mut vec = vec.clone();
-    vec.shuffle(&mut rng);
-    vec
+// 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.
+fn name_this(b: &Board) -> Vec<Coord> {
+    // TODO
+    return Vec::new();
 }

+ 5 - 4
src/main.rs

@@ -2,6 +2,7 @@ extern crate argparse;
 
 mod config;
 mod board;
+mod util;
 
 use self::argparse::{ArgumentParser, Store};
 
@@ -31,10 +32,10 @@ fn main() {
 
     let config = config::load_config_file(config_location);
 
-    let board = board::init_board();
-    board.intro();
+    let screen: board::Screen = Default::default();
+    screen.intro();
 
-    board.w.clear();
+    screen.w.clear();
 
-    board.initialize_game(config);
+    screen.initialize_game(config);
 }

+ 9 - 0
src/util.rs

@@ -0,0 +1,9 @@
+extern crate rand;
+use self::rand::prelude::SliceRandom;
+
+pub fn copy_shuffle<T: Clone>(vec: &Vec<T>) -> Vec<T> {
+    let mut rng = rand::thread_rng();
+    let mut vec = vec.clone();
+    vec.shuffle(&mut rng);
+    vec
+}