From 9b2f1f9d782911b706663617922520cb3f835ef2 Mon Sep 17 00:00:00 2001 From: adrien Date: Sat, 14 Nov 2020 01:24:43 +0100 Subject: [PATCH] freeze grid, fix containsDuplicates --- classes.php | 31 +++++++++---------------------- sudoku.js | 5 ++++- sudoku.php | 8 +++++--- 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/classes.php b/classes.php index 15092b8..3d3fc6a 100644 --- a/classes.php +++ b/classes.php @@ -24,7 +24,7 @@ } class Box { - public $values = array("1", "2", "3", "4", "5", "6", "7", "8", "9"); + public $values = array('1', '2', '3', '4', '5', '6', '7', '8', '9'); function __construct($rowId, $columnId, $regionId) { $this->value = UNKNOWN; @@ -87,17 +87,15 @@ function containsDuplicates() { foreach(array_merge($this->rows, $this->columns, $this->regions) as $area) { $knownBoxes = array_filter($area, "isKnown"); - foreach($area as $box1) { - if ($box1->value != UNKNOWN) { - foreach($area as $box2) { - if (($box1 !== $box2) && ($box1->value == $box2->value)) { - return true; - } + foreach($knownBoxes as $box1) { + foreach($knownBoxes as $box2) { + if (($box1 != $box2) && ($box1->value == $box2->value)) { + return true; } } } - return false; } + return false; } function generate() { @@ -143,6 +141,8 @@ } function countSolutions($max=2) { + if ($this->containsDuplicates()) + return 0; $solutions = $this->solutionsGenerator(false); $solutionsWithoutDuplicates = array(); $nbSolutions = 0; @@ -151,6 +151,7 @@ $nbSolutions = count($solutionsWithoutDuplicates); if ($nbSolutions >= $max) { $solutions->send(true); + break; } } return $nbSolutions; @@ -189,20 +190,6 @@ } $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(); } } diff --git a/sudoku.js b/sudoku.js index 22eea3e..5813052 100644 --- a/sudoku.js +++ b/sudoku.js @@ -60,6 +60,7 @@ window.onload = function () { box.previousValue = savedGame[i] } }) + fixGridLink.href = savedGame } boxes.forEach(box => { @@ -148,7 +149,9 @@ function oninput() { } function refreshBox(box) { - localStorage[location.href] = boxes.map(box => box.value || ".").join("") + let saveGame = boxes.map(box => box.value || UNKNOWN).join("") + localStorage[location.href] = saveGame + fixGridLink.href = saveGame box.neighbourhood.concat([box]).forEach(neighbour => { searchCandidatesOf(neighbour) diff --git a/sudoku.php b/sudoku.php index 5749c54..8342a7e 100644 --- a/sudoku.php +++ b/sudoku.php @@ -120,15 +120,17 @@ 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") ?> - +