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 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; #Index unused, inaccurate? 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 for num in index.validValues(): if (isValid(index,num)): index.setValue(num) Sudoku_validCheck.validCheck(Sudoku_board.board.tileList()) if Sudoku_main.main()==True: #TEST# Sudoku_view.view() return True else: Sudoku_main.reset() Sudoku_main.createScenario3() index.setValue(num) if solveSudoku(tileList): return True index.setValue(-1) return False # this triggers backtracking #solveSudoku(Sudoku_board.board.tileList()) #elapsed = timeit.default_timer() - start_time #print elapsed
def backtrack(strippedList): Sudoku_validCheck.validCheck(Sudoku_board.board.tileList()) for tile in sortedList: for validValue in tile.validValues(): tile.setValue(validValue) Sudoku_validCheck.validCheck((Sudoku_board.board.tileList())) #Keep things up to date here... Sudoku_main.main() if Sudoku_main.isComplete()==True: return else: Sudoku_main.reset() Sudoku_main.createScenario3() #Ah, this solves the mystery Sudoku_view.view()
def attempt3(): Sudoku_validCheck.validCheck(Sudoku_board.board.tileList()) Sudoku_eliminationTest.elimination(Sudoku_board.board.tileList()[0]) for item in Sudoku_board.board.tileList(): col=Sudoku_board.board.columnList()[item.col()-1] row=Sudoku_board.board.rowList()[item.row()-1] square=Sudoku_board.board.squareList()[item.square()-1] for num in item.validValues(): if not (col.valueExists(num) or row.valueExists(num) or square.valueExists(num)): item.setValue(num) Sudoku_validCheck.validCheck(Sudoku_board.board.tileList()) Sudoku_eliminationTest.elimination(Sudoku_board.board.tileList()[item])
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 attempt(): validNums=[1,2,3,4,5,6,7,8,9] Sudoku_validCheck.validCheck(Sudoku_board.board.tileList()) Sudoku_eliminationTest.elimination(Sudoku_board.board.tileList()[0]) for item in Sudoku_board.board.tileList(): if item.value()==-1: print item.name() col=Sudoku_board.board.columnList()[item.col()-1] row=Sudoku_board.board.rowList()[item.row()-1] square=Sudoku_board.board.squareList()[item.square()-1] for num in validNums: if not (col.valueExists(num) or row.valueExists(num) or square.valueExists(num)): #But how to backtrack here...? item.setValue(num) Sudoku_validCheck.validCheck(Sudoku_board.board.tileList()) Sudoku_eliminationTest.elimination(item) else: continue return "Done"
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 attempt2(iteration, validNumbers=[1,2,3,4,5,6,7,8,9], keyMax=9): Sudoku_validCheck.validCheck(Sudoku_board.board.tileList()) Sudoku_eliminationTest.elimination(Sudoku_board.board.tileList()[0]) keyMax key=0 iteration=0 while iteration<len(Sudoku_board.board.tileList()): col=Sudoku_board.board.columnList()[Sudoku_board.board.tileList()[iteration].col()-1] row=Sudoku_board.board.rowList()[Sudoku_board.board.tileList()[iteration].row()-1] square=Sudoku_board.board.squareList()[Sudoku_board.board.tileList()[iteration].square()-1] while key<keyMax: if key==keyMax: if not (col.valueExists(validNumbers[key-1]) or row.valueExists(validNumbers[key-1]) or square.valueExists(validNumbers[key-1])): Sudoku_board.board.tileList()[iteration].setValue(validNumbers[key]) Sudoku_validCheck.validCheck(Sudoku_board.board.tileList()) Sudoku_eliminationTest.elimination(Sudoku_board.board.tileList()[iteration]) return attempt2(iteration+1) else: return attempt2(iteration-1,validNumbers[1:0],keyMax=keyMax-1) else: if not (col.valueExists(validNumbers[key-1]) or row.valueExists(validNumbers[key-1]) or square.valueExists(validNumbers[key-1])): Sudoku_board.board.tileList()[iteration].setValue(validNumbers[key]) Sudoku_validCheck.validCheck(Sudoku_board.board.tileList()) Sudoku_eliminationTest.elimination(Sudoku_board.board.tileList()[iteration]) return attempt2(iteration+1) key+=1 iteration+=1
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
"""I can think of a brute force back-tracking algorithm that will check all valid numbers for every square...Horrifically inefficient, but possible.""" #How exactly to implement, though? 81 nested for loops sounds ridiculous... #Recursion? import operator import Sudoku_board import Sudoku_main import Sudoku_eliminationTest import Sudoku_view import Sudoku_validCheck Sudoku_main.createScenario3() Sudoku_validCheck.validCheck(Sudoku_board.board.tileList()) tileList = Sudoku_board.board.tileList() def merge(left, right, compare): result=[] i , j = 0 , 0 while i<len(left) and j<len(right): if compare(len(left[i].validValues()), len(right[j].validValues())): result.append(left[i]) i+=1 else: result.append(right[j]) j+=1 while (i<len(left)): result.append(left[i]) i+=1 while (j<len(right)): result.append(right[j])