-3

I’m trying to write a basic sudoku solver from scratch with a my own made algorithm where my program solves some of the empty elements but not all of them. Whatever elements it solves, I just copy that sudoku board replace the old board and run my program again and it should solve the other empty elements. But, the issues i’m having with my program is that the elements are not being replaced or solved, and my program is not printing the whole board. It only prints the last row. I think it has to do with how I read the rows, columns, and blocks with rone, cone, and bone, but i'm not sure what i'm doing wrong here.

This is my algorithm I made that I’m following. here

Boards file.

1 4 0 0 8 0 0 3 9
0 3 0 0 0 0 0 1 2
0 0 0 1 0 0 0 0 4
6 7 9 2 5 8 1 4 3
4 0 0 0 9 1 0 0 0
2 0 1 0 0 4 0 6 5
9 0 0 0 1 3 0 0 0
5 1 0 0 0 0 0 9 0
3 6 0 0 4 0 0 8 0

Program

a1 = []
a2 = []
a3 = []
a4 = []
a5 = []
a6 = []
a7 = []
a8 = []
a9 = []
with open('Boards') as f:
    for line in f:
        data = line.split()
        a1.append(int(data[0]))
        a2.append(int(data[1]))
        a3.append(int(data[2]))
        a4.append(int(data[3]))
        a5.append(int(data[4]))
        a6.append(int(data[5]))
        a7.append(int(data[6]))
        a8.append(int(data[7]))
        a9.append(int(data[8]))





    print("Old Board")



    print(a1[0], a2[0], a3[0],"|", a4[0], a5[0], a6[0],"|", a7[0], a8[0], a9[0])
    print(a1[1], a2[1], a3[1],"|", a4[1], a5[1], a6[1],"|", a7[1], a8[1], a9[1])
    print(a1[2], a2[2], a3[2],"|", a4[2], a5[2], a6[2],"|", a7[2], a8[2], a9[2])
    print("---------------------")
    print(a1[3], a2[3], a3[3],"|", a4[3], a5[3], a6[3],"|", a7[3], a8[3], a9[3])
    print(a1[4], a2[4], a3[4],"|", a4[4], a5[4], a6[4],"|", a7[4], a8[4], a9[4])
    print(a1[5], a2[5], a3[5],"|", a4[5], a5[5], a6[5],"|", a7[5], a8[5], a9[5])
    print("---------------------")
    print(a1[6], a2[6], a3[6],"|", a4[6], a5[6], a6[6],"|", a7[6], a8[6], a9[6])
    print(a1[7], a2[7], a3[7],"|", a4[7], a5[7], a6[7],"|", a7[7], a8[7], a9[7])
    print(a1[8], a2[8], a3[8],"|", a4[8], a5[8], a6[8],"|", a7[8], a8[8], a9[8])





    #Rows
    r1 = [a1[0], a2[0], a3[0], a4[0], a5[0], a6[0], a7[0], a8[0], a9[0]]
    r2 = [a1[1], a2[1], a3[1], a4[1], a5[1], a6[1], a7[1], a8[1], a9[1]]
    r3 = [a1[2], a2[2], a3[2], a4[2], a5[2], a6[2], a7[2], a8[2], a9[2]]
    r4 = [a1[3], a2[3], a3[3], a4[3], a5[3], a6[3], a7[3], a8[3], a9[3]]
    r5 = [a1[4], a2[4], a3[4], a4[4], a5[4], a6[4], a7[4], a8[4], a9[4]]
    r6 = [a1[5], a2[5], a3[5], a4[5], a5[5], a6[5], a7[5], a8[5], a9[5]]
    r7 = [a1[6], a2[6], a3[6], a4[6], a5[6], a6[6], a7[6], a8[6], a9[6]]
    r8 = [a1[7], a2[7], a3[7], a4[7], a5[7], a6[7], a7[7], a8[7], a9[7]]
    r9 = [a1[8], a2[8], a3[8], a4[8], a5[8], a6[8], a7[8], a8[8], a9[8]]
    #Cols
    c1 = [a1[0], a1[1], a1[2], a1[3], a1[4], a1[5], a1[6], a1[7], a1[8]]
    c2 = [a2[0], a2[1], a2[2], a2[3], a2[4], a2[5], a2[6], a2[7], a2[8]]
    c3 = [a3[0], a3[1], a3[2], a3[3], a3[4], a3[5], a3[6], a3[7], a3[8]]
    c4 = [a4[0], a4[1], a4[2], a4[3], a4[4], a4[5], a4[6], a4[7], a4[8]]
    c5 = [a5[0], a5[1], a5[2], a5[3], a5[4], a5[5], a5[6], a5[7], a5[8]]
    c6 = [a6[0], a6[1], a6[2], a6[3], a6[4], a6[5], a6[6], a6[7], a6[8]]
    c7 = [a7[0], a7[1], a7[2], a7[3], a7[4], a7[5], a7[6], a7[7], a7[8]]
    c8 = [a8[0], a8[1], a8[2], a8[3], a8[4], a8[5], a8[6], a8[7], a8[8]]
    c9 = [a9[0], a9[1], a9[2], a9[3], a9[4], a9[5], a9[6], a9[7], a9[8]]
    #3x3 boxes from Left to right
    b1 = [a1[0], a2[0], a3[0], a1[1], a2[1], a3[1], a1[2], a2[2], a3[2]]
    b2 = [a4[0], a5[0], a6[0], a4[1], a5[1], a6[1], a4[2], a5[2], a6[2]]
    b3 = [a7[0], a8[0], a9[0], a7[1], a8[1], a9[1], a7[2], a8[2], a9[2]]
    b4 = [a1[3], a2[3], a3[3], a1[4], a2[4], a3[4], a1[5], a2[5], a3[5]]
    b5 = [a4[3], a5[3], a6[3], a4[4], a5[4], a6[4], a4[5], a5[5], a6[5]]
    b6 = [a7[3], a8[3], a9[3], a7[4], a8[4], a9[4], a7[5], a8[5], a9[5]]
    b7 = [a1[6], a2[6], a3[6], a1[7], a2[7], a3[7], a1[8], a2[8], a3[8]]
    b8 = [a4[6], a5[6], a6[6], a4[7], a5[7], a6[7], a4[8], a5[8], a6[8]]
    b9 = [a7[6], a8[6], a9[6], a7[7], a8[7], a9[7], a7[8], a8[8], a9[8]]
    print("\n")

