gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > Using variables to define location in a 2D array to write to

#118813 - neilio - Fri Feb 16, 2007 11:56 pm

...or at least, that could be the source of my query

I'm working on a basic Sudoku program and so far I have a 9 x 9 2D array where each entry describes a cell in the Sudoku grid, which is translated to the screen as sprites. A cursor can be moved around the grid and a number entered into the cell location matching the cursor when the A key is released (at the moment, just a 1).

This works for most of the cells, except for some odd behaviour... basically:
(1) The number entered at grid X 0, grid Y 8 appears at X 1, Y 0 instead
(2) A similar thing happens for the other X locations on Y 8, where the number appears in the next column along and the top row instead
(3) Number entered at X 8, Y 8 appears as expected
(4) If a number is entered at X 8, Y 0 (or X 7, Y 8 with the above bug) then the cursor doesn't respond to the key inputs

I tested the program by forcing numbers into the cells at cell initialisation, at which point bugs (1) and (2) still occur but not (4). (4) only occurs when the cursor is moved on X 8, Y 0 (or X 7, Y 8) and the number entered.

I'm using this code to update the number at the cursor location:

Code:

      if(key_released(KEY_A))
      {
         sudokuGrid[gridCursor.x][gridCursor.y].cellEntry = 1;

         UpdateSprite(&sudokuGrid[gridCursor.x][gridCursor.y]);
      }


...and this is the code that I use to update the cell:

Code:

void UpdateSprite(GridEntry* ent)
{
   ent->gfxID = ent->cellEntry * 4;
   ent->oam->attribute[2] = ent->gfxID;
}


If the problem isn't here, then the rest of the code (and compiled ROM file) can be found here:
Gbasudoku.Zip by Bigupload.Com

Thanks for your help in advance!
_________________
I'd like to think this signature is under development, but it isn't.

#118817 - Cearn - Sat Feb 17, 2007 1:27 am

Sudoku.c wrote:
Code:

   GridEntry sudokuGrid[8][8];

   ...

This is not a 9x9 grid, it's an 8x8 grid. sudokuGrid[8][x] will be out of bounds. (Sidenote: Elements varying in the lower index are next to each other in memory, corresponding to horizontal movement through the matrix. Therefore, C matrices are usually ordered [y][x], rather than [x][y]. )

#118853 - neilio - Sat Feb 17, 2007 3:31 pm

Thanks, problem sorted. I wrongly assumed that when declaring the variable the numbering be from 0 to 8 and not 1 to 9.
_________________
I'd like to think this signature is under development, but it isn't.

#118855 - tepples - Sat Feb 17, 2007 3:44 pm

int x[9][9];
makes active cells from x[0][0] to x[8][8]
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#118861 - keldon - Sat Feb 17, 2007 5:00 pm

neilio wrote:
Thanks, problem sorted. I wrongly assumed that when declaring the variable the numbering be from 0 to 8 and not 1 to 9.


The numbering is from 0-8, but you are declaring that there are 9 elements in the array. The array int array [2] has two elements: array[0] and array[1].