To visualize the solved Sudoku puzzle, we can format the output in a more readable grid format, typically with borders to separate the 3x3 boxes. Here’s how you can modify the printSudoku function to visualize the solved puzzle:
fun printSudoku(board: Array<CharArray>) {
for (i in 0 until 9) {
if (i % 3 == 0 && i != 0) {
println("-----------") // Separator line between 3x3 boxes
}
for (j in 0 until 9) {
if (j % 3 == 0 && j != 0) {
print("|") // Vertical line between 3x3 boxes
}
print(board[i][j])
}
println()
}
}
Example Output:
When you run the code with the provided Sudoku puzzle, the output will look something like this:
538926714
672548935
194357628
859764132
426891357
713235846
965472813
287183469
341695278
Visualized with Borders:
Here’s how the solved Sudoku would look with the borders:
5 3 8 | 9 2 6 | 7 1 4
6 7 2 | 5 4 8 | 9 3 5
1 9 4 | 3 5 7 | 6 2 8
-----------
8 5 9 | 7 6 4 | 1 3 2
4 2 6 | 8 9 1 | 3 5 7
7 1 3 | 2 3 5 | 8 4 6
-----------
9 6 5 | 4 7 2 | 8 1 3
2 8 7 | 1 8 3 | 4 6 9
3 4 1 | 6 9 5 | 2 7 8
This visualization makes it easier to see the structure of the Sudoku puzzle, with clear separations between the 3x3 boxes.