Browse Source

refactor and prepare solve_by_unit_check

mloeschenkohl 2 weeks ago
parent
commit
fc3754d20c
1 changed files with 62 additions and 20 deletions
  1. 62 20
      sudoku_solver.py

+ 62 - 20
sudoku_solver.py

@@ -2,18 +2,20 @@
 # Enthält diese Liste nur einen Wert, ist das der gesuchte Wert für das Feld.
 
 import logging
-logging.basicConfig(level=logging.INFO)
+logging.basicConfig(level=logging.INFO) # change to DEBUG if needed
 
 gamefield = [
-    [[],[7],[],[5],[8],[3],[],[2],[]],
-    [[],[5],[9],[2],[],[],[3],[],[]],
-    [[3],[4],[],[],[],[6],[5],[],[7]],
-    [[7],[9],[5],[],[],[],[6],[3],[2]],
-    [[],[],[3],[6],[9],[7],[1],[],[]],
-    [[6],[8],[],[],[],[2],[7],[],[]],
-    [[9],[1],[4],[8],[3],[5],[],[7],[6]],
-    [[],[3],[],[7],[],[1],[4],[9],[5]],
-    [[5],[6],[7],[4],[2],[9],[],[1],[3]]
+    [[],[],[],      [5],[],[7],       [],[],[]],
+    [[],[4],[],      [2],[6],[3],       [],[],[]],
+    [[1],[],[7],      [4],[],[],       [],[],[]],
+    
+    [[3],[6],[],      [],[],[],       [],[4],[5]],
+    [[],[],[2],      [],[5],[],       [7],[],[]],
+    [[7],[9],[],      [],[],[],       [],[6],[2]],
+
+    [[],[],[],      [],[],[9],       [4],[],[1]],
+    [[],[],[],      [1],[3],[4],       [],[9],[]],
+    [[],[],[],      [6],[],[5],       [],[],[]]
 ];
 
 def fill_empty_fields_with_possible_values():
@@ -50,14 +52,51 @@ def print_sudoku():
         print('')
     print('')
 
-def solve_by_cell_check():
-    logging.debug("solve_by_cell_check")
+def solve_sudoku():
+    logging.debug("solve_sudoku")
+    # always repeat everything from the beginning if something is found
     while True:
-        if solve_by_cell_check_inner():
-            # break when algorithm runs through without any findings
-            break
+        if not solve_by_cell_check():
+            continue
+        if not solve_by_unit_check():
+            continue
+        # break when every algorithm runs through without any findings
+        break
 
-def solve_by_cell_check_inner():
+def solve_by_unit_check():
+    '''checks each row/cell/3x3 if 1-9 is the only possible value for this unit'''
+    logging.debug("solve_by_unit_check_inner")
+    # check for each row. check for 1 to 9 if it is the only possible value for this unit. if yes, set. it and restart
+    for row in gamefield:
+        field_changed = False
+        v = 1
+        while True:
+            count = 0
+            foo_field = []
+            for field in row:
+                if v in field:
+                    count += 1
+                    foo_field = field
+            if count == 1 and len(foo_field) > 1:
+                foo_field.clear
+                foo_field.append(v)
+                field_changed = True
+                break
+            v += 1
+            if v == 9:
+                break
+    
+    # check for each column. check for 1 to 9 if it is the only possible value for this unit. if yes, set. it and restart
+    # TODO
+    # check for each 3x3 field. check for 1 to 9 if it is the only possible value for this unit. if yes, set. it and restart
+    # TODO
+    
+    # return True when algorithm runs through without any findings
+    # return False when algorithm found something
+    return not field_changed
+
+def solve_by_cell_check():
+    '''checks each cell to remove possible values'''
     logging.debug("solve_by_cell_check_inner")
     # for each cell
     y = 0
@@ -144,7 +183,10 @@ def get_3x3_start_coords(x, y):
 def get_field_by_coord(x, y):
     return gamefield[y][x]
 
-fill_empty_fields_with_possible_values()
-print_sudoku()
-solve_by_cell_check()
-print_sudoku()
+def main():
+    fill_empty_fields_with_possible_values()
+    print_sudoku()
+    solve_sudoku()
+    print_sudoku()
+
+main()