소스 검색

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);
     }
 }