OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Python Sudoku Solver: Appling Rules and Breaking Infinite Loops

  • Thread starter Thread starter abizittin
  • Start date Start date
A

abizittin

Guest
I am writing sudoku solver python code. But I cannot find problem. Python cannot finish the code. I do not know what happening but it is like python enters infinite loop. This loop usually affect a row and changing values. Sometimes it write wrong number and contunies like this [
8 and 8 in same row(second row)
] .I have create if else statement for this but it is not solving this problem.

Here is my code:

Code:
import tkinter
from copy import deepcopy
import time
import threading

window = tkinter.Tk()
window.title("Sudoku Solver")
window.config(padx=30, pady=30)

posibles = []  # adı posibles olacak
entrylist = []
exactvals = []
labellist = []

def create000():
    global posibles
    global exactvals
    for i in range(9):
        posiblesemp = []
        exectemp = []
        for j in range(9):
            posiblesemp.append([])
            exectemp.append(0)
        posibles.append(deepcopy(posiblesemp))
        exactvals.append(deepcopy(exectemp))
        posiblesemp.clear()
        exectemp.clear()
    return posibles


# def solve():
#     def check(i,j):
#         global posibles
#         for i in range(9):
#             for j in range(9):
#                 for element in posibles[i][j]:
#                     if j == 9:


def solve():
    # def set_text(x, y, text):
    #     u = threading.Thread(target=set_text_wouthreading, args=(x, y, text))
    #     u.start()
    #     u.join()

    def set_text(x, y, text):  # set_text_wouthreading idi.
        global labellist
        if len(posibles[x][y]) == 1:
            return
        elif str(text) == "":
            labellist[x][y].config(text=str(text), background="gray")
        elif text != 0:
            labellist[x][y].config(text=str(text), background="red")
        window.update()
        time.sleep(0.0001)
        return

    def is_in_3x3(element, columnpart, rowpart):
        for m in range(3):
            for n in range(3):
                if element == exactvals[3*columnpart + m][3*rowpart + n]: return False


    def check(i, j):

        def nexcheck(i,j):   #to engele borders
            pass

        print("check")
        global posibles
        global exactvals

        if len(posibles[i][j]) == 1:
            if j != 8:
                if check(i, j+1) == "ok": return True
            elif i != 8:
                if check(i+1, 0) == "ok": return True


        columnpart = int(i / 3)
        rowpart = int(j / 3)
        for element in range(1, 10):
        # for element in posibles[i][j]:

            found = False
            print(element)
            set_text(i, j, element)

            # 3x3 func oluşturcaz
            if is_in_3x3(element, columnpart, rowpart) == False:
                set_text(i, j, "")
                continue

            elif (element in exactvals[i]) or (element in [row[j] for row in exactvals]):
                set_text(i, j, "")
                continue

            elif j != 8:
                exactvals[i][j] = element
                if check(i, j + 1) != "ok":
                    exactvals[i][j] = 0
                    set_text(i, j, "")
                    continue

            elif i != 8:
                exactvals[i][j] = element
                if check(i + 1, 0) != "ok":
                    exactvals[i][j] = 0
                    set_text(i, j, "")
                    continue

            else:
                set_text(i, j, element)
                return "ok"


    check(0, 0)
    return exactvals


def collect_data():  # collec ederken zaten var  i j kullanıp uctan ekleyebiliriz
    global posibles
    global exactvals
    global labellist
    for i in range(9):
        for j in range(9):  # yatay liste değiştiriyor
            if len(entrylist[i][j].get()) != 0:
                value = [int(entrylist[i][j].get())]
                exc = int(entrylist[i][j].get())
                labellist[i][j].config(text=exc, background="lightgreen")
            else:
                value = [x + 1 for x in range(9)]
                exc = 0
            posibles[i][j] = value
            exactvals[i][j] = exc
    window.update()
    solve()
    return posibles

for row in range(9):
    tempentry = []
    for column in range(9):
        a = tkinter.Entry(width=3)
        a.grid(column=column, row=row)
        tempentry.append(a)
    entrylist.append(tempentry)

emptylabel = tkinter.Label(width=3)
emptylabel.grid(column=9,row=0,rowspan=9)

for row in range(9):
    templabel = []
    for column in range(10, 19):
        a = tkinter.Label(width=3,borderwidth=2, relief="groove")
        a.grid(column=column, row=row)
        templabel.append(a)
    labellist.append(templabel)


