def enterRow(sudoku): val = raw_input("\nEnter row, of the form: \"1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]\"\nPut a 0 for a blank square.\n\n") if len(val) != 31: print "\nIncorrect format. Here is an example: " print " 1 = [2, 5, 0, 0, 0, 6, 7, 8, 0] will put those values in the first row" return try: rowInd = int(val[0:1]) - 1 nums = map(int, val[5:-1].split(",")) except ValueError: print "\nIncorrect format ERROR. Here is an example: " print " 1 = [2, 5, 0, 0, 0, 6, 7, 8, 0] will put those values in the first row" return row = [] for col in range(9): row.append(nums[col]) sudoku[rowInd] = row main.printSudoku(sudoku)
def enterColumn(sudoku): val = raw_input("\nEnter column, of the form: \"colNum = [1, 2, ... , 9]\"\nPut a 0 for a blank square.\n\n") if len(val) != 31: print "\nIncorrect format. Here is an example: " print " 1 = [2, 5, 0, 0, 0, 6, 7, 8, 0] will put those values in the first column" return try: colInd = int(val[0:1]) - 1 nums = map(int, val[5:-1].split(",")) except ValueError: print "\nIncorrect format ERROR. Here is an example: " print " 1 = [2, 5, 0, 0, 0, 6, 7, 8, 0] will put those values in the first row" return col = [] for row in range(9): col.append(nums[row]) for row in range(9): sudoku[row][colInd] = col[row] main.printSudoku(sudoku)
def enterValue(sudoku): print "0 will clear the cell, 1-9 will insert a number" val = raw_input("\nEnter a coordinate and a value of the form \"[row][col] = number\":\n\n") if len(val) != 10: print "\nIncorrect format. Here is an example: " print " [1][9] = 5 will put a 5 in the very bottom left corner" return try: row = int(val[1:2]) row -= 1 col = int(val[4:5]) col -= 1 num = int(val[-1]) except ValueError: print "\nIncorrect format. Here is an example: " print " [1][9] = 5 will put a 5 in the very bottom left corner" sudoku[row][col] = num main.printSudoku(sudoku)
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 clearSudoku(sudoku): for y in range(9): for x in range(9): sudoku[x][y] = 0 main.printSudoku(sudoku)
"""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])
import Sudoku_board import Sudoku_eliminationTest import Sudoku_view import Sudoku_validCheck import Sudoku_main import timeit """ A Backtracking program in Python. to solve Sudoku problem""" """Creating scenario""" Sudoku_main.createScenario3() #TEMP # UNASSIGNED is used for empty cells in sudoku grid UNASSIGNED=0 # N is used for size of Sudoku grid. Size will be NxN N=9 def findUnassignedLocation(tileList): """This function finds an entry in grid that is still unassigned. Returns Boolean false if it can't find an unassigned item. Otherwise, returns item.""" for item in Sudoku_board.board.tileList(): if item.value()==-1: return item else: return False # Checks whether it will be legal to assign num to the given tile def isValid(tile, num):