|
@@ -18,8 +18,8 @@ use util::{copy_shuffle};
|
|
|
|
|
|
mod board {}
|
|
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")
|
|
// 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 TERMINAL_PRINTING_SPEED: time::Duration = time::Duration::from_millis(20);
|
|
@@ -32,7 +32,9 @@ pub struct Screen {
|
|
}
|
|
}
|
|
|
|
|
|
struct Board {
|
|
struct Board {
|
|
- c: [char; COLS * ROWS]
|
|
|
|
|
|
+ rows: usize,
|
|
|
|
+ cols: usize,
|
|
|
|
+ c: Vec<char>
|
|
}
|
|
}
|
|
|
|
|
|
struct Coord {
|
|
struct Coord {
|
|
@@ -40,35 +42,64 @@ struct Coord {
|
|
end: 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 {
|
|
|
|
+ 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]
|
|
self.c[i]
|
|
}
|
|
}
|
|
- pub fn getxy(&self, x: usize, y: usize) -> char {
|
|
|
|
|
|
+
|
|
|
|
+ fn getxy(&self, x: usize, y: usize) -> char {
|
|
self.c[x*y]
|
|
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;
|
|
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;
|
|
self.c[x*y] = c;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
impl Screen {
|
|
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) {
|
|
pub fn intro(&self) {
|
|
self.w.clear();
|
|
self.w.clear();
|
|
thread::sleep(time::Duration::from_millis(250));
|
|
thread::sleep(time::Duration::from_millis(250));
|
|
@@ -89,17 +120,16 @@ impl Screen {
|
|
false,
|
|
false,
|
|
TERMINAL_PRINTING_SPEED
|
|
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 shuffled_words = copy_shuffle(&conf.words);
|
|
|
|
|
|
let word_len = conf.words[0].len();
|
|
let word_len = conf.words[0].len();
|
|
// Assumes validate() ensured `words` is not empty, and each word is the same length
|
|
// 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 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 {
|
|
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);
|
|
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.
|
|
// 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.
|
|
// TODO: this would make a nice generator, once those are stable.
|
|
let mut rng = rand::thread_rng();
|
|
let mut rng = rand::thread_rng();
|
|
let mut hex = rng.gen_range(0,200) + 63744;
|
|
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;
|
|
ls[i] = hex;
|
|
hex += 12;
|
|
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.
|
|
// 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.
|