Draw a Sodoku Grid in C++

Viewed 51

So I have been writing a program that will take a sodoku from an external text file, fill it into a 2 dimensional array, and then print the solved result. I have all the logic for solving the program finished, but whenever I output the sodoku into the console, it looks like this:

8 1 2 7 5 3 6 4 9
9 4 3 6 8 2 1 7 5
6 7 5 4 9 1 2 8 3
1 5 4 2 3 7 8 9 6
3 6 9 8 4 5 7 2 1
2 8 7 1 6 9 5 3 4
5 2 1 9 7 4 3 6 8
4 3 8 5 2 6 9 1 7
7 9 6 3 1 8 4 5 2

Now if my goal was simply to output the result, this would be perfectly fine, but I want something more easy to read and Sodoku-like.

Ideally I would like for their to be 3x3 squares around all of the necessary blocks in the Sodoku, and make it look something like this or at least something similar:

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

Here I will also include my print function for the sodoku itself:

void print_puzzle()
{
    for (int i = 0; i < 9; i++)
    {
        for (int j = 0; j < 9; j++)
        {
            cout << puzzle[i][j] << " ";
        }
        cout << endl;
    }
}

If anyone could help me out with the specific implementation to achieve my goal that would be extremely helpful

EDIT

I have rewritten the print function of my code to where it outputs the sodoku as the following:

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

I have done so by editing my function to the following:

void printPuzzle()
{
    for (int i = 0; i < 9; i++)
    {
        if (i == 0 || i == 3 || i == 6)
        {
            cout << " ------  ------  ------" << endl;
        }
        for (int j = 0; j < 9; j++)
        {
            if (j == 0 || j == 3 || j == 6)
            {
                cout << "| ";
            }
            cout << puzzle[i][j] << " ";
        }
        cout << "|";
        cout << endl;
    }
    cout << " ------  ------  ------" << endl;
}
0 Answers
Related