def test_unsolvable_puzzle(self): string = ''' 3 0 0 5 5 5 5 6 6 0 6 1 0 1 4 4 6 4 4 ''' solver = PuzzleSolver(string) with self.assertRaises(ValueError): solver._try_fill_empty_cells()
def test_check_group_size(self): string = ''' 3 0 5 5 5 5 5 6 6 0 0 1 0 1 4 4 6 4 4 ''' solver = PuzzleSolver(string) with self.assertRaises(ValueError): solver._check_group_size() string = ''' 2 0 5 5 5 5 5 6 6 0 0 5 0 1 4 4 6 4 4 ''' solver = PuzzleSolver(string) with self.assertRaises(ValueError): solver._find_unfilled_groups()
def test_solve_and_try_fill_empty_cells(self): string = ''' 3 0 0 5 0 0 5 3 0 0 5 1 0 1 4 4 3 2 2 ''' solver = PuzzleSolver(string, True) solver.solve() for cell in solver.field_state.field.get_all_cells(): self.assertNotEqual(solver.field_state.get_state(cell), 0)
def test_solve(self): string = ''' 0 0 0 2 3 5 5 1 0 3 5 1 3 4 4 4 3 3 4 ''' solver = PuzzleSolver(string) solver.solve() for cell, value in (((0, 1), 5), ((0, 2), 5), ((2, 1), 2), ((0, 0), 3)): self.assertEqual(solver.field_state.get_state(cell), value) string = ''' 3 0 0 5 0 5 5 3 0 4 5 1 0 1 4 4 3 2 2 ''' solver = PuzzleSolver(string) solver.solve() for cell, value in (((0, 1), 3), ((0, 2), 3), ((1, 1), 5), ((2, 1), 4), ((3, 0), 3)): self.assertEqual(solver.field_state.get_state(cell), value)
def test_find_possible_values_strict(self): string = ''' 3 3 0 5 0 5 5 3 0 4 5 1 0 1 4 4 3 2 2 ''' solver = PuzzleSolver(string, False, True) solver._refresh_state() for cell, values in (((0, 2), {3, 5}), ((1, 1), {3, 4, 5})): self.assertSetEqual(set(solver.possible_values[cell]), values)
def test_find_additional_values(self): string = ''' 3 3 0 5 0 5 5 0 4 4 5 1 0 1 4 4 0 2 2 ''' solver = PuzzleSolver(string, True, False) solver._refresh_state() for cell, values in (((0, 2), {1, 3, 5}), ((1, 1), {1, 3, 5}), ((2, 0), {1, 2, 3, 5}), ((3, 0), {2, 3, 5}), ((4, 0), {3, 5})): self.assertSetEqual(set(solver.possible_values[cell]), values)
def test_error_from_string_to_state(self): string = ''' 3 5 1 2 3 5 5 2 3 1 5 0 0 4 4 4 ''' with self.assertRaises(ValueError): PuzzleSolver(string) string = ''' 3 5 1 2 3 5 2 3 1 5 0 0 4 4 4 0 0 4 ''' with self.assertRaises(ValueError): PuzzleSolver(string)
def write_solution(puzzle, unity, filename, colored, strict): try: solver = PuzzleSolver(puzzle, bool(unity), bool(strict)) solver.solve() if filename: try: with open(filename, 'w') as output_file: print(solver.field_state, file=output_file) except Exception as e: print('Error while writing to file\n{}'.format(e), file=sys.stderr) sys.exit(ERROR_WRITING_TO_FILE) else: if colored: solver.field_state.color_state() print(solver.field_state) except Exception as e: print('Error while solving puzzle\n{}'.format(e), file=sys.stderr) sys.exit(ERROR_SOLVING_PUZZLE)
def solve_puzzle_without_some_values(self): string = ''' 0 0 0 4 4 4 0 6 6 0 6 1 0 1 4 4 6 4 4 ''' solver = PuzzleSolver(string, False, True) with self.assertRaises(ValueError): solver.solve() solver = PuzzleSolver(string, False, False) solver.solve() for value in solver.field_state.get_full_state().values(): self.assertNotEqual(value, 0)