Search code examples
pythonblocksudokusolver

Python sudoku solver blocks


I have a problem with my sudoku solver. I was able to check vertical and horizontal but now i will check the blocks. This is the code i have now for checking the blocks

def print_sudoku(array):
    for i in range(0,3):
        print()
        for j in range(0,3):
            print(array[i][j][0],end=' ')
def controleren(array):
    for i in range(0,8):
        for j in range(0,8):
            if distance[i][j][1] + distance[i][j][2]+ distance[i][j][3]+ distance[i][j][4]+ distance[i][j][5]+ distance[i][j][6]+ distance[i][j][7]+ distance[i][j][8]+ distance[i][j][9] == 1:
                if distance[i][j][k] == 1:
                    distance[i][j][k] = k

def main():
    pass
import pprint
distance = [[[0 for k in range(10)] for j in range(3)] for i in range(3)]

distance[0][0][0] = '*'
distance[0][1][0] = 2
distance[0][2][0] = 3
distance[1][0][0] = 4
distance[1][1][0] = 5
distance[1][2][0] = 6
distance[2][0][0] = 7
distance[2][1][0] = 8
distance[2][2][0] = 9

print_sudoku(distance)

Here i'm giving every possibility the value 1

for i in range(0,3):
    for j in range(0,3):
        if distance[i][j][0] == '*':
            for k in range(1,10):
                distance[i][j][k] = 1

This is the important part. The while loop is infinite distance[0][0][0] remains equal to * instead of get the value of 1 that is the only number what is missing in the block. What is happening is: that every value that is already standing in one of the othor distance[][][] is set to 0

while distance[0][0][0] == '*':
    for i in range(0,3):
        for j in range(0,3):
            if distance[i][j][0] != '*':
                k = distance[i][j][0]
                for i in range(0,3):
                    for j in range(0,3):
                        distance[i][j][k] = 0

In this part the code looks for the missing number so is there one possibilty, one number with the value 1 because that number is the missing number.

    for i in range(0,3):
        for j in range(0,3):
            if distance[i][j][1] + distance[i][j][2]+ distance[i][j][3]+ distance[i][j][4]+ distance[i][j][5]+ distance[i][j][6]+ distance[i][j][7]+ distance[i][j][8]+ distance[i][j][9] == 1:
                for k in range(1,10):
                    if distance[i][j][k] == 1:
                        distance[i][j][0] = k

print('')
print_sudoku(distance)

I hope you do understand it, probably not (i find it hard to explain what i'm doing) so just say it if something is not clear.

Rudy


Solution

  • while distance[0][0][0] == '*':
        for i in range(0,3):
            for j in range(0,3):
                if distance[i][j][0] != '*':
                    k = distance[i][j][0]
                    for i in range(0,3):
                        for j in range(0,3):
                            distance[i][j][k] = 0
    

    In here there are two is and js. I think this is the part causing the problem. Try to rename them because it affects the output.

    So the bottom part should look like this.

     k = distance[i][j][0]
     for m in range(0,3):
         for n in range(0,3):
             distance[m][n][k] = 0
    

    This is your code.

    distance = [[[0 for k in range(10)] for j in range(3)] for i in range(3)]
    
    distance[0][0][0] = '*'
    distance[0][1][0] = 2
    distance[0][2][0] = 3
    distance[1][0][0] = 4
    distance[1][1][0] = 5
    distance[1][2][0] = 6
    distance[2][0][0] = 7
    distance[2][1][0] = 8
    distance[2][2][0] = 9
    
    for i in range(0,3):
        for j in range(0,3):
            if distance[i][j][0] == '*':
                for k in range(1,10):
                    distance[i][j][k] = 1
    
    if distance[0][0][0] == '*':
        for i in range(0,3):
            for j in range(0,3):
                if distance[i][j][0] != '*':
                    k = distance[i][j][0]
                    for i in range(0,3):
                        for j in range(0,3):
                            distance[i][j][k] = 0
    print (distance)
    

    When there are same iteration variables:

    >>>[[['*', 1, 0, 1, 0, 1, 1, 0, 0, 0], ... ]
    

    After you rename them:

    >>>[[['*', 1, 0, 0, 0, 0, 0, 0, 0, 0], ...]
    

    I changed while to if to see changes in distance.