|
@@ -0,0 +1,83 @@
|
|
|
+# Sudoku: 9x9 Spielfeld. Auf jedem Feld ist eine Liste mit möglichen Werten.
|
|
|
+# Enthält diese Liste nur einen Wert, ist das der gesuchte Wert für das Feld.
|
|
|
+
|
|
|
+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]]
|
|
|
+];
|
|
|
+
|
|
|
+def fill_empty_fields_with_possible_values():
|
|
|
+ y = 0
|
|
|
+ for row in gamefield:
|
|
|
+ x = 0
|
|
|
+ for field in row:
|
|
|
+ if(len(field) == 0):
|
|
|
+ field = [1,2,3,4,5,6,7,8,9]
|
|
|
+ row[x] = field
|
|
|
+ x = x + 1
|
|
|
+ gamefield[y] = row
|
|
|
+ y = y + 1
|
|
|
+
|
|
|
+def print_sudoku():
|
|
|
+ for row in gamefield:
|
|
|
+ for field in row:
|
|
|
+ if(len(field) == 1):
|
|
|
+ print(field, end='')
|
|
|
+ else:
|
|
|
+ print('[ ]', end='')
|
|
|
+ print('')
|
|
|
+
|
|
|
+def solve_by_cell_check():
|
|
|
+ # for each cell
|
|
|
+ y = 0
|
|
|
+ for row in gamefield:
|
|
|
+ x = 0
|
|
|
+ for field in row:
|
|
|
+ # if value is already defined, check next value
|
|
|
+ if(len(field) == 1):
|
|
|
+ continue
|
|
|
+ # for each possible value
|
|
|
+ for possible_value in field:
|
|
|
+ print("check value", possible_value, "at (",x,"/",y,") : ",gamefield[y][x])
|
|
|
+ if(check_row_for_value(x, y, possible_value) or
|
|
|
+ check_column_for_value(x, y, possible_value) or
|
|
|
+ check_3x3_for_value(x, y, possible_value)):
|
|
|
+ # if yes, remove it from possible values
|
|
|
+ remove_from_possible_values(x, y, possible_value)
|
|
|
+ print(gamefield[y][x])
|
|
|
+ # reset x and break to check that field again
|
|
|
+ x = x - 1
|
|
|
+ break
|
|
|
+ x = x + 1
|
|
|
+ y = y + 1
|
|
|
+
|
|
|
+def remove_from_possible_values(x, y, value):
|
|
|
+ gamefield[y][x].remove(value)
|
|
|
+
|
|
|
+# i do not explicitely skip the field itself because it is skipped by len check
|
|
|
+def check_row_for_value(x, y, value):
|
|
|
+ for field in gamefield[y]:
|
|
|
+ if len(field) == 1:
|
|
|
+ if field[0] == value:
|
|
|
+ return True
|
|
|
+ return False
|
|
|
+
|
|
|
+def check_column_for_value(x, y, value):
|
|
|
+ return False
|
|
|
+
|
|
|
+def check_3x3_for_value(x, y, value):
|
|
|
+ return False
|
|
|
+
|
|
|
+fill_empty_fields_with_possible_values()
|
|
|
+#print(gamefield[0][0])
|
|
|
+print_sudoku()
|
|
|
+solve_by_cell_check()
|
|
|
+#print(gamefield[0][0])
|
|
|
+#print_sudoku()
|