diff --git a/classes.php b/classes.php
index a7cc40e..6bbe3b8 100644
--- a/classes.php
+++ b/classes.php
@@ -52,8 +52,8 @@
$this->columns = array_fill(0, 9, array());
$this->regions = array_fill(0, 9, array());
for ($regionRowId = 0; $regionRowId < 3; $regionRowId++) {
- for($regionColumnId = 0; $regionColumnId < 3; $regionColumnId++) {
- for ($rowId = 3*$regionRowId; $rowId < 3*($regionRowId+1); $rowId++) {
+ for ($rowId = 3*$regionRowId; $rowId < 3*($regionRowId+1); $rowId++) {
+ for($regionColumnId = 0; $regionColumnId < 3; $regionColumnId++) {
for ($columnId = 3*$regionColumnId; $columnId < 3*($regionColumnId+1); $columnId++) {
$regionId = 3*$regionRowId + $regionColumnId;
$box = new Box($rowId, $columnId, $regionId);
@@ -73,14 +73,37 @@
$box->neighbourhood[] = $neighbour;
}
}
+
+ function import($gridStr) {
+ foreach ($this->boxes as $i => $box) {
+ $box->value = $gridStr[$i];
+ }
+ forEach($this->boxes as $box) {
+ forEach($box->neighbourhood as $neighbour)
+ array_unset_value($box->value, $neighbour->candidates);
+ }
+ }
+
+ function containsDuplicates() {
+ foreach(array_merge($this->rows, $this->columns, $this->regions) as $area) {
+ $unknownBoxes = array_filter($area, "isUnknown");
+ foreach($unknownBoxes as $box1) {
+ foreach($unknownBoxes as $box2) {
+ if (($box1 !== $box2) && ($box1->value == $box2->value)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+ }
function generate() {
// Init with a shuffle row
$values = array("1", "2", "3", "4", "5", "6", "7", "8", "9");
shuffle($values);
- forEach($values as $columnId => $value) {
- $box = $this->rows[0][$columnId];
- $box->value = $value;
+ forEach($this->rows[0] as $columnId => $box) {
+ $box->value = $values[$columnId];
forEach($box->neighbourhood as $neighbour)
array_unset_value($box->value, $neighbour->candidates);
}
@@ -145,16 +168,9 @@
if ($randomized) shuffle($testBox->candidates);
$stop = null;
foreach($testBox->candidates as $testBox->value) {
- $correctGrid = true;
foreach(array_filter($testBox->neighbourhood, "isUnknown") as $neighbour)
$neighbour->candidateRemoved[] = array_unset_value($testBox->value, $neighbour->candidates);
- foreach(array_filter($testBox->neighbourhood, "isUnknown") as $neighbour) {
- if (count($neighbour->candidates) == 0) {
- $correctGrid = false;
- break;
- }
- }
- if ($correctGrid) {
+ if ($this->candidatesOnEachUnknownBoxeOf($testBox->neighbourhood)) {
$solutions = $this->solutionsGenerator($randomized);
foreach($solutions as $solution) {
$stop = (yield $solution);
@@ -171,9 +187,32 @@
}
$testBox->value = UNKNOWN;
} else {
+ foreach(array($this->rows, $this->columns, $this->regions) as $areas) {
+ foreach ($areas as $area) {
+ foreach($area as $box1) {
+ if (($box1->value == UNKNOWN) && (count($box1->candidates) == 0)) {
+ return;
+ }
+ foreach($area as $box2) {
+ if (($box1 !== $box2) && ($box1->value != UNKNOWN) && ($box1->value == $box2->value)) {
+ return;
+ }
+ }
+ }
+ }
+ }
yield $this->toString();
}
}
+
+ function candidatesOnEachUnknownBoxeOf($area) {
+ foreach($area as $box) {
+ if (($box->value == UNKNOWN) && (count($box->candidates) == 0)) {
+ return false;
+ }
+ }
+ return true;
+ }
function toString() {
$str = "";
diff --git a/index.php b/index.php
index 162cc99..57d5214 100644
--- a/index.php
+++ b/index.php
@@ -3,6 +3,8 @@
session_start();
$grid = new Grid();
$grid->generate();
- header("Location: ".$_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"].dirname($_SERVER["DOCUMENT_URI"])."/".$grid->toString());
+ $currentGrid = $grid->toString();
+ $_SESSION[$currentGrid] = "checked";
+ header("Location: ".$_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"].dirname($_SERVER["DOCUMENT_URI"])."/".$currentGrid);
exit();
?>
diff --git a/manifest.json.php b/manifest.json.php
index 12385e9..33cacb8 100644
--- a/manifest.json.php
+++ b/manifest.json.php
@@ -1,6 +1,9 @@
{
"short_name": "Sudoku",
diff --git a/service-worker.js.php b/service-worker.js.php
index 4198d9a..781afdc 100644
--- a/service-worker.js.php
+++ b/service-worker.js.php
@@ -1,6 +1,9 @@
/*
diff --git a/sudoku.php b/sudoku.php
index 7ffd66e..e529a31 100644
--- a/sudoku.php
+++ b/sudoku.php
@@ -3,6 +3,24 @@
session_start();
$currentGrid = strip_tags($_GET['grid']);
$_SESSION["currentGrid"] = $currentGrid;
+
+ if (!isset($_SESSION[$currentGrid])) {
+ $grid = new Grid();
+ $grid->import($currentGrid);
+ if ($grid->containsDuplicates()) {
+ $warning = "Cette grille contient des doublons.";
+ } else {
+ switch($grid->countSolutions(2)) {
+ case 0:
+ $warning = "Cette grille n'a pas de solution.";
+ break;
+ case 1:
+ break;
+ default:
+ $warning = "Cette grille a plusieurs solutions.";
+ }
+ }
+ }
?>
@@ -95,7 +113,12 @@
\n");
+ else
+ echo(" Remplissez la grille de sorte que chaque ligne, colonne et région (carré de 3×3 cases) contienne tous les chiffres de 1 à 9.\n")
+?>