Explorar o código

Track if something removed/found.

Steve Thielemann hai 9 meses
pai
achega
0cbf69eea8
Modificáronse 1 ficheiros con 32 adicións e 23 borrados
  1. 32 23
      sudoku/src/sudoku.rs

+ 32 - 23
sudoku/src/sudoku.rs

@@ -152,6 +152,8 @@ pub const fn xy(pos: u8) -> (u8, u8) {
     ((pos % WIDTH), (pos / WIDTH))
 }
 
+const DEBUG_OUTPUT: bool = false;
+
 /*
 (0 .. 10)
                         .map(|_| HashSet::<usize>::new())
@@ -341,13 +343,15 @@ impl Sudoku {
     }
 
     fn calculate_possible(&mut self, solutions: &mut u16) -> bool {
-         for idx in 0..MAX_SIZE {
+        for idx in 0..MAX_SIZE {
             if self.board[idx as usize] == 0 {
                 // Ok, there's a blank here
 
                 let (x, y) = xy(idx);
-                println!("idx={} ({},{})", idx, x, y);
-                self.display();
+                if DEBUG_OUTPUT {
+                    println!("idx={} ({},{})", idx, x, y);
+                    self.display();
+                }
 
                 'outer: for possible in 1..=9 {
                     let mut g = for_row(y);
@@ -372,7 +376,9 @@ impl Sudoku {
                     }
 
                     // Ok, it could go here!
-                    println!("({},{})={}", x, y, possible);
+                    if DEBUG_OUTPUT {
+                        println!("({},{})={}", x, y, possible);
+                    }
 
                     self.board[idx as usize] = possible;
                     if self.puzzle_complete() {
@@ -399,12 +405,12 @@ impl Sudoku {
         let mut workset = Sudoku {
             board: self.board,
             possible: [Possible(0); MAX_SIZE as usize],
-        }; // self.possible };
+        };
 
         let mut solutions: u16 = 0;
         workset.calculate_possible(&mut solutions);
 
-        // find number of solutions.
+        // return number of solutions.
         solutions
     }
 
@@ -491,7 +497,9 @@ impl Sudoku {
                 let value = pick_one(self, &mut rng, i);
                 if value.is_some() {
                     let value = value.unwrap();
-                    println!("Set({},{})={}", x, y, value);
+                    if DEBUG_OUTPUT {
+                        println!("Set({},{})={}", x, y, value);
+                    }
                     self.set(x, y, value);
                 }
             }
@@ -554,7 +562,7 @@ impl Sudoku {
             }
         };
 
-        // Change to 0..SIZE ...  Keep it simple.
+        // Change to 0..WIDTH ...  Keep it simple.
         for i in 0..WIDTH {
             let mut g = for_column(i);
             // g.for_column(i, 1);
@@ -598,7 +606,7 @@ impl Sudoku {
 
                                 let mut pair_removed = false;
 
-                                // It isn't currently possible to tell if anything was removed...
+                                // Check to see if anything was removed.
 
                                 for remove in 0..WIDTH {
                                     if (gidx == remove) || (fidx == remove) {
@@ -612,21 +620,11 @@ impl Sudoku {
                                         pair_removed = true;
                                     }
 
-                                    /* ).is_some() {
-                                        found_something = true;
-                                        pair_removed = true;
-                                    };
-                                    */
                                     if self.possible[rpos as usize].get(values[1]) {
                                         self.possible[rpos as usize].set(values[1], false);
                                         found_something = true;
                                         pair_removed = true;
                                     }
-                                    /*.is_some() {
-                                        found_something = true;
-                                        pair_removed = true;
-                                    };
-                                    */
                                 }
 
                                 if pair_removed {
@@ -655,16 +653,27 @@ impl Sudoku {
                                         if (remove == xy(gpos).1) || (remove == xy(fpos).1) {
                                             continue;
                                         }
-                                        self.possible[pos(column, remove) as usize]
-                                            .set(values[0], false);
+
+                                        if self.possible[pos(column, remove) as usize]
+                                            .get(values[0])
+                                        {
+                                            self.possible[pos(column, remove) as usize]
+                                                .set(values[0], false);
+                                            found_something = true;
+                                        }
                                         /* v
                                             .is_some()
                                         {
                                             found_something = true;
                                         };
                                         */
-                                        self.possible[pos(column, remove) as usize]
-                                            .set(values[1], false);
+                                        if self.possible[pos(column, remove) as usize]
+                                            .get(values[1])
+                                        {
+                                            self.possible[pos(column, remove) as usize]
+                                                .set(values[1], false);
+                                            found_something = true;
+                                        }
                                         /*
                                             .is_some()
                                         {