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 {
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,10 +87,9 @@
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)) {
foreach($knownBoxes as $box1) {
foreach($knownBoxes as $box2) {
if (($box1 != $box2) && ($box1->value == $box2->value)) {
return true;
}
}
@ -98,7 +97,6 @@
}
return false;
}
}
function generate() {
// Init with a shuffle row
@ -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();
}
}

View File

@ -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)

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")
?>
</section>
<ul id="contextMenu" class="context-menu"></ul>
<ul id='contextMenu' class='context-menu'></ul>
<footer>
<div id='links'>
<a href=''>Lien vers cette grille</a><br/>
<a href='.'>Nouvelle grille</a><br/>
<a href='.................................................................................'>Grille vierge</a><br/>
<a href='.'>Nouvelle grille</a>
<a href='' id='fixGridLink'>Figer cette grille</a>
</div>
<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>
</footer>
</body>