Remove gen() function

This commit is contained in:
Camden Dixie O'Brien 2022-11-23 23:46:18 +00:00
parent 2ca91dec99
commit e2838d89e3

65
sud.c
View File

@ -44,73 +44,10 @@ bool load(struct sudoku *sud, const char *ptr)
if (update(sud, i / NDIGITS, i % NDIGITS, ptr[i] - '1') != OK)
return false;
}
return true;
}
static void clear(struct sudoku *sud, unsigned r, unsigned c)
{
unsigned tr, tc, val;
/* Set undetermined with all possible values */
sud->cells[r][c].det = false;
for (unsigned i = 0; i < NDIGITS; ++i)
sud->cells[r][c].pvals[i] = true;
/* Update cell's possible values based off column. */
for (tr = 0; tr < NDIGITS; ++tr) {
if (tr == r || !sud->cells[tr][c].det)
continue;
val = sud->cells[tr][c].val;
sud->cells[r][c].pvals[val] = false;
}
/* Update possible values of cells in same row. */
for (tc = 0; tc < NDIGITS; ++tc) {
if (tc == c || !sud->cells[r][tc].det)
continue;
val = sud->cells[r][tc].val;
sud->cells[r][c].pvals[val] = false;
}
/* Update possible values of cells in same segment. */
const unsigned segr0 = SEGLEN * (r / SEGLEN);
const unsigned segc0 = SEGLEN * (c / SEGLEN);
for (tr = segr0; tr < segr0 + SEGLEN; ++tr) {
for (tc = segc0; tc < segc0 + SEGLEN; ++tc) {
if ((tr == r && tc == c) || !sud->cells[tr][tc].det)
continue;
val = sud->cells[tr][tc].val;
sud->cells[r][c].pvals[val] = false;
}
}
}
void gen(struct sudoku *sud, double fill_prop)
{
/* Generate a completed sudoku. */
retry:
initposs(sud);
for (unsigned r = 0; r < NDIGITS; ++r) {
for (unsigned c = 0; c < NDIGITS; ++c) {
unsigned val, n = 0;
do {
val = (unsigned)(rand() % NDIGITS);
++n;
if (n >= MAX_FILL_ATTEMPTS)
goto retry;
} while (update(sud, r, c, val) == NOT_ALLOWED);
}
}
/* Remove cells with probability of 1 - fill_prop. */
for (unsigned r = 0; r < NDIGITS; ++r) {
for (unsigned c = 0; c < NDIGITS; ++c) {
if ((double)rand() / (double)RAND_MAX < 1 - fill_prop)
clear(sud, r, c);
}
}
}
enum update_res update(struct sudoku *sud, unsigned r, unsigned c, unsigned val)
{
unsigned tr, tc;