Beispiel #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=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)
            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
Beispiel #2
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 range(1,10):
    
        #if looks promising
        if (isValid(index, num)):
            # make tentative assignment
#            print "assigning "+index.name()+" to: "+str(num)
            index.setValue(num)
#            Sudoku_validCheck.validChecker(Sudoku_board.board.tileList())
#            Sudoku_view.view()
#            print
 
            # return, if success, yay!
            if solveSudoku(tileList):
                return True
 
            # failure, unmake & try again
            index.setValue(-1)
#            Sudoku_validCheck.validChecker(Sudoku_board.board.tileList())
#    print "Backtracking."
    return False # this triggers backtracking
Beispiel #3
0
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."
Beispiel #4
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()
Beispiel #5
0
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 
Beispiel #6
0
def solveSudoku(unassignedTileList):
    """ 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(unassignedTileList)
 
    # If there is no unassigned location, we are done
    if (findUnassignedLocation(unassignedTileList)==False):
       Sudoku_view.view()
       return True # success!
 
    # consider valid digits
    for num in index.validValues():
        if (isValid(index,num)):
            index.setValue(num)
            if solveSudoku(unassignedTileList):
                return True
            index.setValue(-1)
    return False # this triggers backtracking
Beispiel #7
0
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
Beispiel #8
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) """
    unassigned_tiles=findUnassignedLocation(tileList)[1]
    index=findUnassignedLocation(tileList)[0]
 
    # If there is no unassigned location, we are done
    if (unassigned_tiles==False):
       Sudoku_view.view()
       return True # success!
 
    # consider valid digits
    for num in index.validValues():
        if (isValid(index,num)):
            index.setValue(num)
            if solveSudoku(unassigned_tiles):
                return True
            index.setValue(-1)
    return False # this triggers backtracking