-2

I have pasted only parts of the code that I find difficult, otherwise it's an ordinary 9x9 soduko board. It's a nested list

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


def formatted_board():
 print(f'{board[0][0:3]} \n {board[1][0:3]} \n {board[2][0:3]}')
 print(board[3][0:3],board[4][0:3],board[5][0:3]) 
 print(board[6][0:3],board[7][0:3],board[8][0:3])
 print(board[0][3:6],board[1][1:6],board[2][1:6])
 print(board[3][3:6],board[4][1:6],board[5][1:6])
 print(board[6][3:6],board[7][1:6],board[8][1:6])
 print(board[0][6:9],board[1][6:9],board[2][6:9])
 print(board[3][6:9],board[4][6:9],board[5][6:9])
 print(board[6][6:9],board[7][6:9],board[8][6:9])


def choose_cell():
 row = int(input("choose row:"))
 column = int(input("choose column:"))
 choose_num = int(input("choose a num between 1 og 9:"))
 if 0< choose_num <=9:
    board[row][column] = choose_num
    formatted_board()
else:
    print("illegal num try again")
    choose_cell()

formatted_board() goes into choose_cell(). The first line in formatted_board() prints okay, but I want to print it in the below format every time someone inserts a code:

    0 1 2   3 4 5   6 7 8 
  +-------+-------+-------+
0 | 0 0 6 | 9 0 5 | 0 1 0 |
1 | 9 7 0 | 0 1 2 | 3 0 5 |
2 | 0 2 0 | 0 0 4 | 8 6 0 |
  +-------+-------+-------+
3 | 5 0 3 | 8 0 0 | 0 2 0 |
4 | 0 0 0 | 0 0 0 | 0 0 0 |
5 | 0 8 0 | 0 0 1 | 9 0 7 |
  +-------+-------+-------+
6 | 0 5 4 | 1 0 0 | 0 7 0 |
7 | 2 0 7 | 4 5 0 | 0 9 3 |
8 | 0 6 0 | 7 0 3 | 1 0 0 |
  +-------+-------+-------+
wovano
  • 4,543
  • 5
  • 22
  • 49
Shushue
  • 11
  • 1
  • how did you come up with the indices to use in formatted_board()? they are all over the place.... – pygri Nov 17 '21 at 15:32
  • @pygri , the first line : print(f'{board[0][0:3]} \n {board[1][0:3]} \n {board[2][0:3]}') is the first 3x3 square in the upper left, the numbers 0,0,6,9,7,0,0,2,0. – Shushue Nov 17 '21 at 15:34
  • so, capitalizing on the success of printing the first line correctly, try again printing the second line... – pygri Nov 17 '21 at 15:37

2 Answers2

1

How about this print function:

def niceSudo(board):
    side    = len(board)
    base    = int(side**0.5)
    def expandLine(line):
        return line[0]+line[5:9].join([line[1:5]*(base-1)]*base)+line[9:]
    line0  = "  "+expandLine("╔═══╤═══╦═══╗")
    line1  = "# "+expandLine("║ . │ . ║ . ║ #")
    line2  = "  "+expandLine("╟───┼───╫───╢")
    line3  = "  "+expandLine("╠═══╪═══╬═══╣")
    line4  = "  "+expandLine("╚═══╧═══╩═══╝")

    symbol = " 123456789" if base <= 3 else " ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    nums   = [ [""]+[f"({symbol[-n]})" if n<0 else f" {symbol[n]} "  for n in row]
               for row in board ]
    coord  = "   "+"".join(f" {s}  " for s in symbol[1:side+1])
    lines  = []
    lines.append(coord)
    lines.append(line0)
    for r in range(1,side+1):
        line1n = line1.replace("#",str(symbol[r]))
        lines.append( "".join(n+s for n,s in zip(nums[r-1],line1n.split(" . "))) )
        lines.append([line2,line3,line4][(r%side==0)+(r%base==0)])
    lines.append(coord)
    print(*lines,sep="\n")

Output:

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

