Explorar el Código

cargo test working in sudoku module again.

Steve Thielemann hace 9 meses
padre
commit
f14668a057
Se han modificado 3 ficheros con 63 adiciones y 40 borrados
  1. 2 32
      src/main.rs
  2. 4 3
      sudoku/src/group.rs
  3. 57 5
      sudoku/src/sudoku.rs

+ 2 - 32
src/main.rs

@@ -19,15 +19,9 @@ fn main() {
 
     let puzzle = load_ksudoku(args.filename).unwrap();
 
-    /*
-    let filename = args.filename; // "../puzzle1";
-    let fh = File::open(filename).unwrap();
-    let puzzle: Ksudoku = from_reader(fh).unwrap();
-    println!("Puzzle: {:?}", puzzle);
-    */
-
     let mut s = Sudoku::new();
-    s.load_from(puzzle.as_str());
+    // Ksudoku is stored TLD.
+    s.load_from_tld('b', '_', puzzle.as_str());
     s.display();
     s.display_possible();
 
@@ -37,28 +31,4 @@ fn main() {
         s.display_possible();
     }
 
-    /*
-    s.display();
-    s.display_possible();
-    */
-
-    /*
-    let mut g = Group::new();
-    g.for_row(1, 1);
-    print!("Row: ");
-    println!("{:?}", g);
-    println!("{:#?}", g);
-    // g.display();
-    print!("Col: ");
-    g.for_column(1, 1);
-    println!("{:?}", g);
-    // g.display();
-    println!("Blk testing output: ");
-
-    for i in 0..SIZE {
-        g.for_block(i, i);
-        println!("({},{} {:#?})", i, i, g);
-        // g.display();
-    }
-    */
 }

+ 4 - 3
sudoku/src/group.rs

@@ -5,8 +5,8 @@ use crate::*;
 
 /// Width of the sudoku board.
 const WIDTH: u8 = 9;
-/// Size (width * height) of the board.
-const MAX_SIZE: u8 = 81;
+// /// Size (width * height) of the board.
+// const MAX_SIZE: u8 = 81;
 
 // #[derive(Debug)]
 /// Define a Group of sudoku positions.
@@ -420,7 +420,8 @@ pub fn which_cell(x:u8, y:u8) -> u8 {
 
 #[cfg(test)]
 mod tests {
-    use crate::*;
+    // use crate::sudoku::*;
+    use crate::group::*;
     // use crate::sudoku::group::*;
     // use crate::Group;
     // use group::*;

+ 57 - 5
sudoku/src/sudoku.rs

@@ -2,6 +2,7 @@
 use crate::group::*;
 
 use std::collections::HashSet;
+use std::string::String;
 
 /// Width of the sudoku board.
 const WIDTH: u8 = 9;
@@ -67,15 +68,17 @@ impl Sudoku {
         }
     }
 
-    pub fn load_from(&mut self, s: &str) {
+    /// Load puzzle from a string.
+    /// Note, we load from (top,left), to (bottom,left) by columns.
+    pub fn load_from_tld(&mut self, start_ch: char, blank: char, s: &str) {
         self.clear();
         let mut x: u8 = 0;
         let mut y: u8 = 0;
+
         for ch in s.chars() {
-            if ch >= 'b' {
-                self.set(x, y, ch as u8 - 'a' as u8);
-                // self.board[pos(x, y) as usize] = ch as u8 - 'a' as u8;
-            };
+            if ch != blank {
+                self.set(x,y, (ch as u8-start_ch as u8) +1);
+            }
             y += 1;
             if y >= WIDTH {
                 y = 0;
@@ -84,6 +87,55 @@ impl Sudoku {
         }
     }
 
+    /// Load puzzle from a string.
+    /// This loads from (top,left) to (top,right), by rows.
+    pub fn load_from_tlr(&mut self, start_ch: char, blank: char, s: &str) {
+        self.clear();
+        let mut i: u8 = 0;
+
+        for ch in s.chars() {
+            if ch != blank {
+                self.set(xy(i).0, xy(i).1, (ch as u8-start_ch as u8) +1);
+            }
+            i += 1;
+        }
+    }
+
+    pub fn save_to_tld(&self, mut start_ch:char, blank: char) -> String {
+        let mut result = String::new();
+        result.reserve(MAX_SIZE as usize);
+        start_ch = (start_ch as u8 -1) as char;
+        let mut x:u8 = 0;
+        let mut y:u8 = 0;
+
+        for i in 0..MAX_SIZE {
+            if self.board[pos(x,y) as usize] == 0 {
+                result.push(blank);
+            } else {
+                result.push((start_ch as u8 + self.board[i as usize]) as char);
+            }
+            y += 1;
+            if y >= WIDTH {
+                y = 0;
+                x +=1;
+            }
+        }
+        result
+    }
+
+    pub fn save_to_tlr(&self, mut start_ch:char, blank: char) -> String {
+        let mut result = String::new();
+        result.reserve(MAX_SIZE as usize);
+        start_ch = (start_ch as u8 -1) as char;
+        for i in 0..MAX_SIZE {
+            if self.board[i as usize] == 0 {
+                result.push(blank);
+            } else {
+                result.push((start_ch as u8 + self.board[i as usize]) as char);
+            }
+        }
+        result
+    }
     pub fn set(&mut self, x: u8, y: u8, value: u8) {
         self.board[pos(x, y) as usize] = value;
         // Ok, update the possible