java - Dividing a 2D array into boxes -
i having trouble dividing 2d array boxes, in sudoku. have array of squares in board object, , want divide them 2x3 or 3x3 boxes, box objects have 1d array keep track of squares.
k box number, in 9x9 sudoku, boxes numbered 0 through 8.
int l = 0; for(int i=k*a; i<k*a+a;i++){ for(int j=k*b;j<k*b+b;j++){ narray[l]=brd.getsquare(i,j); brd.getsquare(i,j).setbox(this); l++; }
this gets first box right, goes off after that. i've been thinking hours now, , can't seem wrap head around it. have neat trick this?
so, i'll assume boxes numbered this:
012 345 678
(and boxes consist of 3x3 cells each)
if i
, j
x , y coordinates, you'll need translate above coordinates. like:
0 1 2 3 4 5 6 7 8 x 0 1 2 0 1 2 0 1 2 y 0 0 0 1 1 1 2 2 2
so x = k%3
, y = k/3
.
in actual grid x , y has start 0, 3 , 6 rather 0, 1 , 2, multiply 3.
so should it: (changes depending on coordinate x , y)
int size = 3; int l = 0; for(int = 0; < size; i++){ for(int j = 0; j < size; j++){ int x = + k % size * size; int y = j + k / size * size; narray[l] = brd.getsquare(x, y); brd.getsquare(x, y).setbox(this); l++; } }
Comments
Post a Comment