/* * Copyright (C) 2022 Camden Dixie O'Brien * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public * License along with this program. If not, see * . */ #ifndef SUD_H #define SUD_H #include #define SEGLEN 3 #define NDIGITS (SEGLEN * SEGLEN) #define NCELLS (NDIGITS * NDIGITS) struct cellstate { bool det; union { unsigned val; bool pvals[NDIGITS]; }; }; struct sudoku { struct cellstate cells[NDIGITS][NDIGITS]; }; enum update_res { NOT_ALLOWED, ALREADY_DET, OK }; 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 * cells that are filled will be approximately `fill_prop`. */ void gen(struct sudoku *sud, double fill_prop); /** * Attempt to update the cell at row `r`, column `c` to have the value * `val`. Returns `OK` on success, `ALREADY_DET` if the cell is * already determined or `NOT_ALLOWED` if the cell being `val` would * violate the sudoku rules. */ enum update_res update(struct sudoku *sud, unsigned r, unsigned c, unsigned val); /** * Print a string representation of the sudoku to stdout. */ void print(const struct sudoku *sud); /** * Determine whether the sudoku has been solved correctly, contains * invalid choices or is incomplete. */ enum check_res check(const struct sudoku *sud); #endif