freeze grid, fix containsDuplicates

This commit is contained in:
Adrien MALINGREY 2020-11-14 01:24:43 +01:00
parent 3b8e9b85ea
commit 9b2f1f9d78
3 changed files with 18 additions and 26 deletions

View File

@ -24,7 +24,7 @@
} }
class Box { 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) { function __construct($rowId, $columnId, $regionId) {
$this->value = UNKNOWN; $this->value = UNKNOWN;
@ -87,17 +87,15 @@
function containsDuplicates() { function containsDuplicates() {
foreach(array_merge($this->rows, $this->columns, $this->regions) as $area) { foreach(array_merge($this->rows, $this->columns, $this->regions) as $area) {
$knownBoxes = array_filter($area, "isKnown"); $knownBoxes = array_filter($area, "isKnown");
foreach($area as $box1) { foreach($knownBoxes as $box1) {
if ($box1->value != UNKNOWN) { foreach($knownBoxes as $box2) {
foreach($area as $box2) { if (($box1 != $box2) && ($box1->value == $box2->value)) {
if (($box1 !== $box2) && ($box1->value == $box2->value)) { return true;
return true;
}
} }
} }
} }
return false;
} }
return false;
} }
function generate() { function generate() {
@ -143,6 +141,8 @@
} }
function countSolutions($max=2) { function countSolutions($max=2) {
if ($this->containsDuplicates())
return 0;
$solutions = $this->solutionsGenerator(false); $solutions = $this->solutionsGenerator(false);
$solutionsWithoutDuplicates = array(); $solutionsWithoutDuplicates = array();
$nbSolutions = 0; $nbSolutions = 0;
@ -151,6 +151,7 @@
$nbSolutions = count($solutionsWithoutDuplicates); $nbSolutions = count($solutionsWithoutDuplicates);
if ($nbSolutions >= $max) { if ($nbSolutions >= $max) {
$solutions->send(true); $solutions->send(true);
break;
} }
} }
return $nbSolutions; return $nbSolutions;
@ -189,20 +190,6 @@
} }
$testBox->value = UNKNOWN; $testBox->value = UNKNOWN;
} else { } 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(); yield $this->toString();
} }
} }

View File

@ -60,6 +60,7 @@ window.onload = function () {
box.previousValue = savedGame[i] box.previousValue = savedGame[i]
} }
}) })
fixGridLink.href = savedGame
} }
boxes.forEach(box => { boxes.forEach(box => {
@ -148,7 +149,9 @@ function oninput() {
} }
function refreshBox(box) { 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 => { box.neighbourhood.concat([box]).forEach(neighbour => {
searchCandidatesOf(neighbour) searchCandidatesOf(neighbour)

View File

@ -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") 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")
?> ?>
</section> </section>
<ul id="contextMenu" class="context-menu"></ul> <ul id='contextMenu' class='context-menu'></ul>
<footer> <footer>
<div id='links'> <div id='links'>
<a href=''>Lien vers cette grille</a><br/> <a href=''>Lien vers cette grille</a><br/>
<a href='.'>Nouvelle grille</a><br/>
<a href='.................................................................................'>Grille vierge</a><br/> <a href='.................................................................................'>Grille vierge</a><br/>
<a href='.'>Nouvelle grille</a> <a href='' id='fixGridLink'>Figer cette grille</a>
</div> </div>
<div class='credits'> <div class='credits'>
Icônes par <a href="https://www.flaticon.com/authors/freepik" title="Freepik">Freepik</a> chez <a href="https://www.flaticon.com/" title="Flaticon">www.flaticon.com</a> Icônes par <a href='https://www.flaticon.com/authors/freepik' title='Freepik'>Freepik</a> chez <a href='https://www.flaticon.com/' title='Flaticon'>www.flaticon.com</a>
</div> </div>
</footer> </footer>
</body> </body>