From 6bc7fdab077b1877f71b69f4b2c1e8046a302e9b Mon Sep 17 00:00:00 2001 From: David Phillips Date: Wed, 9 Mar 2016 00:21:35 +1300 Subject: Initial working copy, buggy --- .gitignore | 2 + Makefile | 8 +++ cell.h | 7 +++ display.c | 26 +++++++++ display.h | 1 + solve.c | 174 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 218 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 cell.h create mode 100644 display.c create mode 100644 display.h create mode 100644 solve.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b22666a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +solve diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..09fc878 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +CFLAGS += -Werror -Wall + +all: solve + +solve: display.o + +clean: + rm -f *.o solve diff --git a/cell.h b/cell.h new file mode 100644 index 0000000..76ecd90 --- /dev/null +++ b/cell.h @@ -0,0 +1,7 @@ +struct cell +{ + char val; + char not[9]; +} cell; + + diff --git a/display.c b/display.c new file mode 100644 index 0000000..f147570 --- /dev/null +++ b/display.c @@ -0,0 +1,26 @@ +#include "cell.h" + +#include + +void display(struct cell board[9][9]) +{ + int x; + int y; + int val; + for (y = 0; y < 9; y++) + { + if (y % 3 == 0 && y != 0) + printf("------+------+------\n"); + for (x = 0; x < 9; x++) + { + if (x % 3 == 0 && x != 0) + printf("|"); + + val = board[x][y].val; + + printf("%c ", val == 0? ' ' : val+'0'); + } + printf("\n"); + } + printf("\n"); +} diff --git a/display.h b/display.h new file mode 100644 index 0000000..b3b5a3d --- /dev/null +++ b/display.h @@ -0,0 +1 @@ +void display(struct cell board[9][9]); diff --git a/solve.c b/solve.c new file mode 100644 index 0000000..f8947c9 --- /dev/null +++ b/solve.c @@ -0,0 +1,174 @@ +#include +#include + +#include "cell.h" +#include "display.h" + +void update_not_row_col(struct cell b[9][9]) +{ + int x = 0; + int y = 0; + int i = 0; + int val = 0; + + for (y = 0; y < 9; y++) + { + for (x = 0; x < 9; x++) + { + val = b[x][y].val; + if (val != 0) + { + for (i = 0; i < 9; i++) + { + b[i][y].not[val - 1] = 1; + b[x][i].not[val - 1] = 1; + } + } + } + } +} + +void not_to_val_row_col(struct cell b[9][9]) +{ + int x = 0; + int y = 0; + int i = 0; + int val = 0; + int total = 0; + + for (y = 0; y < 9; y++) + { + total = 0; + for (x = 0; x < 9; x++) + { + if (b[x][y].val != 0) + continue; + + for (i = 0; i < 9; i++) + { + if (b[x][y].not[i]) + { + total++; + } else { + val = i + 1; + } + } + if (total == 8) + b[x][y].val = val; + } + } +} + + +void not_to_val_cell(struct cell b[9][9]) +{ + int cx = 0; + int cy = 0; + int x = 0; + int y = 0; + int n = 0; + int potentials = 0; + int okx, oky; + struct cell c; + + /* refactor this crap */ + for (cy = 0; cy < 9; cy += 3) + { + for (cx = 0; cx < 9; cx += 3) + { + /* for each val that has to be in cell */ + for (n = 1; n <= 9; n++) + { + for (y = 0; y < 3; y++) + { + for (x = 0; x < 3; x++) + { + c = b[cx+x][cy+y]; + if (c.val == n) + { + potentials = 0; + y = 3; + break; + } + if (c.val) + continue; + + if (c.not[n-1] == 0) + { + potentials++; + okx = x; + oky = y; + } + } + } + if (potentials == 1) + { + b[cx+okx][cy+oky].val = n; + potentials = 0; + } + } + } + } +} + +int main(int argc, char **argv) +{ +// int x = 0; +// int y = 0; + struct cell board[9][9]; + + memset(board, 0, sizeof(board)); + + + board[7][0].val = 3; + board[4][1].val = 3; + board[0][7].val = 3; + board[1][4].val = 3; + + /* dummy board taken from the guardian lol */ + /* FIXME: have a way to input boards, silly */ + board[0][0].val = 7; + board[0][4].val = 5; + + board[1][2].val = 5; + board[1][4].val = 9; + + board[2][1].val = 6; + board[2][2].val = 4; + board[2][5].val = 2; + + board[3][0].val = 1; + board[3][5].val = 9; + board[3][6].val = 7; + + board[4][0].val = 9; + board[4][2].val = 6; + board[4][3].val = 4; + board[4][7].val = 8; + board[4][8].val = 5; + + board[5][4].val = 2; + + board[6][4].val = 8; + board[6][6].val = 5; + board[6][7].val = 6; + + board[7][1].val = 8; + board[7][6].val = 3; + + board[8][0].val = 4; + board[8][4].val = 1; + board[8][5].val = 7; + board[8][8].val = 8; + + + + display(board); + + update_not_row_col(board); + not_to_val_cell(board); + not_to_val_row_col(board); + + display(board); + +} -- cgit v1.1