Exemplo n.º 1
0
def run_app(origBoard):
    print('Board is valid:', sd.BoardIsValid(origBoard))

    candBoard = sd.SolveCandidates(origBoard)

    app = QtWidgets.QApplication(sys.argv)
    mainWin = SudokuMainWindow(origBoard, candBoard)
    mainWin.show()

    return app.exec_()
Exemplo n.º 2
0
    def FillinSingleCandidatesStep(self):
        """ Fills in any empty cells with single candidate
        Copies inputs so not changed by this function."""
        if sd.BoardIsValid(self.curr_board):
            for i in range(0, 9):
                for j in range(0, 9):
                    if len(self.cand_board[i][j]) == 1 and self.curr_board[i][j] == 0:
                        self.curr_board[i][j] = next(iter(self.cand_board[i][j]))

            self.cand_board = sd.SolveCandidatesIntersect(self.curr_board, self.cand_board)
Exemplo n.º 3
0
def run_app(orig_board):
    """ Main application function """
    print('Board is valid:', sd.BoardIsValid(orig_board))

    model = SudokuModel(orig_board)

    app = QtWidgets.QApplication(sys.argv)
    main_win = PyQtSudokuView()
    controller = Controller(main_win, model)
    main_win.show()

    return app.exec_()
Exemplo n.º 4
0
    def Solve(self):
        num_solns = 0

        if sd.BoardIsValid(self.curr_board):
            print('Start Solver')
            num_solns, soln_board = sd.SolvewBacktrack(self.curr_board)
            print('End Solver')

            # Prob not needed but here as a failsafe
            if num_solns == 0:
                print('No solution')
            elif num_solns == 1:
                print('Single Solution')
                self.curr_board = soln_board
            else:
                print('Multiple Solutions')

        if not sd.BoardIsValid(self.curr_board):
            print('Invalid')
            num_solns = -1

        return num_solns
Exemplo n.º 5
0
def UnitTests():
    print('######### Unit Tests #############')
    testboard = [[7, 8, 0, 4, 0, 0, 1, 2, 0], [6, 0, 0, 0, 7, 5, 0, 0, 9],
                 [0, 0, 0, 6, 0, 1, 0, 7, 8], [0, 0, 7, 0, 4, 0, 2, 6, 0],
                 [0, 0, 1, 0, 5, 0, 9, 3, 0], [9, 0, 4, 0, 6, 0, 0, 0, 5],
                 [0, 7, 0, 3, 0, 0, 0, 1, 2], [1, 2, 0, 0, 0, 7, 4, 0, 0],
                 [0, 4, 9, 2, 0, 6, 0, 0, 7]]

    ns, sb = sd.SolvewBacktrack(testboard)
    print('Num Solutions in Test Board, should be 1:', ns)

    testboard[4][4] = 3
    print('Check valid via Row Duplicate Test. Should be False:', \
        sd.BoardIsValid(testboard))

    testboard[4][4] = 7
    print('Check valid via Col Duplicate Test. Should be False:', \
        sd.BoardIsValid(testboard))

    testboard[4][4] = 5
    testboard[8][8] = 1
    print('Check valid via Block Duplicate Test. Should be False:', \
        sd.BoardIsValid(testboard))

    ns, sb = sd.SolvewBacktrack(testboard)
    print('Num Solutions in Invalid Test Board, should be 0:', ns)

    testboard = [[7, 8, 0, 4, 0, 0, 1, 2, 0], [6, 0, 0, 0, 7, 5, 0, 0, 9],
                 [0, 0, 0, 6, 0, 1, 0, 7, 8], [0, 0, 7, 0, 4, 0, 2, 6, 0],
                 [0, 0, 1, 0, 5, 0, 9, 3, 0], [9, 0, 4, 0, 6, 0, 0, 0, 5],
                 [0, 7, 0, 3, 0, 0, 0, 1, 2], [1, 2, 0, 0, 0, 0, 4, 0, 0],
                 [0, 0, 0, 0, 0, 0, 0, 0, 0]]

    ns, sb = sd.SolvewBacktrack(testboard)
    print('Num Solutions in Incomplete Test Board, should be > 1:', ns)

    print('######### End Unit Tests #############')