Implement basic generation and printing
I expect this will probably generate some impossible puzzles, but I'll deal with that later.
This commit is contained in:
parent
abc98edac9
commit
512580ab27
5
Makefile
5
Makefile
@ -17,7 +17,7 @@
|
|||||||
CFLAGS += -std=c11 -pedantic -Wall -Wextra
|
CFLAGS += -std=c11 -pedantic -Wall -Wextra
|
||||||
CFLAGS += -O2 -flto
|
CFLAGS += -O2 -flto
|
||||||
|
|
||||||
SRC = main.c
|
SRC = main.c sud.c
|
||||||
OBJ = $(SRC:.c=.o)
|
OBJ = $(SRC:.c=.o)
|
||||||
|
|
||||||
sudoku: $(OBJ)
|
sudoku: $(OBJ)
|
||||||
@ -26,4 +26,7 @@ sudoku: $(OBJ)
|
|||||||
clean:
|
clean:
|
||||||
rm -f sudoku $(OBJ)
|
rm -f sudoku $(OBJ)
|
||||||
|
|
||||||
|
main.o: sud.h
|
||||||
|
sud.o: sud.h
|
||||||
|
|
||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
|
14
main.c
14
main.c
@ -16,7 +16,21 @@
|
|||||||
* <https://www.gnu.org/licenses/>.
|
* <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "sud.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
|
unsigned seed = time(NULL);
|
||||||
|
printf("Seed: %u\n\n", seed);
|
||||||
|
srand(seed);
|
||||||
|
|
||||||
|
struct sudoku sud;
|
||||||
|
gen(&sud);
|
||||||
|
print(&sud);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
125
sud.c
Normal file
125
sud.c
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
* <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "sud.h"
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define FILLP_RCP 3
|
||||||
|
|
||||||
|
static void initposs(struct sudoku *sud)
|
||||||
|
{
|
||||||
|
for (unsigned r = 0; r < SIDELEN; ++r) {
|
||||||
|
for (unsigned c = 0; c < SIDELEN; ++c) {
|
||||||
|
sud->cells[r][c].det = false;
|
||||||
|
for (unsigned i = 0; i < NDIGITS; ++i)
|
||||||
|
sud->cells[r][c].pvals[i] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void gen(struct sudoku *sud)
|
||||||
|
{
|
||||||
|
initposs(sud);
|
||||||
|
|
||||||
|
for (unsigned r = 0; r < SIDELEN; ++r) {
|
||||||
|
for (unsigned c = 0; c < SIDELEN; ++c) {
|
||||||
|
if (rand() % FILLP_RCP == 0) {
|
||||||
|
assert(!sud->cells[r][c].det);
|
||||||
|
|
||||||
|
unsigned val, n = 0;
|
||||||
|
do {
|
||||||
|
val = (unsigned)(rand() % NDIGITS);
|
||||||
|
++n;
|
||||||
|
} while (update(sud, r, c, val) == NOT_ALLOWED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum update_res update(struct sudoku *sud, unsigned r, unsigned c, unsigned val)
|
||||||
|
{
|
||||||
|
unsigned tr, tc;
|
||||||
|
assert(val < NDIGITS);
|
||||||
|
|
||||||
|
/* Check that the cell is undetermined and the value is allowed. */
|
||||||
|
if (sud->cells[r][c].det)
|
||||||
|
return ALREADY_DET;
|
||||||
|
if (!sud->cells[r][c].pvals[val])
|
||||||
|
return NOT_ALLOWED;
|
||||||
|
|
||||||
|
/* Update possible values of cells in same column. */
|
||||||
|
for (tr = 0; tr < SIDELEN; ++tr) {
|
||||||
|
if (tr == r || sud->cells[tr][c].det)
|
||||||
|
continue;
|
||||||
|
sud->cells[tr][c].pvals[val] = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Update possible values of cells in same row. */
|
||||||
|
for (tc = 0; tc < SIDELEN; ++tc) {
|
||||||
|
if (tc == c || sud->cells[r][tc].det)
|
||||||
|
continue;
|
||||||
|
sud->cells[r][tc].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;
|
||||||
|
sud->cells[tr][tc].pvals[val] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Set the cell's value. */
|
||||||
|
sud->cells[r][c].det = true;
|
||||||
|
sud->cells[r][c].val = val;
|
||||||
|
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print(const struct sudoku *sud)
|
||||||
|
{
|
||||||
|
for (unsigned r = 0; r < SIDELEN; ++r) {
|
||||||
|
/*
|
||||||
|
* Print horizontal divider if on a segment boundary (but not
|
||||||
|
* at the start).
|
||||||
|
*/
|
||||||
|
if (r != 0 && r % SEGLEN == 0)
|
||||||
|
puts("------+-------+------");
|
||||||
|
|
||||||
|
for (unsigned c = 0; c < SIDELEN; ++c) {
|
||||||
|
/*
|
||||||
|
* Print vertical divider if on a segment boundary (but
|
||||||
|
* not at the start).
|
||||||
|
*/
|
||||||
|
if (c != 0 && c % SEGLEN == 0)
|
||||||
|
fputs("| ", stdout);
|
||||||
|
|
||||||
|
if (sud->cells[r][c].det)
|
||||||
|
printf("%u ", sud->cells[r][c].val + 1);
|
||||||
|
else
|
||||||
|
fputs(" ", stdout);
|
||||||
|
}
|
||||||
|
putchar('\n');
|
||||||
|
}
|
||||||
|
}
|
61
sud.h
Normal file
61
sud.h
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
* <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef SUD_H
|
||||||
|
#define SUD_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#define SIDELEN 9
|
||||||
|
#define SEGLEN 3
|
||||||
|
#define NDIGITS 9
|
||||||
|
|
||||||
|
struct cellstate {
|
||||||
|
bool det;
|
||||||
|
union {
|
||||||
|
unsigned val;
|
||||||
|
bool pvals[NDIGITS];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
struct sudoku {
|
||||||
|
struct cellstate cells[SIDELEN][SIDELEN];
|
||||||
|
};
|
||||||
|
|
||||||
|
enum update_res { NOT_ALLOWED, ALREADY_DET, OK };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Populate the sudoku with some random values.
|
||||||
|
*/
|
||||||
|
void gen(struct sudoku *sud);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
|
||||||
|
#endif
|
Loading…
x
Reference in New Issue
Block a user