#Start with rows 1-9
count = 1
for rone in r1, r2, r3, r4, r5, r6, r7, r8, r9: #Checks the rows initially 1-9
    #Check elements 1-9 for a 0
    for element in rone:
        if element == 0:
            #Count represents the number that is being looked for
            while count !=9:
                #Reset Option (Option=0)
                Option = 0
                #Check Block
                for bone in b1, b2, b3, b4, b5, b6, b7, b8, b9: #Check block for number
                    for element in bone:
                        #If there is an element 1-9 when checking 3x3 block that the selected element is in break out of loop and
                            #increment count to next number to check for
                        if element == count: #supposed element in block already

                            count += 1 #++count

                            break
                        else:
                                Option += 1
                                #Check Row
                                for rone in r1, r2, r3, r4, r5, r6, r7, r8, r9: #Check Row for number
                                    for element in rone:
                                        #If there is an element 1-9 when checking row that the selected element is in break out of loop and
                                        #increment count to next number to check for
                                        if element == count: #supposed elemnet in row already
                                            count += 1

                                            break #or Pass or break
                                        else:
                                            Option += 1
                                            for cone in c1, c2, c3, c4, c5, c6, c7, c8, c9: #Check column for number
                                                for element in cone:
                                                    #If there is an element 1-9 when columnthat the selected element is in break out of loop and
                                                    #increment count to next number to check for
                                                    if element == count: #supposed elemnet in column already
                                                        count += 1
                                                        break
                                                    else:
                                                        #The number is not in the row, column, or block so theres an option
                                                        Option += 1
                                                        break
                        if Option == 1:
                            #Place number in elemnent
                            #Replace element with count
                            count = element

                            count = 0
                        else:
                            #Else more than one option, loop to count and ++count
                            count += 1
                            break

                    count += 1


                    break
                continue
