浏览代码

Display formatting fixed for 4 or 5.

Steve Thielemann 4 月之前
父节点
当前提交
0360010958
共有 1 个文件被更改,包括 21 次插入6 次删除
  1. 21 6
      sudoku/src/group.rs

+ 21 - 6
sudoku/src/group.rs

@@ -20,6 +20,17 @@ pub struct AnyGroup {
     pub cell: Vec<usize>,
 }
 
+/// Find the number of digits needed to display given number.
+fn find_number_width(mut number:usize) -> usize {
+    let mut size:usize = 1;
+    number /= 10;
+    while number > 0 {
+        size += 1;
+        number /= 10;
+    }
+    size
+}
+
 impl AnyGroup {
     pub fn new(board_size: u8) -> Self {
         let width = board_size as usize * board_size as usize;
@@ -102,37 +113,39 @@ impl AnyGroup {
     }
 
     pub fn display(&self) {
+        let max_index_size = find_number_width(self.width as usize * self.width as usize);
+
         println!("rows:");
-        println!("{:?}", self.row);
+        // 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!("{:2}({:2},{:2}) ", j, xy.0, xy.1);
+                print!("{0:1$}({2:2},{3:2}) ", j, max_index_size, xy.0, xy.1);
             }
             println!("");
         }
         println!("columns:");
-        println!("{:?}", self.column);
+        // 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!("{:2}({:2},{:2}) ", j, xy.0, xy.1);
+                print!("{0:1$}({2:2},{3:2}) ", j, max_index_size, xy.0, xy.1);
             }
             println!("");
         }
 
         println!("cells:");
-        println!("{:?}", self.cell);
+        // 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!("{:2}({:2},{:2}) ", j, xy.0, xy.1);
+                print!("{0:1$}({2:2},{3:2}) ", j, max_index_size, xy.0, xy.1);
             }
             println!("");
         }
@@ -551,6 +564,8 @@ mod tests {
     fn check_dynamic() {
         let g = AnyGroup::new(3);
         g.display();
+        let g = AnyGroup::new(4);
+        g.display();
         assert!(false);
     }
 }