0

I have a 9x9 sudoku grid that I've drawn using ncurses' (mvw)hline/vline functions and I'm now trying to populate it using numbers from a 9x9 array. My question is what is the most effective/efficient way to do this?

If it's helpful, I'm using ncurses v6.1-r2 and C++17 (compiled with g++). I've gotten about as far as looping through the array (from which the board will be populated), but after that I can't think of a way to populate each square.

Here is the code I'm using to draw the grid.

WINDOW *create_board(int x, int y, int width, int height) {
    WINDOW *local_win;

    local_win = newwin(height, width, y, x);

    mvwaddch(local_win, 0,          0,         '+');
    mvwaddch(local_win, 0,          width - 1, '+');
    mvwaddch(local_win, height - 1, 0,         '+');
    mvwaddch(local_win, height - 1, width - 1, '+');

    mvwhline(local_win, 0,          1, '-', width - 2);
    mvwhline(local_win, height - 1, 1, '-', width - 2);

    mvwvline(local_win, 1,      0,         '|', height - 2);
    mvwvline(local_win, 1,      width - 1,     '|', height - 2);

    for (int i = 0; i < 9; i++) {
        mvwvline(local_win, 1,          (10 * i), '|', height - 2);
        mvwhline(local_win, (4.2 * i),  1,        '-', width - 2);
    }

    wrefresh(local_win);
    return local_win; 
}

I apologize if the wording is awkward, English is not my first language.

EDIT: for clarity, I do not want to generate a sudoku board, I simply want to populate an existing grid with predefined numbers in an array. For example:

int board[9][9] =  
   {{0,0,0,7,0,0,0,0,0},
    {1,0,0,0,0,0,0,0,0},
    {0,0,0,4,3,0,2,0,0},
    {0,0,0,0,0,0,0,6,0},
    {0,0,5,0,9,0,0,0,0},
    {0,0,0,0,0,4,1,8,0},
    {0,0,0,8,1,0,0,0,0},
    {0,2,0,0,0,0,5,0,0},
    {4,0,0,0,0,3,0,0,0}};
PavelF
  • 321
  • 1
  • 2
  • 11
  • Possible duplicate of [Creating sudoku initial boards](https://stackoverflow.com/questions/3218223/creating-sudoku-initial-boards) – Kristopher Ives Mar 24 '19 at 16:50
  • @KristopherIves I don't want to generate a board, I want to populate it with a predefined array. That is, I want to put numbers in the boxes, what the number is doesn't really matter for now. – PavelF Mar 24 '19 at 17:30

0 Answers0