def main(): complete=False count=0 itercount=0 while complete==False and count<=10: Sudoku_validCheck.validChecker(Sudoku_board.board.tileList()) if Sudoku_validCheck.validSetter(Sudoku_board.board.tileList())==True: count-=1 for item in Sudoku_board.board.tileList(): if Sudoku_eliminationTest.elimination(item) !=-1: # print Sudoku_eliminationTest.elimination(item) # count-=1 BUG? item.setValue(Sudoku_eliminationTest.elimination(item)) # Sudoku_validCheck.validCheck(Sudoku_eliminationTest.getAllNeighbourTiles(item)) #To update the valid and invalid tiles after a change has been made. Sudoku_validCheck.validCheck(Sudoku_board.board.tileList()) #If things are happening, continue this thread. # Sudoku_view.view() # print if isComplete()==True: Sudoku_view.view() complete=True return True # if continues()==False: # return count+=1 itercount+=1 # Sudoku_view.view() # print return "Couldn't find a solution in "+str(itercount)+" iterations."
def recursive(n=0): validNums=[1,2,3,4,5,6,7,8,9] for j in range(n,len(Sudoku_board.board.tileList())): for num in validNums: if num in Sudoku_board.board.tileList()[j].validValues(): Sudoku_board.board.tileList()[j].setValue(num) Sudoku_validCheck.validChecker(Sudoku_board.board.tileList()) recursive(n=n+1) print n #How to backtrack? print n Sudoku_view.view() if n==len(Sudoku_board.board.tileList())-1 and num in Sudoku_board.board.tileList()[n].validValues(): Sudoku_board.board.tileList()[n].setValue(num) Sudoku_view.view() return True if n==len(Sudoku_board.board.tileList())-1 and num not in Sudoku_board.board.tileList()[n].validValues(): return False
def main(): Continue=True while Continue==True: reset() scen=Sudoku_scenario.createScenario() if scen==3: start_time = timeit.default_timer() Sudoku_validCheck.validChecker(Sudoku_board.board.tileList()) Sudoku_solver.solveSudoku(Sudoku_board.board.tileList()) elapsed = timeit.default_timer() - start_time print elapsed else: start_time = timeit.default_timer() Sudoku_recursive2.solveSudoku(Sudoku_board.board.tileList()) elapsed = timeit.default_timer() - start_time print elapsed Continue=continues() print "Thanks for playing!" return
def solveSudoku(self,tileList): """ Takes a partially filled-in grid and attempts to assign values to all unassigned locations in such a way to meet the requirements for Sudoku solution (non-duplication across rows, columns, and boxes) """ Sudoku_validCheck.validChecker(Sudoku_board.board.tileList()) index=self.findUnassignedLocation(tileList) # If there is no unassigned location, we are done if (self.findUnassignedLocation(tileList)==False): Sudoku_view.view() return True # success! # consider valid digits for num in index.validValues(): if (self.isValid(index,num)): index.setValue(num) if self.solveSudoku(tileList): return True index.setValue(-1) return False # this triggers backtracking
def main(): Continue=True while Continue==True: reset() Sudoku_scenario.createScenario() Sudoku_view.view() print start_time = timeit.default_timer() unassignedTiles=Sudoku_validCheck.validChecker(Sudoku_board.board.tileList()) Sudoku_solver.solveSudoku(unassignedTiles) elapsed = timeit.default_timer() - start_time print elapsed Continue=continues() print "Thanks for playing!" return
# Checks whether it will be legal to assign num to the given tile def isValid(tile, num): """Checks whether it will be legal to assign num to the given tile. NOTE: this function doesn't check whether the item is unassigned. Please don't call this on already assigned tiles, or it will overwrite them.""" col=Sudoku_board.board.columnList()[tile.col()-1] row=Sudoku_board.board.rowList()[tile.row()-1] square=Sudoku_board.board.squareList()[tile.square()-1] if not (col.valueExists(num) or row.valueExists(num) or square.valueExists(num)): return True else: return False #start_time = timeit.default_timer() Sudoku_validCheck.validChecker(Sudoku_board.board.tileList()) def solveSudoku(tileList): """ Takes a partially filled-in grid and attempts to assign values to all unassigned locations in such a way to meet the requirements for Sudoku solution (non-duplication across rows, columns, and boxes) """ index=findUnassignedLocation(tileList) # print index.name() # If there is no unassigned location, we are done if (findUnassignedLocation(tileList)==False): Sudoku_view.view() return True # success! # consider digits 1 to 9