erken sona eren çözülür bir sudoku bulmaca çözer bir Python programı yapmaya çalışıyorum. Bir karo için rasgele bir koordinat oluşturur ve bu kutuda zaten bir sayı varsa, tekrar dener. Daha sonra, oraya koymak için 1 ile 9 arasında bir sayı üretir ve bu sayı zaten o satır, sütun veya kısımda değilse, değeri atayacaktır ve bu koordinatları işgal edilen karolar listesine ekleyecektir. Tüm fayanslar doldurulduktan sonra, döngüden çıkıp tamamlanmış ızgarayı döndürmesi gerekiyordu.Python döngü
Sorun, programın donmasına neden olan yaklaşık 70 döngüden sonra hiçbir zaman durmaksızın durmaktır. başvurduğu fonksiyonlar için kod burada
def populate(grid):
usedCoords = []
populated = False
while not populated:
x = random.randrange(len(grid))
y = random.randrange(len(grid))
while [x,y] in usedCoords:
x = random.randrange(len(grid))
y = random.randrange(len(grid))
value = random.randrange(1, len(grid) + 1)
if not rowCheck(grid, x, y, value) and not columnCheck(grid, x, y, value) and not squareCheck(grid, x, y, value):
grid[x][y] = value
usedCoords.append([x,y])
print(len(usedCoords))
if len(usedCoords) == len(grid) ** 2:
populated = True
return grid
Ve edilir: Burada
bahsettiğimi işlevi kodudurdef rowCheck(grid, x, y, value):
for i in range(len(grid)):
if not i == x:
if grid[i][y] == value:
return True
return False
def columnCheck(grid, x, y, value):
for i in range(len(grid)):
if not i==y:
if grid[x][i] == value:
return True
return False
def squareCheck(grid, x, y, value):
grid2 = [0] * (sectionSide) #new grid for the specific section
for i in range(len(grid2)):
grid2[i] = [0] * sectionSide
for i in range(x - (sectionSide - 1), x + sectionSide): #scanning only nearby coordinates
for j in range(y - (sectionSide - 1), y + sectionSide):
try:
if i // sectionSide == x // sectionSide and j // sectionSide == y // sectionSide:
grid2[i][j] = grid[x][y]
except IndexError:
pass
for i in range(len(grid2)):
for j in range(len(grid2[i])):
if grid2[i][j] == value and not (i == x and j == y):
return True
return False
Döngünün bitmek bilmez, * sonlandırılıyor * değil mi? – Miles