niceSudo(test)

    1   2   3   4   5   6   7   8   9  
  ╔═══╤═══╤═══╦═══╤═══╤═══╦═══╤═══╤═══╗
1 ║ 8 │   │   ║   │   │   ║   │   │   ║ 1
  ╟───┼───┼───╫───┼───┼───╫───┼───┼───╢
2 ║   │   │ 3 ║ 6 │   │   ║   │   │   ║ 2
  ╟───┼───┼───╫───┼───┼───╫───┼───┼───╢
3 ║   │ 7 │   ║   │ 9 │   ║ 2 │   │   ║ 3
  ╠═══╪═══╪═══╬═══╪═══╪═══╬═══╪═══╪═══╣
4 ║   │ 5 │   ║   │   │ 7 ║   │   │   ║ 4
  ╟───┼───┼───╫───┼───┼───╫───┼───┼───╢
5 ║   │   │   ║   │ 4 │ 5 ║ 6 │   │   ║ 5
  ╟───┼───┼───╫───┼───┼───╫───┼───┼───╢
6 ║   │   │   ║ 1 │   │   ║   │ 3 │   ║ 6
  ╠═══╪═══╪═══╬═══╪═══╪═══╬═══╪═══╪═══╣
7 ║   │   │ 1 ║   │   │   ║   │ 6 │ 8 ║ 7
  ╟───┼───┼───╫───┼───┼───╫───┼───┼───╢
8 ║   │   │ 8 ║ 5 │   │   ║   │ 1 │   ║ 8
  ╟───┼───┼───╫───┼───┼───╫───┼───┼───╢
9 ║   │ 9 │   ║   │   │   ║ 4 │   │   ║ 9
  ╚═══╧═══╧═══╩═══╧═══╧═══╩═══╧═══╧═══╝
    1   2   3   4   5   6   7   8   9  

The function is good for sudoku sizes up to 36x36 and uses the numbers (or symbols) as coordinates. For pre-set positions, you can give it negative numbers and it will print the symbols between parentheses to indicate that these positions cannot be changed. See here for more info on playing and here for a function to generate a random puzzle.

Alain T.
  • 40,517
  • 4
  • 31
  • 51
0
board = [
    [0,0,6,9,0,5,0,1,0],
    [9,7,0,0,1,2,3,0,5],
    [0,2,0,0,0,4,8,6,0],
    [5,0,3,8,0,0,0,2,0],
    [0,0,0,0,0,0,0,0,0],
    [0,8,0,0,0,1,9,0,7],
    [0,5,4,1,0,0,0,7,0],
    [2,0,7,4,5,0,0,9,3],
    [0,6,0,7,0,3,1,0,0]
]

def print_board(board):
    print("     0 1 2   3 4 5   6 7 8")
    print("   +-------+-------+-------+")
    for i, line in enumerate(board):
        print(f" {i} |", end="")
        for j, cell in enumerate(line):
            print(f" {cell}", end="")
            if (j+1) % 3 == 0:
                print(f" |", end="")
        print(f"\n", end="")
        if (i+1) % 3 == 0:
            print(f"   +-------+-------+-------+")

output :

     0 1 2   3 4 5   6 7 8
   +-------+-------+-------+
 0 | 0 0 6 | 9 0 5 | 0 1 0 |
 1 | 9 7 0 | 0 1 2 | 3 0 5 |
 2 | 0 2 0 | 0 0 4 | 8 6 0 |
   +-------+-------+-------+
 3 | 5 0 3 | 8 0 0 | 0 2 0 |
 4 | 0 0 0 | 0 0 0 | 0 0 0 |
 5 | 0 8 0 | 0 0 1 | 9 0 7 |
   +-------+-------+-------+
 6 | 0 5 4 | 1 0 0 | 0 7 0 |
 7 | 2 0 7 | 4 5 0 | 0 9 3 |
 8 | 0 6 0 | 7 0 3 | 1 0 0 |
   +-------+-------+-------+
AlexisG
  • 2,476
  • 3
  • 11
  • 25