如何使用 Python class 打印数独板?

How to print Sudoku board using Python class?

正如我的 post 标题所说,我正在尝试创建一个 class 来解决数独游戏,我想使用 OOP 样式和 if name == "main":

我的输出应该是这样的:

到目前为止,这是我的代码:

class Sudoku:
    def __init__(self):
        self.board = [
            [7, 8, 0, 4, 0, 0, 1, 2, 0],
            [6, 0, 0, 0, 7, 5, 0, 0, 9],
            [0, 0, 0, 6, 0, 1, 0, 7, 8],
            [0, 0, 7, 0, 4, 0, 2, 6, 0],
            [0, 0, 1, 0, 5, 0, 9, 3, 0],
            [9, 0, 4, 0, 6, 0, 0, 0, 5],
            [0, 7, 0, 3, 0, 0, 0, 1, 2],
            [1, 2, 0, 0, 0, 7, 4, 0, 0],
            [0, 4, 9, 2, 0, 6, 0, 0, 7]
        ]

    def print_board(self, board) -> None:
        for row in range(len(board)):
            if row % 3 == 0 and row != 0:
                print("- - - - - - - - - - - - - ")

        for col in range(len(board[0])):
            if col % 3 == 0 and col != 0:
                print(" | ", end="")

            if col == 8:
                print(board[row][col])
            else:
                print(str(board[row][col]) + " ", end="")

    # Not sure if we need string representation of the board
    # def __str__(self):
    #     return f"{self.board}"


if __name__ == "__main__":
    sudoku = Sudoku()
    sudoku.print_board(sudoku.board)

这是我从上面的代码得到的输出:

提前谢谢你。

问题是 'columns' 循环没有嵌套在 'rows' 循环中,您的代码应该如下所示:

class Sudoku:
def __init__(self):
    self.board = [
        [7, 8, 0, 4, 0, 0, 1, 2, 0],
        [6, 0, 0, 0, 7, 5, 0, 0, 9],
        [0, 0, 0, 6, 0, 1, 0, 7, 8],
        [0, 0, 7, 0, 4, 0, 2, 6, 0],
        [0, 0, 1, 0, 5, 0, 9, 3, 0],
        [9, 0, 4, 0, 6, 0, 0, 0, 5],
        [0, 7, 0, 3, 0, 0, 0, 1, 2],
        [1, 2, 0, 0, 0, 7, 4, 0, 0],
        [0, 4, 9, 2, 0, 6, 0, 0, 7]
    ]

def print_board(self, board) -> None:
    for row in range(len(board)):
        if row % 3 == 0 and row != 0:
            print("- - - - - - - - - - - - - ")
        for col in range(len(board[0])):
            if col % 3 == 0 and col != 0:
                print(" | ", end="")

            if col == 8:
                print(board[row][col])
            else:
                print(str(board[row][col]) + " ", end="")

if __name__ == "__main__":
    sudoku = Sudoku()
    sudoku.print_board(sudoku.board)

class Sudoku:
    def __init__(self):
        self.board = [
            [7, 8, 0, 4, 0, 0, 1, 2, 0],
            [6, 0, 0, 0, 7, 5, 0, 0, 9],
            [0, 0, 0, 6, 0, 1, 0, 7, 8],
            [0, 0, 7, 0, 4, 0, 2, 6, 0],
            [0, 0, 1, 0, 5, 0, 9, 3, 0],
            [9, 0, 4, 0, 6, 0, 0, 0, 5],
            [0, 7, 0, 3, 0, 0, 0, 1, 2],
            [1, 2, 0, 0, 0, 7, 4, 0, 0],
            [0, 4, 9, 2, 0, 6, 0, 0, 7]
        ]

    def print_board(self) -> None:
        for index,a in enumerate(self.board):
            a.insert(3,'|')
            a.insert(7,'|')
            print(*a)
            if (index+1)%3==0 and len(self.board)-1>index:
                print("-"*21)

if __name__ == '__main__':
    sudoku = Sudoku()
    sudoku.print_board()

输出

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

您可以查看 py-sudoku 中的 __format_board_ascii 方法。