Esempio n. 1
0
 def display_solutions(self, solver: SudokuSolver):
     top = Toplevel(self.window, width=300, height=300)
     top.title("Solution")
     scroll = Scrollbar(top)
     canvas = Canvas(top,
                     height=300 * len(solver.get_solutions()),
                     width=500,
                     yscrollcommand=scroll.set,
                     scrollregion=(0, 0, 500, 500))
     counter = 0
     for solution in solver.get_solutions():
         for i in range(10):
             for j in range(10):
                 if (i % 3) == 0:
                     canvas.create_line(10,
                                        10 + 290 * counter + i * 30,
                                        280,
                                        10 + 290 * counter + i * 30,
                                        fill="black",
                                        width=2)
                 else:
                     canvas.create_line(10,
                                        10 + 290 * counter + i * 30,
                                        280,
                                        10 + 290 * counter + i * 30,
                                        fill="black",
                                        width=1)
                 if (j % 3) == 0:
                     canvas.create_line(10 + 30 * j,
                                        10 + 290 * counter,
                                        10 + 30 * j,
                                        280 + 290 * counter,
                                        fill="black",
                                        width=2)
                 else:
                     canvas.create_line(10 + 30 * j,
                                        10 + 290 * counter,
                                        10 + 30 * j,
                                        280 + 290 * counter,
                                        fill="black",
                                        width=1)
         for i in range(9):
             for j in range(9):
                 cell = solution.get_cell(i, j)
                 content = tk.Label(top,
                                    background="white",
                                    width=1,
                                    height=1,
                                    relief=FLAT,
                                    text=cell)
                 content.place(x=20 + j * 30, y=17 + i * 30 + 290 * counter)
         counter += 1
     save_button = tk.Button(master=top,
                             text="Save",
                             command=lambda: self.save_solutions(solver))
     save_button.place(x=300, y=10)
     scroll.pack(side=RIGHT, fill=Y)
     scroll.config(command=canvas.yview)
     canvas.pack(fill="both", expand=True)
Esempio n. 2
0
 def solve_grid(self):
     self.error_label = None
     #check for edge cases:
     # -input wrong(not int, not 0 to 9)
     try:
         counter = 0
         for i in range(9):
             for j in range(9):
                 tmp = int(self.cells[i][j].get("1.0", 'end-1c'))
                 if tmp > 9:
                     self.error_label = Label(
                         self.window,
                         text=
                         "The sudoku input was not valid (not a number from 0 to 9)."
                     ).place(x=10, y=400)
                     return
                 if tmp != 0:
                     counter += 1
         if counter < 10:
             warning = Label(
                 self.window,
                 text=
                 "There are very few given numbers. Computing all possible solutions is not feasible\non generic computers."
             ).place(x=10, y=400)
             return
     except ValueError:
         self.error_label = Label(
             self.window,
             text="The sudoku input was not valid (not a number).").place(
                 x=10, y=400)
         return
     # -not enough filled in
     #create a Grid object with all values from the gui grid
     grid = Grid()
     for i in range(9):
         for j in range(9):
             # get each cell, convert string to int
             grid.set_cell(i, j, int(self.cells[i][j].get("1.0", 'end-1c')))
     #create Sudoku Solver object and calculate solution
     solver = SudokuSolver(grid)
     #check if sudoku is valid
     if solver.is_correct() is False:
         self.error_label = Label(
             self.window,
             text="The given input is not a valid sudoku.").place(x=10,
                                                                  y=400)
         return
     solver.solve()
     # display solution in new window
     if len(solver.get_solutions()) == 0:
         warning = Label(
             self.window,
             text="There are no solutions to the given sudoku.").place(
                 x=10, y=400)
     else:
         self.display_solutions(solver)