1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
def valid(grid,x,y,number):
for i in range(0,9):
if grid[y][i] == number:
return False
for i in range(0,9):
if grid[i][x] == number:
return False
xOffset = (x // 3) * 3
yOffset = (y // 3) * 3
for i in range(0,3):
for j in range(0,3):
if grid[yOffset+i][xOffset+j] == number:
return False
return True
def solve(game):
grid = game.board # grid is a 2d list of the grid
for y in range(0,9):
for x in range(0,9):
if grid[y][x] == None:
for num in range(1,10):
if valid(grud,x,y,num,9):
grid[y][x] = num
func = solve(game)
if not func:
grid[y][x] = None
if num == 9 and not valid(grid,x,y,num,9):
return False
|