print("New Board", rone)
dabrams493
  • 61
  • 6
  • You can't expect to write a puzzle solver from scratch, and have it work straight away. Add print statements into your code as you write it, make small changes, and make sure they work. That way, when you come to Stack Overflow, you will have a very specific question and a lot less code that we need to look through to answer it. – Andrew Williamson Feb 01 '16 at 23:05
  • Also, better use nested lists instead of multiple sets of nine variables each holding a single list. – tobias_k Feb 01 '16 at 23:10

2 Answers2

2

First of all, I think that replacing the old board with a new one after every iteration would be too "expensive" and not good programming practice in general.

Below, I've provided you with a basic algorithm you can use to solve a 9 by 9 Sudoku board. Do what you will with it.'

Disp 81 emtpy square grids;
Fill grids with some known numbers;

while(there are empty square){
    if(top left square is empty){
        create a number x;
        if(no other numbers x are in row/column/mini grid){
            plug in x;
        }
    } else{
        while(row number < 9){
            go to next row;
            if(row number == 9){
                row  number = 1;
                next column;
            }
        }
    }
}

I realize that the algorithm I've provided you with might be a bit to basic, but it's a start.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
0

It prints only one row because you explicitly told it to print only that last row, once you were done with the entire outer loop. If you indent the statement to get it inside the rone loop, you'll see the rows printed in the order with which you treated them:

Old Board
(1, 4, 0, '|', 0, 8, 0, '|', 0, 3, 9)
(0, 3, 0, '|', 0, 0, 0, '|', 0, 1, 2)
(0, 0, 0, '|', 1, 0, 0, '|', 0, 0, 4)
---------------------
(6, 7, 9, '|', 2, 5, 8, '|', 1, 4, 3)
(4, 0, 0, '|', 0, 9, 1, '|', 0, 0, 0)
(2, 0, 1, '|', 0, 0, 4, '|', 0, 6, 5)
---------------------
(9, 0, 0, '|', 0, 1, 3, '|', 0, 0, 0)
(5, 1, 0, '|', 0, 0, 0, '|', 0, 9, 0)
(3, 6, 0, '|', 0, 4, 0, '|', 0, 8, 0)


('New Board', [3, 6, 0, 0, 4, 0, 0, 8, 0])
('New Board', [0, 3, 0, 0, 0, 0, 0, 1, 2])
('New Board', [0, 0, 0, 1, 0, 0, 0, 0, 4])
('New Board', [6, 7, 9, 2, 5, 8, 1, 4, 3])
('New Board', [4, 0, 0, 0, 9, 1, 0, 0, 0])
('New Board', [2, 0, 1, 0, 0, 4, 0, 6, 5])
('New Board', [9, 0, 0, 0, 1, 3, 0, 0, 0])
('New Board', [5, 1, 0, 0, 0, 0, 0, 9, 0])
('New Board', [3, 6, 0, 0, 4, 0, 0, 8, 0])

That solves your immediate question. However, you will have more problems just after this one. The most obvious is that you didn't get one part of the code working before you went on to the next -- you seem to have coded this in one large block, making it much harder to debug. For one thing, you change the rone loop varaible from within the loop: you have a nested loop that alters rone, under the else clause at line 102. This seriously disturbs the loop flow for your outermost loop.

Next, you continue to iterate through the original board: b1-b9, r1-r9, and c1-c9. As far as I can see, you don't update these at all as you solve the puzzle.

Overall, you seem to have overreached your programming abilities with a larger project. You don't yet think in terms of lists, and you don't mentally organize tasks in terms of basic data structures. I strongly recommend that you work on some smaller projects first, and then come back to this one when you've exercised those basic skill some more. Don't throw this away -- it's a wonderful learning experience. I kept my first class projects, and I still return to them when learning a new language. In fact, I assign them to my own students, and often show them the mistakes I made (after giving them time to find their own mistakes).

If you're looking for problems to attack, you might start with the ones in Project Euler. It's a lovely collection of algorithm puzzles that gets generally harder, with a new problem posted every week. you'll build up some software tools ... and one of the problems is a nice Sudoku solver.

Prune
  • 76,765
  • 14
  • 60
  • 81