From 9dcf222c8798138f4eedb1df47bca2e0b9b0baf5 Mon Sep 17 00:00:00 2001 From: adrien Date: Mon, 12 Oct 2020 21:36:09 +0200 Subject: [PATCH] replaceChildren --- classes.php | 33 +++++++++++++++++---------------- favicon.png | Bin 0 -> 542 bytes game.php | 5 +++-- style.css | 4 +++- sudoku.js | 18 ++++++++++++++---- 5 files changed, 37 insertions(+), 23 deletions(-) create mode 100644 favicon.png diff --git a/classes.php b/classes.php index a414893..b34d5f1 100644 --- a/classes.php +++ b/classes.php @@ -1,13 +1,12 @@ value != UNKOWN; + return $box->value != UNKNOWN; } - function isUnknown($box) { - return $box->value == UNKOWN; + return $box->value == UNKNOWN; } function easyFirst($box1, $box2) { @@ -28,19 +27,19 @@ public $values = array("1", "2", "3", "4", "5", "6", "7", "8", "9"); function __construct($rowId, $columnId, $regionId) { - $this->value = UNKOWN; + $this->value = UNKNOWN; $this->rowId = $rowId; $this->columnId = $columnId; $this->regionId = $regionId; $this->candidates = $this->values; - $this->testValueWasAllowed = array(); + $this->candidateRemoved = array(); $this->neighbourhood = array(); } function searchCandidates() { $this->candidates = $this->values; forEach($this->neighbourhood as $neighbour) { - if ($neighbour->value != UNKOWN) + if ($neighbour->value != UNKNOWN) array_unset_value($neighbour->value, $this->candidates); } } @@ -103,7 +102,7 @@ $erasedValues = array(); forEach($testBoxes as $testBox) { $erasedValues[] = $testBox->value; - $testBox->value = UNKOWN; + $testBox->value = UNKNOWN; forEach($testBox->neighbourhood as $neighbour) $neighbour->searchCandidates(); } @@ -146,11 +145,14 @@ if ($randomized) shuffle($testBox->candidates); $stop = null; foreach($testBox->candidates as $testBox->value) { - foreach($testBox->neighbourhood as $neighbour) - $neighbour->testValueWasAllowed[] = array_unset_value($testBox->value, $neighbour->candidates); $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; + if (count($neighbour->candidates) == 0) { + $correctGrid = false; + break; + } } if ($correctGrid) { $solutions = $this->solutionsGenerator($randomized); @@ -161,14 +163,13 @@ break; } } - } - forEach($testBox->neighbourhood as $neighbour) - if (array_pop($neighbour->testValueWasAllowed)) + foreach(array_filter($testBox->neighbourhood, "isUnknown") as $neighbour) + if (array_pop($neighbour->candidateRemoved)) $neighbour->candidates[] = $testBox->value; if ($stop) break; } - $testBox->value = UNKOWN; + $testBox->value = UNKNOWN; } else { yield $this->toString(); } @@ -178,7 +179,7 @@ $str = ""; foreach($this->rows as $row) { forEach($row as $box) { - $str .= ($box->value? $box->value : UNKOWN); + $str .= ($box->value? $box->value : UNKNOWN); } } return $str; diff --git a/favicon.png b/favicon.png new file mode 100644 index 0000000000000000000000000000000000000000..2873f4c7c024e7ee5d0f0bf3857ff4a3c9afc2f9 GIT binary patch literal 542 zcmV+(0^$9MP)e@h=sqvS0l3CafOJOS!VCYnK_r4GZ0K0n(2fF!7RIWCY6lNW#^+U zPlRcsi(W!G&9TO(rqVPV?>7Wgy-uiJjraXK+Lm-jgFvhBh-TTY@R)FV#kCY~2zP0v z+>-FVaNL#y3STHa@AISLqR+32?U~F31Xa&y@fYWRu~xE8;Q`^8X;ihJ^QfCd^z!Vq z)E=3=IAlnXBubtt%NV#Mq?RSK|9ykszDX+L(d2JY!xl%8l6d2yR;8;hwII4KpFvIK z{x?sjFg)aUk0hb_!Si1Fpk)g_H$Cs;!1F#2q3IsZ%5{H5avFUTeY38`_Ml@8PR{FE zej85Lq5ICt*|~42NoKH6fahaq&qC)Fgctm*e%3jg?`!JvG@h2G-u&^ zEs0YA000SaNLh0L01m_e01m_fl`9S#00010NklU|^^R;{RmgdPb1Ov^0=@ z1ei68k>US;R53|#tPI?3P+yaC`aAj9QwEi51wu>poHum?U2D0Ttu0+HC%NwF6O g-T+WKq9{890BYhp`vJol;Q#;t07*qoM6N<$fSudoku +
@@ -29,7 +30,7 @@
- +
diff --git a/style.css b/style.css index 30968bb..ca60dff 100644 --- a/style.css +++ b/style.css @@ -115,11 +115,13 @@ input::placeholder { } .highlight-buttons { - column-gap: 0.2em; + column-gap: 0.3em; } button, input[type="color"] { border: 2px outset #6666ff; border-radius: 4px; + font-size: 1em; + margin: 0 0 1px 0; } button { padding: 0 8px 2px 8px; diff --git a/sudoku.js b/sudoku.js index e7e4f8c..639ef0c 100644 --- a/sudoku.js +++ b/sudoku.js @@ -1,4 +1,5 @@ const VALUES = "123456789" +const SUGESTION_DELAY = 60000 //ms let boxes = [] let rows = Array.from(Array(9), x => []) @@ -56,9 +57,7 @@ function searchCandidatesOf(box) { function showCandidatesOn(box) { if (!box.disabled) { - while (box.list.firstChild) { - box.list.firstChild.remove() - } + box.list.replaceChildren() if (!box.value && box.candidates.size) { const candidatesArray = Array.from(box.candidates).sort() candidatesArray.forEach(candidate => { @@ -128,7 +127,7 @@ function refresh(box) { alert(`Bravo ! Vous avez résolu la grille.`) } else { if (suggestionTimer) clearTimeout(suggestionTimer) - suggestionTimer = setTimeout(showSuggestion, 30000) + suggestionTimer = setTimeout(showSuggestion, SUGESTION_DELAY) } } else { // Errors on grid box.reportValidity() @@ -209,3 +208,14 @@ function showSuggestion() { suggestionTimer = null } } + +function clearAll() { + boxes.filter(box => !box.disabled).forEach(box => { + box.value = "" + box.placeholder = "" + }) + boxes.forEach(searchCandidatesOf) + boxes.forEach(showCandidatesOn) + enableButtons() + highlightAndTab() +}