A short note on arrays and ncurses with a bit of a git.

So ncurses operates on a y,x co-ordinate system.  All of it’s functions expect y first, for example:

getyx(y,x);

which gets the location of the cursor putting the values into y and x.  This sounds a bit counter intuitive to us, we are used to x then y. We think of going across the screen and then down.  So throughout my code I’ve thought of ncurses as counting down the screen then across and my code as counting across the screen and then down.  Which is right and the way I want it. Except when you declare an array as:

int fish[12][9];

You have 12 rows and 9 columns. It is also Y and then X. So a loop such as:


for(int x=0;x

Does go down and across. So although conceptually you are going across then down if you print the array out with the same logic via say printf("%d ",array[x][y]) you'll find you print down and across. I think.... 😛

So I have a sudoku puzzle where the first row at the start is effectively:

7 1

Yet my save routine saved out:


diziet@ono-sendai:~/Programming/C/Sudoku$ cat dm3.sud
0 0 0 0 7 1 0 8 3
0 0 0 0 0 0 2 5 0
0 1 0 0 0 3 4 0 0
7 0 3 5 0 0 0 0 0
0 0 0 0 0 8 9 1 0
0 0 0 9 0 0 0 0 0
0 0 0 0 6 0 1 0 8
1 6 0 0 0 4 0 0 0
0 4 0 0 2 0 3 0 0

So all of my loops have held the data going down and then across the array.   This is not a problem, it changes nothing really except it makes the save files look funny.   So I changed it all thus producing a patch from git with:

git format-patch origin/master

all because ncurses functions put y before x thus making me think it was odd. A snippet from the patch:


@@ -547,12 +547,12 @@ int display_sudoku(WINDOW *wi_sudoku, struct _sudoku *st_sudoku)

getyx(wi_sudoku,old_dy,old_dx);

- for(int x=0; x
+ for(int y=0; y
{
- for(int y=0; y
+ for(int x=0; xcells[x][y].value;
+ cell_value = st_sudoku->cells[y][x].value;

Ho hum.  At least now I can type in a potential sudoku once and keep loading it to test various ideas.  Of course we all know the backtracking algorithm will be the best but it's not the point of this entire exercise anyway.

This entry was posted in Programming and tagged , , , , . Bookmark the permalink.