简体   繁体   中英

Python sudoku solver nice lay-out

Maybe you already saw my other question about the sudoku solver i'm writing. For now I'm happy with the this he can. But when I run it it doesn't look like a sudoku. I'm trying to make it look more like a sudoku but I'm not able to come further than this:

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

This is the code that produces this answer:

def print_sudoku(array):
    for i in range(0,9):
        print()
        for j in range(0,9):
            print(array[i][j][0],end=' ')

import pprint
distance = [[[0 for k in range(10)] for j in range(9)] for i in range(9)]

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

print_sudoku(distance)

The only thing I want is that it looks more like a sudoku Thanks already Rudy

def print_sudoku(array):
    for i in range(10):
        print()
        if i%3 == 0:
            print("-" * 19)
            if i == 9:
                break
        print ("|", end='')
        for j in range(9):
            print(array[i][j][0], end=' ' if j % 3 != 2 else '|')

If you want colours you'll need to specify what sort of terminal you are using

This is the function I use to print the sudoku

def PrintSudoku(board):
    y = 0
    for n in board:
        x = 0
        while x != 3:
            print n[x],
            x += 1
        print "|",
        while x != 6:
            print n[x],
            x += 1
        print "|",
        while x != 9:
            print n[x],
            x += 1
        print ""
        y = y + 1
        if y == 3 or y == 6:
            print "------|-------|------"

if you format your sudoku as arrays inside an array, like this

Sudoku = [[0,2,9,4,8,6,0,1,3],
          [1,3,0,7,9,0,4,6,0],
          [0,6,4,0,3,1,7,9,2],
          [3,9,1,6,4,7,2,0,5],
          [4,5,0,2,1,3,0,7,6],
          [0,7,2,9,0,8,0,4,1],
          [9,0,7,8,2,0,6,3,0],
          [5,8,6,0,7,4,1,2,9],
          [2,4,3,1,6,0,8,5,7]]

it should look like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM