Compare commits
4 Commits
13be9261ac
...
c965f65422
| Author | SHA1 | Date | |
|---|---|---|---|
| c965f65422 | |||
| 330c42c4da | |||
| 69daa07b63 | |||
| a5b6c52053 |
35
src/sudoku.c
35
src/sudoku.c
@@ -8,11 +8,38 @@
|
|||||||
|
|
||||||
#include "util/intdef.h"
|
#include "util/intdef.h"
|
||||||
|
|
||||||
|
void sudoku_init(u16 *board) {
|
||||||
|
for (uint i = 0; i < SUDOKU_DEPTH_2; i++) {
|
||||||
|
board[i] |= SUDOKU_ALL & -!board[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void sudoku_place(u16 *board, u16 val, uint idx) {
|
||||||
|
uint icol = idx % SUDOKU_DEPTH;
|
||||||
|
uint irow = idx - icol;
|
||||||
|
uint ibox = idx - (idx % SUDOKU_DEPTH_SQRT);
|
||||||
|
for (uint i = 0; i < SUDOKU_DEPTH; i++) {
|
||||||
|
board[irow + i] &= ~val;
|
||||||
|
board[icol + (i * SUDOKU_DEPTH)] &= ~val;
|
||||||
|
board[ibox + (i / SUDOKU_DEPTH_SQRT * SUDOKU_DEPTH) + (i % SUDOKU_DEPTH_SQRT)] &= ~val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void sudoku_solve_step(u16 *board) {
|
void sudoku_solve_step(u16 *board) {
|
||||||
u16 bmask[SUDOKU_DEPTH_2]; // board mask
|
for (uint i = 0; i < SUDOKU_DEPTH_2; i++) {
|
||||||
memcpy(bmask, board, SUDOKU_DEPTH_2 * 2);
|
switch (board[i]) {
|
||||||
// TODO: first, we must somehow loop through the columns
|
case SUDOKU_1:
|
||||||
// TODO: then, we must write to the bmask or do *magic* for the bits designating to 1-9 to be unset if ONLY one of them is set
|
case SUDOKU_2:
|
||||||
|
case SUDOKU_3:
|
||||||
|
case SUDOKU_4:
|
||||||
|
case SUDOKU_5:
|
||||||
|
case SUDOKU_6:
|
||||||
|
case SUDOKU_7:
|
||||||
|
case SUDOKU_8:
|
||||||
|
case SUDOKU_9:
|
||||||
|
sudoku_place(board, board[i], i);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void sudoku_print(const u16 *board) {
|
void sudoku_print(const u16 *board) {
|
||||||
|
|||||||
16
src/sudoku.h
16
src/sudoku.h
@@ -4,8 +4,9 @@
|
|||||||
|
|
||||||
#include "util/intdef.h"
|
#include "util/intdef.h"
|
||||||
|
|
||||||
#define SUDOKU_DEPTH 9 // defines the "depth" of the puzzle.
|
#define SUDOKU_DEPTH_SQRT 3
|
||||||
#define SUDOKU_DEPTH_2 (SUDOKU_DEPTH * SUDOKU_DEPTH) // computes how many squares there are
|
#define SUDOKU_DEPTH (SUDOKU_DEPTH_SQRT * SUDOKU_DEPTH_SQRT)
|
||||||
|
#define SUDOKU_DEPTH_2 (SUDOKU_DEPTH * SUDOKU_DEPTH)
|
||||||
|
|
||||||
/* bitmask */
|
/* bitmask */
|
||||||
enum sudoku_bitmask {
|
enum sudoku_bitmask {
|
||||||
@@ -18,10 +19,17 @@ enum sudoku_bitmask {
|
|||||||
SUDOKU_7 = 0x040,
|
SUDOKU_7 = 0x040,
|
||||||
SUDOKU_8 = 0x080,
|
SUDOKU_8 = 0x080,
|
||||||
SUDOKU_9 = 0x100,
|
SUDOKU_9 = 0x100,
|
||||||
|
SUDOKU_ALL = 0x01FF,
|
||||||
};
|
};
|
||||||
|
|
||||||
/* TODO: write documentation */
|
/* initialises the sudoku board to `SUDOKU_ALL`. (if the tile is empty) */
|
||||||
|
void sudoku_init(u16 *board);
|
||||||
|
|
||||||
|
/* places `val` at `idx` in `board`, and removes `val` from the possibilities in neighbouring tiles */
|
||||||
|
void sudoku_place(u16 *board, u16 val, uint idx);
|
||||||
|
|
||||||
|
/* loops through the board, updating all places that only have a single possibility set. */
|
||||||
void sudoku_solve_step(u16 *board);
|
void sudoku_solve_step(u16 *board);
|
||||||
|
|
||||||
/* TODO: write documentation */
|
/* prints the sudoku board to `stdout` */
|
||||||
void sudoku_print(const u16 *board);
|
void sudoku_print(const u16 *board);
|
||||||
|
|||||||
Reference in New Issue
Block a user