/* * 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 #include #define SEGLEN 3 #define NDIGITS 9 #define NCELLS 81 #define DETMASK 0x8000 #define VALSHIFT 9 #define VALMASK 0x1e00 #define DET(x) (x & DETMASK) #define VAL(x) ((x & VALMASK) >> VALSHIFT) struct sudoku { uint16_t 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); /** * Write the sudoku to the given pointer (`NCELLS` bytes). */ void save(struct sudoku *sud, char *ptr); /** * 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); /** * Determine whether all the sudoku's cells have been determined. */ bool filled(const struct sudoku *sud); #endif