Forráskód Böngészése

Refactored Anygroup::display. Updated ksudoku code.

I'm adding notes on what I want from the ksudoku load and save.
I've got the values I need, just need to return them.
Steve Thielemann 4 hónapja
szülő
commit
204e970f2c
2 módosított fájl, 84 hozzáadás és 2 törlés
  1. 32 0
      sudoku/src/group.rs
  2. 52 2
      sudoku/src/ksudoku.rs

+ 32 - 0
sudoku/src/group.rs

@@ -105,46 +105,78 @@ impl AnyGroup {
         }
     }
 
+    // Possibly update this so it shows +1 on x and y
+    // to match actual positions (rather then starting at 0).
+
     pub fn display(&self) {
         let max_index_size = find_number_width(self.width as usize * self.width as usize);
+        let max_pos_size = find_number_width(self.width as usize);
+
+        let printer = |row:u8, data:&[usize]| {
+            print!("[{:2}: ", row);
+            for j in data {
+                let xy = self.xy(*j);
+                print!("{0:1$}({3:2$},{4:2$}) ", j, max_index_size, max_pos_size, xy.0+1, xy.1+1);
+            }
+            println!("");
+        };
 
         println!("rows:");
         // println!("{:?}", self.row);
         for i in 0..self.width {
             let r = self.row(i);
+            /*
             print!("[{:2}]: ", i);
             for j in r {
                 let xy = self.xy(*j);
                 print!("{0:1$}({2:2},{3:2}) ", j, max_index_size, xy.0, xy.1);
             }
             println!("");
+            */
+            printer(i, r);
         }
         println!("columns:");
         // println!("{:?}", self.column);
         for i in 0..self.width {
             let r = self.column(i);
+            /*
             print!("[{:2}]: ", i);
             for j in r {
                 let xy = self.xy(*j);
                 print!("{0:1$}({2:2},{3:2}) ", j, max_index_size, xy.0, xy.1);
             }
             println!("");
+            */
+            printer(i, r);
         }
 
         println!("cells:");
         // println!("{:?}", self.cell);
         for i in 0..self.width {
             let r = self.cell(i);
+            /*
             print!("[{:2}]: ", i);
             for j in r {
                 let xy = self.xy(*j);
                 print!("{0:1$}({2:2},{3:2}) ", j, max_index_size, xy.0, xy.1);
             }
             println!("");
+            */
+            printer(i, r);
         }
     }
 }
 
+/*
+  ___  _     _    ____          _      
+ / _ \| | __| |  / ___|___   __| | ___ 
+| | | | |/ _` | | |   / _ \ / _` |/ _ \
+| |_| | | (_| | | |__| (_) | (_| |  __/
+ \___/|_|\__,_|  \____\___/ \__,_|\___|
+                                       
+Static tables for 3x3 boards only...
+ */
+
 /// Width of the sudoku board.
 const WIDTH: u8 = 9;
 // /// Size (width * height) of the board.

+ 52 - 2
sudoku/src/ksudoku.rs

@@ -21,13 +21,47 @@ impl fmt::Display for UnsupportedGroup {
 
 impl error::Error for UnsupportedGroup {}
 
+/*
+What I want from load_ksudoku:
+
+  - Puzzle string
+  - Solution string (so I can validate it/check my code)
+  - specific-type & type (combined as a string is ok - I think)
+  - order (as a number)
+
+I get values (the puzzle), I also need solution.
+
+Having order will allow me to use AnyBoard::new(order) and load it up.  
+
+Currently, it loads, and will fail if it isn't Plain, sudoku, 9.
+I'm not sure what future ksudoku puzzles I want to support, so I'd like to
+have that as flexible as possible.
+
+save_ksudoku, would need to save those fields as well...
+ */
+
+ pub struct Ksudoku {
+    pub puzzle :String,
+    pub solution: String,
+    pub specific_type: String,
+    pub puzzle_type: String,
+    pub order: u8,
+ }
+
 pub fn load_ksudoku(filename: std::path::PathBuf) -> Result<String, Box<dyn error::Error>> {
     let fh = File::open(filename)?;
     let buffer = BufReader::new(fh);
     let parser = EventReader::new(buffer);
+
+    let mut element_name: String = String::new();    
+
     // I'm interested in values and (maybe) solution
-    let mut values: String = String::from("");
-    let mut element_name: String = String::from("");
+    let mut values: String = String::new();
+    let mut _solution = String::new();
+    let mut _specific_type = String::new();
+    let mut _puzzle_type = String::new();
+    let mut _order = String::new();
+    let mut _order_value: u8 = 0;
 
     for e in parser {
         match e {
@@ -49,6 +83,19 @@ pub fn load_ksudoku(filename: std::path::PathBuf) -> Result<String, Box<dyn erro
 
                     let blank = String::new();
 
+                    _specific_type = attrs.get(&"specific-type".to_string()).unwrap_or(&blank).clone();
+                    _puzzle_type = attrs.get(&"type".to_string()).unwrap_or(&blank).clone();
+                    _order = attrs.get(&"order".to_string()).unwrap_or(&blank).clone();
+
+                    if !_order.is_empty() {
+                        let r = _order.parse::<u8>();
+                        if r.is_ok() {
+                            _order_value = r.unwrap();
+                        } else {
+                            // Failed to convert order to u8...
+                            return Err(Box::new(r.unwrap_err()));
+                        }
+                    }
                     // Format in specific order here.
                     let expected = format!(
                         "{}-{}-{}",
@@ -71,6 +118,9 @@ pub fn load_ksudoku(filename: std::path::PathBuf) -> Result<String, Box<dyn erro
                 if element_name == "values" {
                     values = text.clone();
                 }
+                if element_name == "solution" {
+                    _solution = text.clone();
+                }
             }
             Err(e) => {
                 return Err(Box::new(e));