I am creating a program to solve sudoku which contains a struct for each cell that contains the value of the cell and all possible values for the cell as an int with each bit corresponding to a possible value.
To update every cell I have a function called applyMask which removes the bit corresponding to the number of the cell from all other cells that are affected by this cell. This function works properly in testing however when I loop through all cells a large number is passed instead of the correct number. For example, it will pass x is 0 and y is 2 properly but then instead of passing x is 0 and y is 3 next it will pass x is 4199111 and y is 1. In gdb, the function is passed x is 0 and y is 3 however upon stepping into the function it says that x is 4199111 and y is 1. The function called:
typedef struct Cell
{ int values;
int value;
} cell;
void getSection(int pos, int *section1, int *section2)
{ switch(pos % 3){
case 0:
*section1 = pos + 1;
*section2 = pos + 2;
case 1:
*section1 = pos - 1;
*section2 = pos + 1;
case 2:
*section1 = pos - 2;
*section2 = pos - 1;
}
}
void applyMask(cell sudokuBoard[9][9], int x, int y)
{ int mask = ~(1<<(sudokuBoard[x][y].value-1));
for(int maskP = 0; maskP < 9; maskP++)
{ sudokuBoard[maskP][y].values &= mask;
sudokuBoard[x][maskP].values &= mask;
}
int sectionX1;
int sectionX2;
getSection(x, §ionX1, §ionX2);
int sectionY1;
int sectionY2;
getSection(y, §ionY1, §ionY2);
sudokuBoard[sectionX1][sectionY1].values &= mask;
sudokuBoard[sectionX1][sectionY2].values &= mask;
sudokuBoard[sectionX2][sectionY1].values &= mask;
sudokuBoard[sectionX2][sectionY2].values &= mask;
}
And is called by
for(int i = 0; i < 9; i++)
for(int j = 0; j < 9; j++)
applyMask(sudokuBoard, i, j);