From 06bba6ca68ec45412bf238007450c73e36e1685f Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 21 Dec 2020 21:00:42 +0100 Subject: [PATCH] sudoku: constraints: add solved predicate --- src/constraints.c | 18 ++++++++++++++++++ src/constraints.h | 2 ++ 2 files changed, 20 insertions(+) diff --git a/src/constraints.c b/src/constraints.c index c2edcc2..8ecb387 100644 --- a/src/constraints.c +++ b/src/constraints.c @@ -48,3 +48,21 @@ bool square_violation(const struct sudoku *grid, size_t i, size_t j) { return false; } + +bool solved(const struct sudoku *grid) { + if (!grid) + return false; + + for (size_t i = 0; i < 9; ++i) { + for (size_t j = 0; j < 9; ++j) { + if (line_violation(grid, i, j)) + return false; + if (column_violation(grid, i, j)) + return false; + if (square_violation(grid, i, j)) + return false; + } + } + + return true; +} diff --git a/src/constraints.h b/src/constraints.h index 09e73a0..618448c 100644 --- a/src/constraints.h +++ b/src/constraints.h @@ -10,4 +10,6 @@ bool line_violation(const struct sudoku *grid, size_t i, size_t j); bool column_violation(const struct sudoku *grid, size_t i, size_t j); bool square_violation(const struct sudoku *grid, size_t i, size_t j); +bool solved(const struct sudoku *grid); + #endif /* !CONSTRAINTS_H */