Add load() for loading sudokus from text

This commit is contained in:
Camden Dixie O'Brien 2022-11-23 22:17:38 +00:00
parent 0ca01985cd
commit 9bc49daf4c
2 changed files with 19 additions and 0 deletions

12
sud.c
View File

@ -35,6 +35,18 @@ static void initposs(struct sudoku *sud)
} }
} }
bool load(struct sudoku *sud, const char *ptr)
{
initposs(sud);
for (unsigned i = 0; i < NCELLS; ++i) {
if (ptr[i] == '0')
continue;
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) static void clear(struct sudoku *sud, unsigned r, unsigned c)
{ {
unsigned tr, tc, val; unsigned tr, tc, val;

7
sud.h
View File

@ -23,6 +23,7 @@
#define SEGLEN 3 #define SEGLEN 3
#define NDIGITS (SEGLEN * SEGLEN) #define NDIGITS (SEGLEN * SEGLEN)
#define NCELLS (NDIGITS * NDIGITS)
struct cellstate { struct cellstate {
bool det; bool det;
@ -40,6 +41,12 @@ enum update_res { NOT_ALLOWED, ALREADY_DET, OK };
enum check_res { INCOMPLETE, INCORRECT, SOLVED }; enum check_res { INCOMPLETE, INCORRECT, SOLVED };
/**
* Read `NCELLS` values from the given pointer and load them into the
* sudoku.
*/
bool load(struct sudoku *sud, const char *ptr);
/** /**
* Populate the sudoku with some random values. The proportion of * Populate the sudoku with some random values. The proportion of
* cells that are filled will be approximately `fill_prop`. * cells that are filled will be approximately `fill_prop`.