三行代码  ›  专栏  ›  技术社区  ›  Mo Fatah

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

  •  1
  • Mo Fatah  · 技术社区  · 1 月前

    正如我的帖子标题所说,我正试图创建一个解决数独游戏的类,我想用OOP风格和 if name == "main":

    我的输出应该是这样的: enter image description here

    以下是我目前的代码:

    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)
    
    

    下面是我从上述代码中得到的输出:

    enter image description here

    提前谢谢你。

    3 回复  |  直到 1 月前
        1
  •  2
  •   Romanos    1 月前

    问题是“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)
    
        2
  •  1
  •   Sharim Iqbal    1 月前
    
    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
    
        3
  •  0
  •   Piotr Ostrowski    1 月前

    你可以看看 __format_board_ascii 方法 py-sudoku .