def setUp(self): """ Create rows, columns and boxes for each test """ self.fields = auxiliary_functions.load_sudoku(self.sudoku) self.fields_s = auxiliary_functions.load_sudoku(self.sudoku_s) self.rows = cr.create_rows(self.fields) self.columns = cr.create_columns(self.fields) self.boxes = cr.create_boxes(self.fields)
def test_load_sudoku(self): fields_t = auxiliary_functions.load_sudoku(self.sudoku) self.assertEqual(fields_t[0].x, 0) self.assertEqual(fields_t[7].x, 7) self.assertEqual(fields_t[80].x, 8) self.assertEqual(fields_t[0].y, 0) self.assertEqual(fields_t[8].y, 0) self.assertEqual(fields_t[79].y, 8) self.assertEqual(fields_t[77].number, 9) self.assertEqual(fields_t[14].number, 5) self.assertEqual(fields_t[28].number, 7)
def setUpClass(cls): cls.sudoku = [[2, 0, 0, 0, 9, 0, 0, 5, 1], [0, 0, 6, 8, 0, 5, 3, 0, 0], [5, 0, 3, 0, 4, 0, 0, 0, 0], [4, 7, 0, 0, 0, 8, 0, 0, 2], [0, 0, 0, 9, 0, 0, 0, 3, 0], [0, 6, 0, 0, 7, 0, 0, 4, 0], [0, 9, 4, 7, 0, 6, 0, 0, 0], [0, 5, 0, 0, 0, 0, 9, 0, 3], [0, 0, 0, 2, 0, 9, 0, 0, 7]] # s id for solved cls.sudoku_s = [[2, 4, 7, 6, 9, 3, 8, 5, 1], [9, 1, 6, 8, 2, 5, 3, 7, 4], [5, 8, 3, 1, 4, 7, 6, 2, 9], [4, 7, 1, 3, 6, 8, 5, 9, 2], [8, 2, 5, 9, 1, 4, 7, 3, 6], [3, 6, 9, 5, 7, 2, 1, 4, 8], [1, 9, 4, 7, 3, 6, 2, 8, 5], [7, 5, 2, 4, 8, 1, 9, 6, 3], [6, 3, 8, 2, 5, 9, 4, 1, 7]] cls.fields = auxiliary_functions.load_sudoku(cls.sudoku) cls.fields_s = auxiliary_functions.load_sudoku(cls.sudoku_s)
def setUpClass(cls): cls.sudoku = [[2, 0, 0, 0, 9, 0, 0, 5, 1], [0, 0, 6, 8, 0, 5, 3, 0, 0], [5, 0, 3, 0, 4, 0, 0, 0, 0], [4, 7, 0, 0, 0, 8, 0, 0, 2], [0, 0, 0, 9, 0, 0, 0, 3, 0], [0, 6, 0, 0, 7, 0, 0, 4, 0], [0, 9, 4, 7, 0, 6, 0, 0, 0], [0, 5, 0, 0, 0, 0, 9, 0, 3], [0, 0, 0, 2, 0, 9, 0, 0, 7]] cls.fields = auxiliary_functions.load_sudoku(cls.sudoku) # Create rows, column and boxes for the testing cls.rows = cr.create_rows(cls.fields) cls.columns = cr.create_columns(cls.fields) cls.boxes = cr.create_boxes(cls.fields)
def solve_sudoku(sudoku): # Create field objects for the sudoku fields = load_sudoku(sudoku) fields_old = load_sudoku(sudoku) # Create lists for all rows, column and boxes rows = create_rows(fields) columns = create_columns(fields) boxes = create_boxes(fields) # Loop all the fields max 20 times and run all solving functions. counter = 1 while not is_solved(fields) and counter < 20: for field in fields: # Remove possibilities if field if they appears in the same row, colomn or box field.possible = simple_remove_possibilities(field, rows[field.y]) field.possible = simple_remove_possibilities( field, columns[field.x]) field.possible = simple_remove_possibilities( field, boxes[field.box]) # Remove all other possibilities if a possibility only appears ones in a row, column or box field.possible = find_single_possibilities(field, rows[field.y]) field.possible = find_single_possibilities(field, columns[field.x]) field.possible = find_single_possibilities(field, boxes[field.box]) # If a pair of two possibilities just appears in two fields in a row, column or box then remove them from all other # them from all other fields in that row, column or box for row in rows: row = remove_poss_just_two(row) for column in columns: column = remove_poss_just_two(column) for box in boxes: box = remove_poss_just_two(box) # Remove extra possibilities in a field if two of the possibilities just appear in one other # field in the row, column or box field.possible = remove_extra_poss_field(field, rows[field.y]) field.possible = remove_extra_poss_field(field, columns[field.x]) field.possible = remove_extra_poss_field(field, boxes[field.box]) # Set the finds number if there is only one possibility field.number = check_field_possible_number(field) counter += 1 # If the solution method cant solve the sudoku in 20 iterations the recursive solution kiks in if not is_solved(fields): used_recursive = True sudo = RecursivSolv(fields) # Call recursive solution method fields = load_sudoku( sudo.sudoku_num) # Update fields from RecursiveSolve output else: used_recursive = False # Just for print output print(" ") if is_solved(fields): if not used_recursive: print("The sudoku was solved after", str(counter), "iterations") else: print("The sudoku was solved after", str(counter), "normal iterations") print("and", str(sudo.counter), "recursive solution interations") else: print("The sudoku was not solved after", str(counter), "iterations") print(count_zeros(fields), "unsolved fields") # The original and the hopfully solved sudoku are printed side by side print_two_sudokus(fields_old, fields) return fields