Browse Source

build the screen buffer

Josh Bicking 4 years ago
parent
commit
927188c105
2 changed files with 36 additions and 20 deletions
  1. 21 9
      src/board.rs
  2. 15 11
      src/grid.rs

+ 21 - 9
src/board.rs

@@ -10,7 +10,6 @@ use self::pancurses::{
 
 extern crate rand;
 use self::rand::Rng;
-use self::rand::prelude::SliceRandom;
 
 use std::{thread, time};
 
@@ -73,20 +72,16 @@ impl Screen {
         let shuffled_words = copy_shuffle(&conf.words);
         let words = &shuffled_words[..conf.choose];
 
-        let coord = self.board.randomly_add_words(words);
+        let coords = self.board.randomly_add_words(words);
 
         // The board of words + garbage characters has been built: now the screen must be displayed.
-        // Take the board
-
         let mut screen_buffer = Grid::new(Some(self.board.rows), Some(self.board.cols + (2 * BORDER_HEX_WIDTH) + 3));
         let border_hex = border_hex_gen(self.board.rows, self.board.cols);
         self.add_border_hex_to_grid(&mut screen_buffer, &border_hex);
         self.add_board_to_grid(&mut screen_buffer);
 
         // Debugging
-        endwin();
-        trace!("Words:\n{}", self.board.dump());
-        trace!("Hex:\n{}", screen_buffer.dump());
+        trace!("Screen:\n{}", screen_buffer.dump());
     }
 
     pub fn end_window(&self) {
@@ -125,7 +120,7 @@ impl Screen {
         }
 
         // Half the board, plus characters for the first hex values, plus 2 spaces of padding.
-        let right_hand_offset = grid.cols / 2;
+        let right_hand_offset = (grid.cols / 2) + 1;
         for (row, hex_val) in right_side {
             trace!("Printing 0x{:x} at {}, {}", hex_val, row, right_hand_offset);
             grid.set_rowcol_str(row, right_hand_offset, &format!("0x{:x}", hex_val));
@@ -136,6 +131,23 @@ impl Screen {
         // TODO
         // The board is pressed to the right of each column
         // Where to start placing the board on each row: (grid.cols / 2) - (self.board.cols / 2)
+
+        // TODO this should dynamically check size
+
+        let left_side_start = (grid.cols / 2) - (self.board.cols / 2);
+        let right_side_start = (grid.cols) - (self.board.cols / 2);
+
+        let mut board_iter = self.board.iter();
+        for row in 0..self.board.rows {
+            for col in 0..(self.board.cols / 2) {
+                grid.set_rowcol(row, left_side_start + col, *board_iter.next().unwrap());
+            }
+        }
+        for row in 0..self.board.rows {
+            for col in 0..(self.board.cols / 2) {
+                grid.set_rowcol(row, right_side_start + col, *board_iter.next().unwrap());
+            }
+        }
     }
 
 }
@@ -150,7 +162,7 @@ fn window_enable_colors() {
 fn border_hex_gen(rows: usize, cols: 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.
+    // TODO: this would make a nice iterator, or generator, once those are stable.
     let mut rng = rand::thread_rng();
     let mut hex = rng.gen_range(0,200) + 63744;
 

+ 15 - 11
src/grid.rs

@@ -100,6 +100,21 @@ impl Grid {
         coords
     }
 
+    pub fn iter(&self) -> std::slice::Iter<char> {
+        return self.c.iter();
+    }
+
+    pub fn dump(&self) -> String {
+        let mut s = String::with_capacity(self.rows * self.cols);
+        for row in 0..self.rows {
+            for col in 0..self.cols {
+                s.push(self.get_rowcol(row, col));
+            }
+            s.push('\n');
+        }
+        s
+    }
+
     // Think of the board in word_len "chunks": place a slot for a word in each chunk
     fn generate_word_coords(&self, num_of_words: usize, word_len: usize) -> Vec<Coord> {
         let chunk_len = self.capacity() / num_of_words;
@@ -186,15 +201,4 @@ impl Grid {
             self.set_i_str(coord.start, word);
         }
     }
-
-    pub fn dump(&self) -> String {
-        let mut s = String::with_capacity(self.rows * self.cols);
-        for row in 0..self.rows {
-            for col in 0..self.cols {
-                s.push(self.get_rowcol(row, col));
-            }
-            s.push('\n');
-        }
-        s
-    }
 }