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 #############')
def Solve(self): """ Solves the board using the backtracking alogorithm """ if sd.BoardSolved(self.currBoard): return self.ClearHighlights() dups = sd.FindDuplicates(self.currBoard) NumSolns = 0 if sd.CheckValid(dups): self.FillinSingleCandidates() prevBoard = deepcopy(self.currBoard) print('Start Solver') NumSolns, solnBoard = sd.SolvewBacktrack(self.currBoard) print('End Solver') if NumSolns == 0: print('No solution') self.msgText.setText('Num Solutions: 0') elif NumSolns == 1: self.currBoard = solnBoard self.UpdateChangedCells(prevBoard) dups = sd.FindDuplicates(self.currBoard) if not sd.CheckValid(dups): print('Invalid') NumSolns = 0 else: print('Single Solution') else: print('Multiple Solutions') if NumSolns == 1: self.msgText.setStyleSheet( "border: 1px solid black; color: black;") else: self.msgText.setStyleSheet("border: 1px solid black; color: red;") self.msgText.setText('Num Solutions: ' + str(NumSolns)) self.ShowInvalidCells(dups)