Skip to content

Commit

Permalink
Fix brute force UI bug, update module entry points
Browse files Browse the repository at this point in the history
  • Loading branch information
wdye committed Aug 29, 2018
1 parent 247466e commit 19acf42
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 50 deletions.
36 changes: 5 additions & 31 deletions sudoku/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
all_cells,
columns,
)
from sudoku.sample_puzzles import get_puzzle_by_name
from sudoku.utils.event import EventDispatcher


Expand Down Expand Up @@ -62,6 +63,7 @@ class BruteForceSolver(SudokuSolver[MatrixSudoku]):
def solve(self) -> Optional[Sudoku]:
"""Solve the puzzle by trying all possible values for all empty cells."""
if self.sudoku.is_solved():
self.on_grid_changed(self.sudoku)
return self.sudoku
cell = self.sudoku.get_next_empty_cell()
if cell is None:
Expand Down Expand Up @@ -270,37 +272,9 @@ def get_solver(sudoku: Sudoku, algorithm: SolutionAlgorithm) -> SudokuSolver:


if __name__ == '__main__':
# sudoku = MatrixSudoku.from_string("""
# +-------+-------+-------+
# | 4 . . | . . . | 8 . 5 |
# | . 3 . | . . . | . . . |
# | . . . | 7 . . | . . . |
# +-------+-------+-------+
# | . 2 . | . . . | . 6 . |
# | . . . | . 8 . | 4 . . |
# | . . . | . 1 . | . . . |
# +-------+-------+-------+
# | . . . | 6 . 3 | . 7 . |
# | 5 . . | 2 . . | . . . |
# | 1 . 4 | . . . | . . . |
# +-------+-------+-------+
# """)
sudoku = MatrixSudoku.from_string("""
6 . 2 |4 8 . |9 3 7
8 3 4 |6 . 9 |1 5 2
9 7 1 |. 2 5 |8 6 4
------+------+------
. 6 7 |8 1 2 |5 . 3
3 1 5 |7 9 . |6 2 8
2 9 . |5 6 3 |. 7 1
------+------+------
. 8 . |. 3 . |2 . 5
5 . 3 |1 . 6 |. 8 .
7 . 9 |. 5 8 |3 1 6
""")
solver = DLXSolver(sudoku, minimize_branching=True)
solved = solver.solve()
puzzle_string = get_puzzle_by_name('hard-1')
solved = solve(sudoku_string=puzzle_string)
if solved:
print(str(solved))
print(solved)
else:
print('Failed to solve sudoku')
22 changes: 3 additions & 19 deletions sudoku/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@

from sudoku import (
DictSudoku,
MatrixSudoku,
Row,
SolutionAlgorithm,
Sudoku,
ds,
get_puzzle_by_name,
get_solver,
ms,
)
from sudoku.grid import (
all_cells,
Expand Down Expand Up @@ -161,20 +159,6 @@ def run(self) -> None:


if __name__ == '__main__':
s = """
6 . 2 |4 8 . |9 3 7
8 3 4 |6 . 9 |1 5 2
9 7 1 |. 2 5 |8 6 4
------+------+------
. 6 7 |8 1 2 |5 . 3
3 1 5 |7 9 . |6 2 8
2 9 . |5 6 3 |. 7 1
------+------+------
. 8 . |. 3 . |2 . 5
5 . 3 |1 . 6 |. 8 .
7 . 9 |. 5 8 |3 1 6
"""
ms2 = MatrixSudoku.from_string(s) # TODO - does this produce an invalid solution??
ds2 = DictSudoku.from_string(s)
app = SudokuApp(sudoku=ds, delay_millis=1000)
puzzle_string = get_puzzle_by_name('hard-1')
app = SudokuApp(sudoku=DictSudoku.from_string(puzzle_string), delay_millis=1000)
app.run()

0 comments on commit 19acf42

Please sign in to comment.