Hello all.
I've written a program to solve sudoku puzzles. I had it working for int, see below.
typedef int element;
typedef element game[GAME];
I changed element into short, and then it exits with a segmentation fault. I then changed element into char and I got an immediate segmentation fault. Does anyone know why this happens? Here's what I think is the critical parts (any help greatly appreciated):
OKplaces* checkfunction ( game const g )
{
int i, j;
OKplaces *possibles = (OKplaces *)malloc(sizeof(OKplaces));
possibles->n = 0;
for(i=0;i<SQUARE;i+=3)
for(j=0;j<SQUARE;j+=3)
{
int l = 0;
element *s, *missingnumbers, *emptyplaces, **rows, **cols, *p;
OKplaces *places = (OKplaces *)malloc(sizeof(OKplaces));
s = extractsquare(&g[i*SQUARE+j]);
missingnumbers = extractmissingnumbers(s);
emptyplaces = extractemptyplaces(s);
rows = extractrows(g, i);
cols = extractcols(g, j);
p = missingnumbers;
while(*p!=0 && p-missingnumbers<SQUARE)
{
l++;
p++;
}
l *= 2;
p = missingnumbers;
places->n = 0;
places->square[0] = i;
places->square[1] = j;
while(*p!=0)
{
int k;
places->value = *p;
int *q = places->coords;
for(k=0;k<l;k+=2)
{
if(!numberisin(rows[emptyplaces[k]], *p) && !numberisin(cols[emptyplaces[k+1]], *p))
{
places->n++;
*q++ = emptyplaces[k];
*q++ = emptyplaces[k+1];
}
}
if(places->n==1)
return places;
else if(possibles->n==0 || places->n<possibles->n)
{
int m, N2 = 2*places->n;
possibles->value = places->value;
possibles->n = places->n;
for(m=0; m<N2; m+=2)
{
possibles->coords[m] = places->coords[m];
possibles->coords[m+1] = places->coords[m+1];
}
possibles->square[0] = places->square[0];
possibles->square[1] = places->square[1];
}
places->n =0;
p++;
}
}
return possibles;
}
int gamecheck ( game g, OKplaces* (f)(), OKplaces* const cand )
{
printf("Placing %d at (%d, %d) in %s square.\n", cand->value, cand->coords[0], cand->coords[1], printwhichsquare(cand->square[0], cand->square[1]));
g[(cand->square[0]+cand->coords[0])*SQUARE+cand->square[1]+cand->coords[1]] = cand->value;
if(gamefull(g))
return 1;
else
{
OKplaces *possibles = f(g);
while(possibles->n)
{
int n1 = possibles->n-1;
OKplaces *position = (OKplaces *)malloc(sizeof(OKplaces));
position->value = possibles->value;
position->n = 1;
position->coords[0] = possibles->coords[2*n1];
position->coords[1] = possibles->coords[2*n1+1];
position->square[0] = possibles->square[0];
position->square[1] = possibles->square[1];
if(possibles->n>1)
printf("Multiple possibles: guessing.\n");
if(gamecheck(g, (*f), position))
return 1;
possibles->n--;
}
}
printf("Wrong: deleting %d at (%d, %d) in %s square.\n", cand->value, cand->coords[0], cand->coords[1], printwhichsquare(cand->square[0], cand->square[1]));
g[(cand->square[0]+cand->coords[0])*SQUARE+cand->square[1]+cand->coords[1]] = 0;
return 0;
}
int gamesolve ( game g )
{
OKplaces *first = checkfunction(g), *cand = (OKplaces *)malloc(sizeof(OKplaces));
int *p = first->coords, *q = cand->coords;
cand->value = first->value;
cand->n = 1;
cand->square[0] = first->square[0];
cand->square[1] = first->square[1];
while(first->n)
{
*q++ = *p++;
*q++ = *p++;
if(gamecheck(g, checkfunction, cand))
return 1;
q = cand->coords;
first->n--;
}
return 0;
}