but_solve = tkinter.Button(command=collect_data, text="SOLVE", width=9, pady=5)
but_solve.grid(columnspan=3, row=9, column=3)

create000()

window.mainloop()

I want to solve problem related sudoku rules and break the eendless loop.
<p>I am writing sudoku solver python code. But I cannot find problem.
Python cannot finish the code. I do not know what happening but it is like python enters infinite loop. This loop usually affect a row and changing values. Sometimes it write wrong number and contunies like this [<img src="https://i.sstatic.net/bmLZaHEU.png" alt="8 and 8 in same row(second row)" />]
.I have create if else statement for this but it is not solving this problem.</p>
<p>Here is my code:</p>
<pre><code>import tkinter
from copy import deepcopy
import time
import threading

window = tkinter.Tk()
window.title("Sudoku Solver")
window.config(padx=30, pady=30)

posibles = [] # adı posibles olacak
entrylist = []
exactvals = []
labellist = []

def create000():
global posibles
global exactvals
for i in range(9):
posiblesemp = []
exectemp = []
for j in range(9):
posiblesemp.append([])
exectemp.append(0)
posibles.append(deepcopy(posiblesemp))
exactvals.append(deepcopy(exectemp))
posiblesemp.clear()
exectemp.clear()
return posibles


# def solve():
# def check(i,j):
# global posibles
# for i in range(9):
# for j in range(9):
# for element in posibles[j]:
# if j == 9:


def solve():
# def set_text(x, y, text):
# u = threading.Thread(target=set_text_wouthreading, args=(x, y, text))
# u.start()
# u.join()

def set_text(x, y, text): # set_text_wouthreading idi.
global labellist
if len(posibles[x][y]) == 1:
return
elif str(text) == "":
labellist[x][y].config(text=str(text), background="gray")
elif text != 0:
labellist[x][y].config(text=str(text), background="red")
window.update()
time.sleep(0.0001)
return

def is_in_3x3(element, columnpart, rowpart):
for m in range(3):
for n in range(3):
if element == exactvals[3*columnpart + m][3*rowpart + n]: return False


def check(i, j):

def nexcheck(i,j): #to engele borders
pass

print("check")
global posibles
global exactvals

if len(posibles[j]) == 1:
if j != 8:
if check(i, j+1) == "ok": return True
elif i != 8:
if check(i+1, 0) == "ok": return True


columnpart = int(i / 3)
rowpart = int(j / 3)
for element in range(1, 10):
# for element in posibles[j]:

found = False
print(element)
set_text(i, j, element)

# 3x3 func oluşturcaz
if is_in_3x3(element, columnpart, rowpart) == False:
set_text(i, j, "")
continue

elif (element in exactvals) or (element in [row[j] for row in exactvals]):
set_text(i, j, "")
continue

elif j != 8:
exactvals[j] = element
if check(i, j + 1) != "ok":
exactvals[j] = 0
set_text(i, j, "")
continue

elif i != 8:
exactvals[j] = element
if check(i + 1, 0) != "ok":
exactvals[j] = 0
set_text(i, j, "")
continue

else:
set_text(i, j, element)
return "ok"


check(0, 0)
return exactvals


def collect_data(): # collec ederken zaten var i j kullanıp uctan ekleyebiliriz
global posibles
global exactvals
global labellist
for i in range(9):
for j in range(9): # yatay liste değiştiriyor
if len(entrylist[j].get()) != 0:
value = [int(entrylist[j].get())]
exc = int(entrylist[j].get())
labellist[j].config(text=exc, background="lightgreen")
else:
value = [x + 1 for x in range(9)]
exc = 0
posibles[j] = value
exactvals[j] = exc
window.update()
solve()
return posibles

for row in range(9):
tempentry = []
for column in range(9):
a = tkinter.Entry(width=3)
a.grid(column=column, row=row)
tempentry.append(a)
entrylist.append(tempentry)

emptylabel = tkinter.Label(width=3)
emptylabel.grid(column=9,row=0,rowspan=9)

for row in range(9):
templabel = []
for column in range(10, 19):
a = tkinter.Label(width=3,borderwidth=2, relief="groove")
a.grid(column=column, row=row)
templabel.append(a)
labellist.append(templabel)


but_solve = tkinter.Button(command=collect_data, text="SOLVE", width=9, pady=5)
but_solve.grid(columnspan=3, row=9, column=3)

create000()

window.mainloop()
</code></pre>
<p>I want to solve problem related sudoku rules and break the eendless loop.</p>
 

Latest posts

Top