Exemplo n.º 1
0
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
Exemplo n.º 2
0
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()
Exemplo n.º 3
0
"""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])
Exemplo n.º 4
0
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):