Home > Mobile >  How to print Sudoku board using Python class?
How to print Sudoku board using Python class?

Time:05-08

Exactly as my post title says, I'm trying to create a class to solve Sudoku game and I wanted to print a board using OOP style and if name == "main":

My output suppose to look like this: enter image description here

Here's my code so far:

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)

And here's the output that I'm getting from the above code:

enter image description here

Thank you in advance.

CodePudding user response:

The problem is that the 'columns' loop isn't nested inside the 'rows' loop, your code should look something like this:

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)

CodePudding user response:


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()

OUTPUT

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

CodePudding user response:

You could have a look at the __format_board_ascii method in py-sudoku.

  • Related